Module: CallableTree::Node::Hooks::Terminator

Included in:
Root
Defined in:
lib/callable_tree/node/hooks/terminator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#after_terminator_callbacksObject



82
83
84
# File 'lib/callable_tree/node/hooks/terminator.rb', line 82

def after_terminator_callbacks
  @after_terminator_callbacks ||= []
end

#around_terminator_callbacksObject



78
79
80
# File 'lib/callable_tree/node/hooks/terminator.rb', line 78

def around_terminator_callbacks
  @around_terminator_callbacks ||= []
end

#before_terminator_callbacksObject



74
75
76
# File 'lib/callable_tree/node/hooks/terminator.rb', line 74

def before_terminator_callbacks
  @before_terminator_callbacks ||= []
end

Class Method Details

.included(_subclass) ⇒ Object



7
8
9
# File 'lib/callable_tree/node/hooks/terminator.rb', line 7

def self.included(_subclass)
  raise ::CallableTree::Error, "#{self} must be prepended"
end

Instance Method Details

#after_terminator(&block) ⇒ Object



29
30
31
# File 'lib/callable_tree/node/hooks/terminator.rb', line 29

def after_terminator(&block)
  clone.after_terminator!(&block)
end

#after_terminator!(&block) ⇒ Object



33
34
35
36
# File 'lib/callable_tree/node/hooks/terminator.rb', line 33

def after_terminator!(&block)
  after_terminator_callbacks << block
  self
end

#around_terminator(&block) ⇒ Object



20
21
22
# File 'lib/callable_tree/node/hooks/terminator.rb', line 20

def around_terminator(&block)
  clone.around_terminator!(&block)
end

#around_terminator!(&block) ⇒ Object



24
25
26
27
# File 'lib/callable_tree/node/hooks/terminator.rb', line 24

def around_terminator!(&block)
  around_terminator_callbacks << block
  self
end

#before_terminator(&block) ⇒ Object



11
12
13
# File 'lib/callable_tree/node/hooks/terminator.rb', line 11

def before_terminator(&block)
  clone.before_terminator!(&block)
end

#before_terminator!(&block) ⇒ Object



15
16
17
18
# File 'lib/callable_tree/node/hooks/terminator.rb', line 15

def before_terminator!(&block)
  before_terminator_callbacks << block
  self
end

#terminate?(output, *inputs, **options) ⇒ Boolean



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/callable_tree/node/hooks/terminator.rb', line 38

def terminate?(output, *inputs, **options)
  output = before_terminator_callbacks.reduce(output) do |output, callable|
    callable.call(output, *inputs, **options, _node_: self)
  end

  terminated =
    if around_terminator_callbacks.empty?
      super(output, *inputs, **options)
    else
      around_terminator_callbacks_head, *around_terminator_callbacks_tail = around_terminator_callbacks
      terminator = proc { super(output, *inputs, **options) }

      terminated =
        around_terminator_callbacks_head
        .call(
          output,
          *inputs,
          **options,
          _node_: self
        ) { terminator.call }

      around_terminator_callbacks_tail.reduce(terminated) do |terminated, callable|
        callable.call(
          output,
          *inputs,
          **options,
          _node_: self
        ) { terminated }
      end
    end

  after_terminator_callbacks.reduce(terminated) do |terminated, callable|
    callable.call(terminated, **options, _node_: self)
  end
end