Class: Swarm::Engine::Worker::Command

Inherits:
Object
  • Object
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( || {})
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



17
18
19
# File 'lib/swarm/engine/worker/command.rb', line 17

def action
  @action
end

#hiveObject (readonly)

Returns the value of attribute hive.



17
18
19
# File 'lib/swarm/engine/worker/command.rb', line 17

def hive
  @hive
end

#metadataObject (readonly)

Returns the value of attribute metadata.



17
18
19
# File 'lib/swarm/engine/worker/command.rb', line 17

def 
  @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,  = data.values_at(:action, :metadata)
  new(action: action, 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

#objectObject



39
40
41
42
43
44
45
# File 'lib/swarm/engine/worker/command.rb', line 39

def object
  @object ||= begin
    return nil unless [:type] && [:id]

    hive.fetch([:type], [:id])
  end
end

#observersObject



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

Raises:



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

Returns:

  • (Boolean)


47
48
49
# File 'lib/swarm/engine/worker/command.rb', line 47

def stop?
  action == "stop_worker"
end

#to_hashObject



51
52
53
54
55
56
57
# File 'lib/swarm/engine/worker/command.rb', line 51

def to_hash
  {
    action: action,
    metadata: ,
    object: object
  }
end