Module: CallableTree::Node::Hooks::Caller
- Included in:
- Root
- Defined in:
- lib/callable_tree/node/hooks/caller.rb
Instance Attribute Summary collapse
- #after_caller_callbacks ⇒ Object readonly
- #around_caller_callbacks ⇒ Object readonly
- #before_caller_callbacks ⇒ Object readonly
Class Method Summary collapse
Instance Method Summary collapse
- #after_call(&block) ⇒ Object (also: #after_caller)
- #after_call!(&block) ⇒ Object (also: #after_caller!)
- #around_call(&block) ⇒ Object (also: #around_caller)
- #around_call!(&block) ⇒ Object (also: #around_caller!)
- #before_call(&block) ⇒ Object (also: #before_caller)
- #before_call!(&block) ⇒ Object (also: #before_caller!)
- #call(*inputs, **options) ⇒ Object
Instance Attribute Details
#after_caller_callbacks ⇒ Object
91 92 93 |
# File 'lib/callable_tree/node/hooks/caller.rb', line 91 def after_caller_callbacks @after_caller_callbacks ||= [] end |
#around_caller_callbacks ⇒ Object
87 88 89 |
# File 'lib/callable_tree/node/hooks/caller.rb', line 87 def around_caller_callbacks @around_caller_callbacks ||= [] end |
#before_caller_callbacks ⇒ Object
83 84 85 |
# File 'lib/callable_tree/node/hooks/caller.rb', line 83 def before_caller_callbacks @before_caller_callbacks ||= [] end |
Class Method Details
.included(_subclass) ⇒ Object
7 8 9 |
# File 'lib/callable_tree/node/hooks/caller.rb', line 7 def self.included(_subclass) raise ::CallableTree::Error, "#{self} must be prepended" end |
Instance Method Details
#after_call(&block) ⇒ Object Also known as: after_caller
29 30 31 |
# File 'lib/callable_tree/node/hooks/caller.rb', line 29 def after_call(&block) clone.after_call!(&block) end |
#after_call!(&block) ⇒ Object Also known as: after_caller!
33 34 35 36 |
# File 'lib/callable_tree/node/hooks/caller.rb', line 33 def after_call!(&block) after_caller_callbacks << block self end |
#around_call(&block) ⇒ Object Also known as: around_caller
20 21 22 |
# File 'lib/callable_tree/node/hooks/caller.rb', line 20 def around_call(&block) clone.around_call!(&block) end |
#around_call!(&block) ⇒ Object Also known as: around_caller!
24 25 26 27 |
# File 'lib/callable_tree/node/hooks/caller.rb', line 24 def around_call!(&block) around_caller_callbacks << block self end |
#before_call(&block) ⇒ Object Also known as: before_caller
11 12 13 |
# File 'lib/callable_tree/node/hooks/caller.rb', line 11 def before_call(&block) clone.before_call!(&block) end |
#before_call!(&block) ⇒ Object Also known as: before_caller!
15 16 17 18 |
# File 'lib/callable_tree/node/hooks/caller.rb', line 15 def before_call!(&block) before_caller_callbacks << block self end |
#call(*inputs, **options) ⇒ Object
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 73 74 75 76 77 78 79 80 81 |
# File 'lib/callable_tree/node/hooks/caller.rb', line 45 def call(*inputs, **) input_head, *input_tail = inputs input_head = before_caller_callbacks.reduce(input_head) do |input_head, callable| callable.call(input_head, *input_tail, **, _node_: self) end output = if around_caller_callbacks.empty? super(input_head, *input_tail, **) else around_caller_callbacks_head, *around_caller_callbacks_tail = around_caller_callbacks caller = proc { super(input_head, *input_tail, **) } output = around_caller_callbacks_head .call( input_head, *input_tail, **, _node_: self ) { caller.call } around_caller_callbacks_tail.reduce(output) do |output, callable| callable.call( input_head, *input_tail, **, _node_: self ) { output } end end after_caller_callbacks.reduce(output) do |output, callable| callable.call(output, **, _node_: self) end end |