callable_tree 0.3.5 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -0
  3. data/.ruby-version +1 -1
  4. data/CHANGELOG.md +17 -0
  5. data/Gemfile.lock +2 -2
  6. data/README.md +366 -108
  7. data/callable_tree.gemspec +1 -0
  8. data/examples/builder/hooks.rb +67 -0
  9. data/examples/builder/internal-seekable.rb +69 -73
  10. data/examples/builder/logging.rb +129 -0
  11. data/examples/{external-verbosify.rb → class/external-verbosify.rb} +1 -1
  12. data/examples/class/hooks.rb +70 -0
  13. data/examples/{identity.rb → class/identity.rb} +1 -1
  14. data/examples/{internal-broadcastable.rb → class/internal-broadcastable.rb} +0 -0
  15. data/examples/{internal-composable.rb → class/internal-composable.rb} +0 -0
  16. data/examples/{internal-seekable.rb → class/internal-seekable.rb} +1 -1
  17. data/examples/{logging.rb → class/logging.rb} +45 -43
  18. data/lib/callable_tree/node/builder.rb +20 -4
  19. data/lib/callable_tree/node/external/builder.rb +1 -1
  20. data/lib/callable_tree/node/external/verbose.rb +1 -1
  21. data/lib/callable_tree/node/external.rb +15 -0
  22. data/lib/callable_tree/node/hooks/caller.rb +110 -0
  23. data/lib/callable_tree/node/hooks/matcher.rb +101 -0
  24. data/lib/callable_tree/node/hooks/terminator.rb +99 -0
  25. data/lib/callable_tree/node/internal.rb +27 -12
  26. data/lib/callable_tree/node/root.rb +3 -1
  27. data/lib/callable_tree/node.rb +8 -0
  28. data/lib/callable_tree/version.rb +1 -1
  29. data/lib/callable_tree.rb +3 -1
  30. metadata +17 -12
  31. data/examples/builder/hooks-call.rb +0 -38
  32. data/examples/hooks-call.rb +0 -39
  33. data/lib/callable_tree/node/hooks/call.rb +0 -81
@@ -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