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(action:, metadata:, hive: Hive.default) ⇒ Command
Returns a new instance of Command.
19
20
21
22
23
|
# File 'lib/swarm/engine/worker/command.rb', line 19
def initialize(action:, metadata:, hive: Hive.default)
@hive = hive
@action = action
@metadata = Swarm::Support.symbolize_keys(metadata || {})
end
|
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
17
18
19
|
# File 'lib/swarm/engine/worker/command.rb', line 17
def action
@action
end
|
#hive ⇒ Object
Returns the value of attribute hive.
17
18
19
|
# File 'lib/swarm/engine/worker/command.rb', line 17
def hive
@hive
end
|
Returns the value of attribute metadata.
17
18
19
|
# File 'lib/swarm/engine/worker/command.rb', line 17
def metadata
@metadata
end
|
Class Method Details
.from_job(job, hive: Hive.default) ⇒ Object
10
11
12
13
14
|
# File 'lib/swarm/engine/worker/command.rb', line 10
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
59
60
61
|
# File 'lib/swarm/engine/worker/command.rb', line 59
def ==(other)
other.is_a?(self.class) && other.to_hash == to_hash
end
|
#object ⇒ Object
39
40
41
42
43
44
45
|
# File 'lib/swarm/engine/worker/command.rb', line 39
def object
@object ||= begin
return nil unless metadata[:type] && metadata[:id]
hive.fetch(metadata[:type], metadata[:id])
end
end
|
#observers ⇒ Object
33
34
35
36
37
|
# File 'lib/swarm/engine/worker/command.rb', line 33
def observers
@observers ||= hive.registered_observers.map { |observer_class|
observer_class.new(self)
}
end
|
#run! ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/swarm/engine/worker/command.rb', line 25
def run!
raise MissingObjectError if object.nil?
observers.each(&:before_action)
object.send("_#{action}")
observers.each(&:after_action)
end
|
#stop? ⇒ Boolean
47
48
49
|
# File 'lib/swarm/engine/worker/command.rb', line 47
def stop?
action == "stop_worker"
end
|
#to_hash ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/swarm/engine/worker/command.rb', line 51
def to_hash
{
action: action,
metadata: metadata,
object: object
}
end
|