callable_tree 0.3.4 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,81 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CallableTree
4
- module Node
5
- module Hooks
6
- module Call
7
- def self.included(_subclass)
8
- raise ::CallableTree::Error, "#{self} must be prepended"
9
- end
10
-
11
- def before_call(&block)
12
- clone.before_call!(&block)
13
- end
14
-
15
- def before_call!(&block)
16
- before_callbacks << block
17
- self
18
- end
19
-
20
- def around_call(&block)
21
- clone.around_call!(&block)
22
- end
23
-
24
- def around_call!(&block)
25
- around_callbacks << block
26
- self
27
- end
28
-
29
- def after_call(&block)
30
- clone.after_call!(&block)
31
- end
32
-
33
- def after_call!(&block)
34
- after_callbacks << block
35
- self
36
- end
37
-
38
- def call(*inputs, **options)
39
- input_head, *input_tail = inputs
40
-
41
- input_head = before_callbacks.reduce(input_head) do |input_head, callable|
42
- callable.call(input_head, *input_tail, self, **options)
43
- end
44
-
45
- output = super(input_head, *input_tail, **options)
46
-
47
- output = around_callbacks.reduce(output) do |output, callable|
48
- callable.call(input_head, *input_tail, self, **options) { output }
49
- end
50
-
51
- after_callbacks.reduce(output) do |output, callable|
52
- callable.call(output, self, **options)
53
- end
54
- end
55
-
56
- def before_callbacks
57
- @before_callbacks ||= []
58
- end
59
-
60
- def around_callbacks
61
- @around_callbacks ||= []
62
- end
63
-
64
- def after_callbacks
65
- @after_callbacks ||= []
66
- end
67
-
68
- private
69
-
70
- attr_writer :before_callbacks, :around_callbacks, :after_callbacks
71
-
72
- def initialize_copy(_node)
73
- super
74
- self.before_callbacks = before_callbacks.map(&:itself)
75
- self.around_callbacks = around_callbacks.map(&:itself)
76
- self.after_callbacks = after_callbacks.map(&:itself)
77
- end
78
- end
79
- end
80
- end
81
- end