Class: WorkflowTemplate::Definer::HasActions::Position

Inherits:
Object
  • Object
show all
Defined in:
lib/workflow_template/definer/has_actions/position.rb

Defined Under Namespace

Classes: After, At, Before

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reference) ⇒ Position

Returns a new instance of Position.



39
40
41
42
# File 'lib/workflow_template/definer/has_actions/position.rb', line 39

def initialize(reference)
  @reference = reference&.to_sym
  freeze
end

Instance Attribute Details

#referenceObject (readonly)

Returns the value of attribute reference.



53
54
55
# File 'lib/workflow_template/definer/has_actions/position.rb', line 53

def reference
  @reference
end

Class Method Details

.instance(type, reference) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/workflow_template/definer/has_actions/position.rb', line 9

def self.instance(type, reference)
  case type
  when :before
    Position::Before.new(reference)
  when :after
    Position::After.new(reference)
  when :at
    raise Error, 'Reference not expected to be nil' if reference.nil?

    Position::At.new(reference)
  else
    raise Error, "Unexpected position: #{type}"
  end
end

.slice_actions(actions, reference) ⇒ Object

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/workflow_template/definer/has_actions/position.rb', line 24

def self.slice_actions(actions, reference)
  index, *rest = actions.each_index.reduce([]) do |result, index|
    next result unless actions[index].name == reference

    [*result, index]
  end
  raise Error, "Reference not found: #{reference}" if index.nil?
  raise Error, "Duplicate action name: #{reference}" if rest.length.positive?

  before = actions[0...index]
  action = actions[index]
  after = actions[(index + 1)..]
  [before, action, after]
end

Instance Method Details

#apply_onto(actions, action) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/workflow_template/definer/has_actions/position.rb', line 44

def apply_onto(actions, action)
  if reference.nil?
    apply_without_reference(actions, action)
  else
    before, reference, after = self.class.slice_actions(actions, self.reference)
    apply_with_reference(before, action, reference, after)
  end
end