Class: Swarm::Engine::Worker::Command
- Inherits:
-
Object
- Object
- Swarm::Engine::Worker::Command
show all
- Defined in:
- lib/swarm/engine/worker/command.rb
Defined Under Namespace
Classes: MissingObjectError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hive: Hive.default, action:, metadata:) ⇒ Command
Returns a new instance of Command.
17
18
19
20
21
|
# File 'lib/swarm/engine/worker/command.rb', line 17
def initialize(hive: Hive.default, action:, metadata:)
@hive = hive
@action = action
@metadata = Swarm::Support.symbolize_keys(metadata || {})
end
|
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
15
16
17
|
# File 'lib/swarm/engine/worker/command.rb', line 15
def action
@action
end
|
#hive ⇒ Object
Returns the value of attribute hive.
15
16
17
|
# File 'lib/swarm/engine/worker/command.rb', line 15
def hive
@hive
end
|
Returns the value of attribute metadata.
15
16
17
|
# File 'lib/swarm/engine/worker/command.rb', line 15
def metadata
@metadata
end
|
Class Method Details
.from_job(job, hive: Hive.default) ⇒ Object
8
9
10
11
12
|
# File 'lib/swarm/engine/worker/command.rb', line 8
def from_job(job, hive: Hive.default)
data = job.to_h
action, metadata = data.values_at(:action, :metadata)
new(action: action, metadata: metadata, hive: hive)
end
|
Instance Method Details
#==(other) ⇒ Object
55
56
57
|
# File 'lib/swarm/engine/worker/command.rb', line 55
def ==(other)
other.is_a?(self.class) && other.to_hash == to_hash
end
|
#object ⇒ Object
36
37
38
39
40
41
|
# File 'lib/swarm/engine/worker/command.rb', line 36
def object
@object ||= begin
return nil unless metadata[:type] && metadata[:id]
hive.fetch(metadata[:type], metadata[:id])
end
end
|
#observers ⇒ Object
30
31
32
33
34
|
# File 'lib/swarm/engine/worker/command.rb', line 30
def observers
@observers ||= hive.registered_observers.map { |observer_class|
observer_class.new(self)
}
end
|
#run! ⇒ Object
23
24
25
26
27
28
|
# File 'lib/swarm/engine/worker/command.rb', line 23
def run!
raise MissingObjectError if object.nil?
observers.each(&:before_action)
object.send("_#{action}")
observers.each(&:after_action)
end
|
#stop? ⇒ Boolean
43
44
45
|
# File 'lib/swarm/engine/worker/command.rb', line 43
def stop?
action == "stop_worker"
end
|
#to_hash ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/swarm/engine/worker/command.rb', line 47
def to_hash
{
action: action,
metadata: metadata,
object: object
}
end
|