Class: Shifty::Gang

Inherits:
Object
  • Object
show all
Includes:
PolicyDeclarable, Taggable
Defined in:
lib/shifty/gang.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PolicyDeclarable

#freeze!, #with_policy

Methods included from Taggable

#criteria=, #criteria_passes?, #has_tag?, #tags=

Constructor Details

#initialize(workers = [], p = {}) ⇒ Gang

Returns a new instance of Gang.



11
12
13
14
15
16
# File 'lib/shifty/gang.rb', line 11

def initialize(workers = [], p = {})
  @roster = Roster.new(workers)
  self.criteria = p[:criteria]
  self.tags     = p[:tags]
  self.pipeline_policy = Policy.validate!(p[:policy]) if p[:policy]
end

Instance Attribute Details

#pipeline_policyObject

Returns the value of attribute pipeline_policy.



24
25
26
# File 'lib/shifty/gang.rb', line 24

def pipeline_policy
  @pipeline_policy
end

#rosterObject (readonly)

Returns the value of attribute roster.



6
7
8
# File 'lib/shifty/gang.rb', line 6

def roster
  @roster
end

#tagsObject (readonly)

Returns the value of attribute tags.



6
7
8
# File 'lib/shifty/gang.rb', line 6

def tags
  @tags
end

Class Method Details

.[](*workers) ⇒ Object



70
71
72
# File 'lib/shifty/gang.rb', line 70

def [](*workers)
  new workers
end

Instance Method Details

#append(worker) ⇒ Object



55
56
57
58
# File 'lib/shifty/gang.rb', line 55

def append(worker)
  roster << worker
  worker.pipeline_policy = pipeline_policy if pipeline_policy
end

#freeze_topology_node!Object

Freezes every roster member plus the roster's own membership, so append/push/pop/shift/unshift all raise FrozenError afterward.



62
63
64
65
66
67
# File 'lib/shifty/gang.rb', line 62

def freeze_topology_node!
  roster.workers.each(&:freeze_topology_node!)
  roster.workers.freeze
  roster.freeze
  freeze
end

#ready_to_work?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/shifty/gang.rb', line 37

def ready_to_work?
  roster.first.ready_to_work?
end

#shiftObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/shifty/gang.rb', line 26

def shift
  if criteria_passes?
    roster.last.shift
  else
    # Even when the gang's criteria bypasses its workers, the value
    # still crosses the gang's boundary — govern it with the entry
    # worker's policy, matching Worker#shift's bypass behavior.
    roster.first.intake(roster.first.supply.shift)
  end
end

#supplies(subscribing_worker) ⇒ Object Also known as: |



49
50
51
52
# File 'lib/shifty/gang.rb', line 49

def supplies(subscribing_worker)
  subscribing_worker.supply = self
  subscribing_worker
end

#supplyObject



41
42
43
# File 'lib/shifty/gang.rb', line 41

def supply
  roster.first&.supply
end

#supply=(supplier) ⇒ Object



45
46
47
# File 'lib/shifty/gang.rb', line 45

def supply=(supplier)
  roster.first.supply = supplier
end