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(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( || {})
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



15
16
17
# File 'lib/swarm/engine/worker/command.rb', line 15

def action
  @action
end

#hiveObject (readonly)

Returns the value of attribute hive.



15
16
17
# File 'lib/swarm/engine/worker/command.rb', line 15

def hive
  @hive
end

#metadataObject (readonly)

Returns the value of attribute metadata.



15
16
17
# File 'lib/swarm/engine/worker/command.rb', line 15

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

#objectObject



36
37
38
39
40
41
# File 'lib/swarm/engine/worker/command.rb', line 36

def object
  @object ||= begin
    return nil unless [:type] && [:id]
    hive.fetch([:type], [:id])
  end
end

#observersObject



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

Raises:



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

Returns:

  • (Boolean)


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

def stop?
  action == "stop_worker"
end

#to_hashObject



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

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