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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -2
- data/Gemfile.lock +2 -2
- data/README.md +47 -47
- data/examples/builder/{hooks-call.rb → hooks-caller.rb} +7 -7
- data/examples/builder/{internal-compose.rb → internal-broadcastable.rb} +3 -3
- data/examples/builder/{internal-broadcast.rb → internal-composable.rb} +3 -3
- data/examples/builder/internal-seekable.rb +93 -0
- data/examples/builder/logging.rb +126 -0
- data/examples/{hooks-call.rb → hooks-caller.rb} +8 -8
- data/examples/{internal-broadcast.rb → internal-broadcastable.rb} +6 -6
- data/examples/{internal-compose.rb → internal-composable.rb} +6 -6
- data/examples/{internal-seek.rb → internal-seekable.rb} +3 -3
- data/lib/callable_tree/node/builder.rb +18 -9
- data/lib/callable_tree/node/external/builder.rb +2 -2
- data/lib/callable_tree/node/external/verbose.rb +1 -1
- data/lib/callable_tree/node/external.rb +15 -0
- data/lib/callable_tree/node/hooks/caller.rb +110 -0
- data/lib/callable_tree/node/hooks/matcher.rb +101 -0
- data/lib/callable_tree/node/internal/builder.rb +1 -1
- data/lib/callable_tree/node/internal.rb +30 -3
- data/lib/callable_tree/node/root.rb +2 -1
- data/lib/callable_tree/node.rb +8 -0
- data/lib/callable_tree/version.rb +1 -1
- data/lib/callable_tree.rb +2 -1
- metadata +14 -12
- data/examples/builder/internal-seek.rb +0 -97
- data/lib/callable_tree/node/hooks/call.rb +0 -81
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ebaae495d65a776fa0eea8c4aa5e23a1dca39245f05531b1d79bb7384c27228
|
4
|
+
data.tar.gz: af843950714bc7302b3fab17fc23a775e5f436f97923aad80783998794d110ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5311c4a2ad2436815362a8349e278b73c8c568a4bf4721c990d7a5b5c8c74d876267dc5729c67f0e6e9a4c540e99cffa3d9ece8b9046acf057ca2f48e20a97ab
|
7
|
+
data.tar.gz: baa36b79d0ad3ba777fcad27c8b04c7ff1b1da7d665c1f74dbadf49e6d36828ed0fc3d0eaa1cc45c54107e24806a8ec66e4b719095e055260fec8a3ec16669c2
|
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,35 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.3.7] - 2022-04-09
|
4
|
+
|
5
|
+
- Add `CallableTree::Node#internal?`
|
6
|
+
- Add `CallableTree::Node#external?`
|
7
|
+
- (Experimental) Change the callables (matcher, caller, terminator) specified in the builder style to receive the current node instance as the `_node_` keyword argument.
|
8
|
+
|
9
|
+
## [0.3.6] - 2022-03-29
|
10
|
+
|
11
|
+
- (Experimental) Add `CallableTree::Node::Hooks::Matcher`.
|
12
|
+
Using this together with `CallableTree::Node::Hooks::Caller` helps to output logs. See `examples/builder/logging.rb` for details.
|
13
|
+
|
14
|
+
## [0.3.5] - 2022-03-20
|
15
|
+
|
16
|
+
- Add `CallableTree::Node::Internal#seekable?` as an alias for `CallableTree::Node::Internal#seek?`.
|
17
|
+
- Add `CallableTree::Node::Internal#seekable` as an alias for `CallableTree::Node::Internal#seek`.
|
18
|
+
- Add `CallableTree::Node::Internal#seekable!` as an alias for `CallableTree::Node::Internal#seek!`.
|
19
|
+
- Add `CallableTree::Node::Internal#broadcastable?` as an alias for `CallableTree::Node::Internal#broadcast?`.
|
20
|
+
- Add `CallableTree::Node::Internal#broadcastable` as an alias for `CallableTree::Node::Internal#broadcast`.
|
21
|
+
- Add `CallableTree::Node::Internal#broadcastable!` as an alias for `CallableTree::Node::Internal#broadcast!`.
|
22
|
+
- Add `CallableTree::Node::Internal#composable?` as an alias for `CallableTree::Node::Internal#compose?`.
|
23
|
+
- Add `CallableTree::Node::Internal#composable` as an alias for `CallableTree::Node::Internal#compose`.
|
24
|
+
- Add `CallableTree::Node::Internal#composable!` as an alias for `CallableTree::Node::Internal#compose!`.
|
25
|
+
- (Experimental) Add `CallableTree::Node::Internal::Builder#terminator` to use instead of `CallableTree::Node::Internal::Builder#terminater`.
|
26
|
+
See `examples/builder/*.rb` for details.
|
27
|
+
|
3
28
|
## [0.3.4] - 2022-03-13
|
4
29
|
|
5
|
-
- Add `CallableTree::Node::Internal::Builder`.
|
30
|
+
- (Experimental) Add `CallableTree::Node::Internal::Builder`.
|
6
31
|
See `examples/builder/*.rb` for details.
|
7
|
-
- Add `CallableTree::Node::External::Builder`.
|
32
|
+
- (Experimental) Add `CallableTree::Node::External::Builder`.
|
8
33
|
See `examples/builder/*.rb` for details.
|
9
34
|
|
10
35
|
## [0.3.3] - 2022-02-19
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
callable_tree (0.3.
|
4
|
+
callable_tree (0.3.7)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -17,7 +17,7 @@ GEM
|
|
17
17
|
rspec-expectations (3.11.0)
|
18
18
|
diff-lcs (>= 1.2.0, < 2.0)
|
19
19
|
rspec-support (~> 3.11.0)
|
20
|
-
rspec-mocks (3.11.
|
20
|
+
rspec-mocks (3.11.1)
|
21
21
|
diff-lcs (>= 1.2.0, < 2.0)
|
22
22
|
rspec-support (~> 3.11.0)
|
23
23
|
rspec-support (3.11.0)
|
data/README.md
CHANGED
@@ -32,11 +32,11 @@ Builds a tree by linking instances of the nodes. The `call` method of the node w
|
|
32
32
|
|
33
33
|
### Basic
|
34
34
|
|
35
|
-
#### `CallableTree::Node::Internal#
|
35
|
+
#### `CallableTree::Node::Internal#seekable` (default)
|
36
36
|
|
37
37
|
This strategy does not call the next sibling node if the `call` method of the current node returns a value other than `nil`. This behavior is changeable by overriding the `terminate?` method.
|
38
38
|
|
39
|
-
`examples/internal-
|
39
|
+
`examples/internal-seekable.rb`:
|
40
40
|
```ruby
|
41
41
|
module Node
|
42
42
|
module JSON
|
@@ -129,17 +129,17 @@ module Node
|
|
129
129
|
end
|
130
130
|
end
|
131
131
|
|
132
|
-
# The `
|
133
|
-
tree = CallableTree::Node::Root.new.append(
|
134
|
-
Node::JSON::Parser.new.append(
|
132
|
+
# The `seekable` method call can be omitted since it is the default strategy.
|
133
|
+
tree = CallableTree::Node::Root.new.seekable.append(
|
134
|
+
Node::JSON::Parser.new.seekable.append(
|
135
135
|
Node::JSON::Scraper.new(type: :animals),
|
136
136
|
Node::JSON::Scraper.new(type: :fruits)
|
137
|
-
)
|
138
|
-
Node::XML::Parser.new.append(
|
137
|
+
),
|
138
|
+
Node::XML::Parser.new.seekable.append(
|
139
139
|
Node::XML::Scraper.new(type: :animals),
|
140
140
|
Node::XML::Scraper.new(type: :fruits)
|
141
|
-
)
|
142
|
-
)
|
141
|
+
)
|
142
|
+
)
|
143
143
|
|
144
144
|
Dir.glob("#{__dir__}/docs/*") do |file|
|
145
145
|
options = { foo: :bar }
|
@@ -148,9 +148,9 @@ Dir.glob("#{__dir__}/docs/*") do |file|
|
|
148
148
|
end
|
149
149
|
```
|
150
150
|
|
151
|
-
Run `examples/internal-
|
151
|
+
Run `examples/internal-seekable.rb`:
|
152
152
|
```sh
|
153
|
-
% ruby examples/internal-
|
153
|
+
% ruby examples/internal-seekable.rb
|
154
154
|
{"Dog"=>"🐶", "Cat"=>"🐱"}
|
155
155
|
---
|
156
156
|
{"Dog"=>"🐶", "Cat"=>"🐱"}
|
@@ -161,11 +161,11 @@ Run `examples/internal-seek.rb`:
|
|
161
161
|
---
|
162
162
|
```
|
163
163
|
|
164
|
-
#### `CallableTree::Node::Internal#
|
164
|
+
#### `CallableTree::Node::Internal#broadcastable`
|
165
165
|
|
166
166
|
This strategy calls all child nodes of the internal node and ignores their `terminate?` methods, and then outputs their results as array.
|
167
167
|
|
168
|
-
`examples/internal-
|
168
|
+
`examples/internal-broadcastable.rb`:
|
169
169
|
```ruby
|
170
170
|
module Node
|
171
171
|
class LessThan
|
@@ -181,16 +181,16 @@ module Node
|
|
181
181
|
end
|
182
182
|
end
|
183
183
|
|
184
|
-
tree = CallableTree::Node::Root.new.append(
|
185
|
-
Node::LessThan.new(5).append(
|
184
|
+
tree = CallableTree::Node::Root.new.broadcastable.append(
|
185
|
+
Node::LessThan.new(5).broadcastable.append(
|
186
186
|
->(input) { input * 2 }, # anonymous external node
|
187
187
|
->(input) { input + 1 } # anonymous external node
|
188
|
-
)
|
189
|
-
Node::LessThan.new(10).append(
|
188
|
+
),
|
189
|
+
Node::LessThan.new(10).broadcastable.append(
|
190
190
|
->(input) { input * 3 }, # anonymous external node
|
191
191
|
->(input) { input - 1 } # anonymous external node
|
192
|
-
)
|
193
|
-
)
|
192
|
+
)
|
193
|
+
)
|
194
194
|
|
195
195
|
(0..10).each do |input|
|
196
196
|
output = tree.call(input)
|
@@ -199,9 +199,9 @@ end
|
|
199
199
|
|
200
200
|
```
|
201
201
|
|
202
|
-
Run `examples/internal-
|
202
|
+
Run `examples/internal-broadcastable.rb`:
|
203
203
|
```sh
|
204
|
-
% ruby examples/internal-
|
204
|
+
% ruby examples/internal-broadcastable.rb
|
205
205
|
0 -> [[0, 1], [0, -1]]
|
206
206
|
1 -> [[2, 2], [3, 0]]
|
207
207
|
2 -> [[4, 3], [6, 1]]
|
@@ -215,11 +215,11 @@ Run `examples/internal-broadcast.rb`:
|
|
215
215
|
10 -> [nil, nil]
|
216
216
|
```
|
217
217
|
|
218
|
-
#### `CallableTree::Node::Internal#
|
218
|
+
#### `CallableTree::Node::Internal#composable`
|
219
219
|
|
220
220
|
This strategy calls all child nodes of the internal node in order to input the output of the previous node to the next node and ignores their `terminate?` methods, and then outputs a single result.
|
221
221
|
|
222
|
-
`examples/internal-
|
222
|
+
`examples/internal-composable.rb`:
|
223
223
|
```ruby
|
224
224
|
module Node
|
225
225
|
class LessThan
|
@@ -235,16 +235,16 @@ module Node
|
|
235
235
|
end
|
236
236
|
end
|
237
237
|
|
238
|
-
tree = CallableTree::Node::Root.new.append(
|
239
|
-
Node::LessThan.new(5).append(
|
238
|
+
tree = CallableTree::Node::Root.new.composable.append(
|
239
|
+
Node::LessThan.new(5).composable.append(
|
240
240
|
proc { |input| input * 2 }, # anonymous external node
|
241
241
|
proc { |input| input + 1 } # anonymous external node
|
242
|
-
)
|
243
|
-
Node::LessThan.new(10).append(
|
242
|
+
),
|
243
|
+
Node::LessThan.new(10).composable.append(
|
244
244
|
proc { |input| input * 3 }, # anonymous external node
|
245
245
|
proc { |input| input - 1 } # anonymous external node
|
246
|
-
)
|
247
|
-
)
|
246
|
+
)
|
247
|
+
)
|
248
248
|
|
249
249
|
(0..10).each do |input|
|
250
250
|
output = tree.call(input)
|
@@ -253,9 +253,9 @@ end
|
|
253
253
|
|
254
254
|
```
|
255
255
|
|
256
|
-
Run `examples/internal-
|
256
|
+
Run `examples/internal-composable.rb`:
|
257
257
|
```sh
|
258
|
-
% ruby examples/internal-
|
258
|
+
% ruby examples/internal-composable.rb
|
259
259
|
0 -> 2
|
260
260
|
1 -> 8
|
261
261
|
2 -> 14
|
@@ -568,20 +568,20 @@ Run `examples/logging.rb`:
|
|
568
568
|
---
|
569
569
|
```
|
570
570
|
|
571
|
-
#### `CallableTree::Node::Hooks::
|
571
|
+
#### `CallableTree::Node::Hooks::Caller` (experimental)
|
572
572
|
|
573
573
|
`examples/hooks-call.rb`:
|
574
574
|
```ruby
|
575
575
|
module Node
|
576
576
|
class HooksSample
|
577
577
|
include CallableTree::Node::Internal
|
578
|
-
prepend CallableTree::Node::Hooks::
|
578
|
+
prepend CallableTree::Node::Hooks::Caller
|
579
579
|
end
|
580
580
|
end
|
581
581
|
|
582
582
|
Node::HooksSample.new
|
583
|
-
.
|
584
|
-
puts "
|
583
|
+
.before_caller do |input, **_options|
|
584
|
+
puts "before_caller input: #{input}";
|
585
585
|
input + 1
|
586
586
|
end
|
587
587
|
.append(
|
@@ -591,14 +591,14 @@ Node::HooksSample.new
|
|
591
591
|
input * 2
|
592
592
|
end
|
593
593
|
)
|
594
|
-
.
|
595
|
-
puts "
|
594
|
+
.around_caller do |input, **_options, &block|
|
595
|
+
puts "around_caller input: #{input}"
|
596
596
|
output = block.call
|
597
|
-
puts "
|
597
|
+
puts "around_caller output: #{output}"
|
598
598
|
output * input
|
599
599
|
end
|
600
|
-
.
|
601
|
-
puts "
|
600
|
+
.after_caller do |output, **_options|
|
601
|
+
puts "after_caller output: #{output}"
|
602
602
|
output * 2
|
603
603
|
end
|
604
604
|
.tap do |tree|
|
@@ -608,20 +608,20 @@ Node::HooksSample.new
|
|
608
608
|
end
|
609
609
|
```
|
610
610
|
|
611
|
-
Run `examples/hooks-
|
611
|
+
Run `examples/hooks-caller.rb`:
|
612
612
|
```sh
|
613
|
-
% ruby examples/hooks-
|
614
|
-
|
613
|
+
% ruby examples/hooks-caller.rb
|
614
|
+
before_caller input: 1
|
615
615
|
external input: 2
|
616
|
-
|
617
|
-
|
618
|
-
|
616
|
+
around_caller input: 2
|
617
|
+
around_caller output: 4
|
618
|
+
after_caller output: 8
|
619
619
|
result: 16
|
620
620
|
```
|
621
621
|
|
622
622
|
## Contributing
|
623
623
|
|
624
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/jsmmr/
|
624
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jsmmr/ruby_callable_tree.
|
625
625
|
|
626
626
|
## License
|
627
627
|
|
@@ -10,8 +10,8 @@ Root =
|
|
10
10
|
|
11
11
|
Root
|
12
12
|
.new
|
13
|
-
.
|
14
|
-
puts "
|
13
|
+
.before_caller do |input, **_options|
|
14
|
+
puts "before_caller input: #{input}"
|
15
15
|
input + 1
|
16
16
|
end
|
17
17
|
.append(
|
@@ -21,14 +21,14 @@ Root
|
|
21
21
|
input * 2
|
22
22
|
end
|
23
23
|
)
|
24
|
-
.
|
25
|
-
puts "
|
24
|
+
.around_caller do |input, **_options, &block|
|
25
|
+
puts "around_caller input: #{input}"
|
26
26
|
output = block.call
|
27
|
-
puts "
|
27
|
+
puts "around_caller output: #{output}"
|
28
28
|
output * input
|
29
29
|
end
|
30
|
-
.
|
31
|
-
puts "
|
30
|
+
.after_caller do |output, **_options|
|
31
|
+
puts "after_caller output: #{output}"
|
32
32
|
output * 2
|
33
33
|
end
|
34
34
|
.tap do |tree|
|
@@ -55,12 +55,12 @@ Multiply3 =
|
|
55
55
|
.caller(&multiply.call(3))
|
56
56
|
.build
|
57
57
|
|
58
|
-
tree = CallableTree::Node::Root.new.
|
59
|
-
LessThan5.new.
|
58
|
+
tree = CallableTree::Node::Root.new.broadcastable.append(
|
59
|
+
LessThan5.new.broadcastable.append(
|
60
60
|
Multiply2.new,
|
61
61
|
Add1.new
|
62
62
|
),
|
63
|
-
LessThan10.new.
|
63
|
+
LessThan10.new.broadcastable.append(
|
64
64
|
Multiply3.new,
|
65
65
|
Subtract1.new
|
66
66
|
)
|
@@ -55,12 +55,12 @@ Multiply3 =
|
|
55
55
|
.caller(&multiply.call(3))
|
56
56
|
.build
|
57
57
|
|
58
|
-
tree = CallableTree::Node::Root.new.
|
59
|
-
LessThan5.new.
|
58
|
+
tree = CallableTree::Node::Root.new.composable.append(
|
59
|
+
LessThan5.new.composable.append(
|
60
60
|
Multiply2.new,
|
61
61
|
Add1.new
|
62
62
|
),
|
63
|
-
LessThan10.new.
|
63
|
+
LessThan10.new.composable.append(
|
64
64
|
Multiply3.new,
|
65
65
|
Subtract1.new
|
66
66
|
)
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'callable_tree'
|
4
|
+
require 'json'
|
5
|
+
require 'rexml/document'
|
6
|
+
|
7
|
+
JSONParser =
|
8
|
+
CallableTree::Node::Internal::Builder
|
9
|
+
.new
|
10
|
+
.matcher do |input, **_options|
|
11
|
+
File.extname(input) == '.json'
|
12
|
+
end
|
13
|
+
.caller do |input, **options, &block|
|
14
|
+
File.open(input) do |file|
|
15
|
+
json = ::JSON.load(file)
|
16
|
+
# The following block call is equivalent to calling `super` in the class style.
|
17
|
+
block.call(json, **options)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
.terminator do
|
21
|
+
true
|
22
|
+
end
|
23
|
+
.build
|
24
|
+
|
25
|
+
XMLParser =
|
26
|
+
CallableTree::Node::Internal::Builder
|
27
|
+
.new
|
28
|
+
.matcher do |input, **_options|
|
29
|
+
File.extname(input) == '.xml'
|
30
|
+
end
|
31
|
+
.caller do |input, **options, &block|
|
32
|
+
File.open(input) do |file|
|
33
|
+
# The following block call is equivalent to calling `super` in the class style.
|
34
|
+
block.call(REXML::Document.new(file), **options)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
.terminator do
|
38
|
+
true
|
39
|
+
end
|
40
|
+
.build
|
41
|
+
|
42
|
+
def build_json_scraper(type)
|
43
|
+
CallableTree::Node::External::Builder
|
44
|
+
.new
|
45
|
+
.matcher do |input, **_options|
|
46
|
+
!!input[type.to_s]
|
47
|
+
end
|
48
|
+
.caller do |input, **_options|
|
49
|
+
input[type.to_s]
|
50
|
+
.map { |element| [element['name'], element['emoji']] }
|
51
|
+
.to_h
|
52
|
+
end
|
53
|
+
.build
|
54
|
+
end
|
55
|
+
|
56
|
+
AnimalsJSONScraper = build_json_scraper(:animals)
|
57
|
+
FruitsJSONScraper = build_json_scraper(:fruits)
|
58
|
+
|
59
|
+
def build_xml_scraper(type)
|
60
|
+
CallableTree::Node::External::Builder
|
61
|
+
.new
|
62
|
+
.matcher do |input, **_options|
|
63
|
+
!input.get_elements("//#{type}").empty?
|
64
|
+
end
|
65
|
+
.caller do |input, **_options|
|
66
|
+
input
|
67
|
+
.get_elements("//#{type}")
|
68
|
+
.first
|
69
|
+
.map { |element| [element['name'], element['emoji']] }
|
70
|
+
.to_h
|
71
|
+
end
|
72
|
+
.build
|
73
|
+
end
|
74
|
+
|
75
|
+
AnimalsXMLScraper = build_xml_scraper(:animals)
|
76
|
+
FruitsXMLScraper = build_xml_scraper(:fruits)
|
77
|
+
|
78
|
+
tree = CallableTree::Node::Root.new.seekable.append(
|
79
|
+
JSONParser.new.seekable.append(
|
80
|
+
AnimalsJSONScraper.new,
|
81
|
+
FruitsJSONScraper.new
|
82
|
+
),
|
83
|
+
XMLParser.new.seekable.append(
|
84
|
+
AnimalsXMLScraper.new,
|
85
|
+
FruitsXMLScraper.new
|
86
|
+
)
|
87
|
+
)
|
88
|
+
|
89
|
+
Dir.glob("#{__dir__}/../docs/*") do |file|
|
90
|
+
options = { foo: :bar }
|
91
|
+
pp tree.call(file, **options)
|
92
|
+
puts '---'
|
93
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'callable_tree'
|
4
|
+
require 'json'
|
5
|
+
require 'rexml/document'
|
6
|
+
|
7
|
+
JSONParser =
|
8
|
+
CallableTree::Node::Internal::Builder
|
9
|
+
.new
|
10
|
+
.matcher do |input, **_options|
|
11
|
+
File.extname(input) == '.json'
|
12
|
+
end
|
13
|
+
.caller do |input, **options, &block|
|
14
|
+
File.open(input) do |file|
|
15
|
+
json = ::JSON.load(file)
|
16
|
+
# The following block call is equivalent to calling `super` in the class style.
|
17
|
+
block.call(json, **options)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
.terminator do
|
21
|
+
true
|
22
|
+
end
|
23
|
+
.hookable
|
24
|
+
.build
|
25
|
+
|
26
|
+
XMLParser =
|
27
|
+
CallableTree::Node::Internal::Builder
|
28
|
+
.new
|
29
|
+
.matcher do |input, **_options|
|
30
|
+
File.extname(input) == '.xml'
|
31
|
+
end
|
32
|
+
.caller do |input, **options, &block|
|
33
|
+
File.open(input) do |file|
|
34
|
+
# The following block call is equivalent to calling `super` in the class style.
|
35
|
+
block.call(REXML::Document.new(file), **options)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
.terminator do
|
39
|
+
true
|
40
|
+
end
|
41
|
+
.hookable
|
42
|
+
.build
|
43
|
+
|
44
|
+
def build_json_scraper(type)
|
45
|
+
CallableTree::Node::External::Builder
|
46
|
+
.new
|
47
|
+
.matcher do |input, **_options|
|
48
|
+
!!input[type.to_s]
|
49
|
+
end
|
50
|
+
.caller do |input, **_options|
|
51
|
+
input[type.to_s]
|
52
|
+
.map { |element| [element['name'], element['emoji']] }
|
53
|
+
.to_h
|
54
|
+
end
|
55
|
+
.hookable
|
56
|
+
.build
|
57
|
+
end
|
58
|
+
|
59
|
+
AnimalsJSONScraper = build_json_scraper(:animals)
|
60
|
+
FruitsJSONScraper = build_json_scraper(:fruits)
|
61
|
+
|
62
|
+
def build_xml_scraper(type)
|
63
|
+
CallableTree::Node::External::Builder
|
64
|
+
.new
|
65
|
+
.matcher do |input, **_options|
|
66
|
+
!input.get_elements("//#{type}").empty?
|
67
|
+
end
|
68
|
+
.caller do |input, **_options|
|
69
|
+
input
|
70
|
+
.get_elements("//#{type}")
|
71
|
+
.first
|
72
|
+
.map { |element| [element['name'], element['emoji']] }
|
73
|
+
.to_h
|
74
|
+
end
|
75
|
+
.hookable
|
76
|
+
.build
|
77
|
+
end
|
78
|
+
|
79
|
+
AnimalsXMLScraper = build_xml_scraper(:animals)
|
80
|
+
FruitsXMLScraper = build_xml_scraper(:fruits)
|
81
|
+
|
82
|
+
loggable = proc do |node|
|
83
|
+
indent_size = 2
|
84
|
+
blank = ' '
|
85
|
+
list_style = '*'
|
86
|
+
|
87
|
+
node.after_matcher! do |matched, _node_:, **|
|
88
|
+
prefix = list_style.rjust(_node_.depth * indent_size - indent_size + list_style.length, blank)
|
89
|
+
puts "#{prefix} #{_node_.identity}: [matched: #{matched}]"
|
90
|
+
matched
|
91
|
+
end
|
92
|
+
|
93
|
+
if node.external?
|
94
|
+
input_label = 'Input :'
|
95
|
+
output_label = 'Output:'
|
96
|
+
|
97
|
+
node
|
98
|
+
.before_caller! do |input, *, _node_:, **|
|
99
|
+
input_prefix = input_label.rjust(_node_.depth * indent_size + input_label.length, blank)
|
100
|
+
puts "#{input_prefix} #{input}"
|
101
|
+
input
|
102
|
+
end
|
103
|
+
.after_caller! do |output, _node_:, **|
|
104
|
+
output_prefix = output_label.rjust(_node_.depth * indent_size + output_label.length, blank)
|
105
|
+
puts "#{output_prefix} #{output}"
|
106
|
+
output
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
tree = CallableTree::Node::Root.new.seekable.append(
|
112
|
+
JSONParser.new.tap(&loggable).seekable.append(
|
113
|
+
AnimalsJSONScraper.new.tap(&loggable).verbosify,
|
114
|
+
FruitsJSONScraper.new.tap(&loggable).verbosify
|
115
|
+
),
|
116
|
+
XMLParser.new.tap(&loggable).seekable.append(
|
117
|
+
AnimalsXMLScraper.new.tap(&loggable).verbosify,
|
118
|
+
FruitsXMLScraper.new.tap(&loggable).verbosify
|
119
|
+
)
|
120
|
+
)
|
121
|
+
|
122
|
+
Dir.glob("#{__dir__}/../docs/*") do |file|
|
123
|
+
options = { foo: :bar }
|
124
|
+
pp tree.call(file, **options)
|
125
|
+
puts '---'
|
126
|
+
end
|
@@ -5,14 +5,14 @@ require 'callable_tree'
|
|
5
5
|
module Node
|
6
6
|
class HooksSample
|
7
7
|
include CallableTree::Node::Internal
|
8
|
-
prepend CallableTree::Node::Hooks::
|
8
|
+
prepend CallableTree::Node::Hooks::Caller
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
Node::HooksSample
|
13
13
|
.new
|
14
|
-
.
|
15
|
-
puts "
|
14
|
+
.before_caller do |input, **_options|
|
15
|
+
puts "before_caller input: #{input}"
|
16
16
|
input + 1
|
17
17
|
end
|
18
18
|
.append(
|
@@ -22,14 +22,14 @@ Node::HooksSample
|
|
22
22
|
input * 2
|
23
23
|
end
|
24
24
|
)
|
25
|
-
.
|
26
|
-
puts "
|
25
|
+
.around_caller do |input, **_options, &block|
|
26
|
+
puts "around_caller input: #{input}"
|
27
27
|
output = block.call
|
28
|
-
puts "
|
28
|
+
puts "around_caller output: #{output}"
|
29
29
|
output * input
|
30
30
|
end
|
31
|
-
.
|
32
|
-
puts "
|
31
|
+
.after_caller do |output, **_options|
|
32
|
+
puts "after_caller output: #{output}"
|
33
33
|
output * 2
|
34
34
|
end
|
35
35
|
.tap do |tree|
|
@@ -16,16 +16,16 @@ module Node
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
tree = CallableTree::Node::Root.new.append(
|
20
|
-
Node::LessThan.new(5).append(
|
19
|
+
tree = CallableTree::Node::Root.new.broadcastable.append(
|
20
|
+
Node::LessThan.new(5).broadcastable.append(
|
21
21
|
->(input) { input * 2 }, # anonymous external node
|
22
22
|
->(input) { input + 1 } # anonymous external node
|
23
|
-
)
|
24
|
-
Node::LessThan.new(10).append(
|
23
|
+
),
|
24
|
+
Node::LessThan.new(10).broadcastable.append(
|
25
25
|
->(input) { input * 3 }, # anonymous external node
|
26
26
|
->(input) { input - 1 } # anonymous external node
|
27
|
-
)
|
28
|
-
)
|
27
|
+
)
|
28
|
+
)
|
29
29
|
|
30
30
|
(0..10).each do |input|
|
31
31
|
output = tree.call(input)
|
@@ -16,16 +16,16 @@ module Node
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
tree = CallableTree::Node::Root.new.append(
|
20
|
-
Node::LessThan.new(5).append(
|
19
|
+
tree = CallableTree::Node::Root.new.composable.append(
|
20
|
+
Node::LessThan.new(5).composable.append(
|
21
21
|
proc { |input| input * 2 }, # anonymous external node
|
22
22
|
proc { |input| input + 1 } # anonymous external node
|
23
|
-
)
|
24
|
-
Node::LessThan.new(10).append(
|
23
|
+
),
|
24
|
+
Node::LessThan.new(10).composable.append(
|
25
25
|
proc { |input| input * 3 }, # anonymous external node
|
26
26
|
proc { |input| input - 1 } # anonymous external node
|
27
|
-
)
|
28
|
-
)
|
27
|
+
)
|
28
|
+
)
|
29
29
|
|
30
30
|
(0..10).each do |input|
|
31
31
|
output = tree.call(input)
|
@@ -85,12 +85,12 @@ module Node
|
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
|
-
tree = CallableTree::Node::Root.new.append(
|
89
|
-
Node::JSON::Parser.new.append(
|
88
|
+
tree = CallableTree::Node::Root.new.seekable.append(
|
89
|
+
Node::JSON::Parser.new.seekable.append(
|
90
90
|
Node::JSON::Scraper.new(type: :animals),
|
91
91
|
Node::JSON::Scraper.new(type: :fruits)
|
92
92
|
),
|
93
|
-
Node::XML::Parser.new.append(
|
93
|
+
Node::XML::Parser.new.seekable.append(
|
94
94
|
Node::XML::Scraper.new(type: :animals),
|
95
95
|
Node::XML::Scraper.new(type: :fruits)
|
96
96
|
)
|