callable_tree 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/Gemfile.lock +1 -1
- data/README.md +136 -113
- data/callable_tree.gemspec +2 -2
- data/examples/{example2.rb → external-verbosify.rb} +0 -0
- data/examples/{example5.rb → hooks-call.rb} +0 -0
- data/examples/{example3.rb → identity.rb} +0 -0
- data/examples/internal-broadcast.rb +31 -0
- data/examples/{example7.rb → internal-compose.rb} +0 -0
- data/examples/{example1.rb → internal-seek.rb} +0 -0
- data/examples/{example4.rb → logging.rb} +0 -0
- data/lib/callable_tree/node/external.rb +14 -7
- data/lib/callable_tree/node/external/verbose.rb +4 -0
- data/lib/callable_tree/version.rb +1 -1
- metadata +16 -16
- data/examples/example6.rb +0 -101
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8fa06f3d5119313cc62468deec9349bb902f92ff6267f9b0051d5625eea8a866
|
4
|
+
data.tar.gz: 8679ef5bd22e419d6379b18fdc0a509074938073c9edaff37b4a7bee6029cda8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ccdf3baffda301532c51510b84d066cec428961f773618ddc8d344917c9af26ca3da3d6186d6748585a8eceb0eabac0d7e652bcbcafb28b3a4a9e74622eb98f
|
7
|
+
data.tar.gz: e1aa253c87040bebc9f03080ad25f704c1cbde0410355b4ffb70bbe969338d3ec7a88804abe856d582b9e351a182db43a7abe4b2f0b89c3f7d97fc6bb64bf1eb
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -18,19 +18,22 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
+
Builds a tree by linking instances of the nodes. The `call` method of the node where the `match?` method returns a truthy value is called in a chain from the root node to the leaf node.
|
22
|
+
|
21
23
|
- `CallableTree::Node::Internal`
|
22
|
-
- This `module` is used to define a node that can have child nodes.
|
24
|
+
- This `module` is used to define a node that can have child nodes. An instance of this node has several strategies. The strategy can be changed by calling the method of the instance.
|
23
25
|
- `CallableTree::Node::External`
|
24
26
|
- This `module` is used to define a leaf node that cannot have child nodes.
|
25
27
|
- `CallableTree::Node::Root`
|
26
28
|
- This `class` includes `CallableTree::Node::Internal`. When there is no need to customize the internal node, use this `class`.
|
27
29
|
|
28
|
-
Builds a tree by linking instances of the nodes. The `call` method of the node where the `match?` method returns a truthy value is called in a chain from the root node to the leaf node.
|
29
|
-
If the `call` method returns a value other than `nil`, the next sibling node does not be called. This behavior is changeable by overriding the `terminate?` method.
|
30
|
-
|
31
30
|
### Basic
|
32
31
|
|
33
|
-
`
|
32
|
+
#### `CallableTree::Node::Internal#seek` (default)
|
33
|
+
|
34
|
+
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.
|
35
|
+
|
36
|
+
`examples/internal-seek.rb`:
|
34
37
|
```ruby
|
35
38
|
module Node
|
36
39
|
module JSON
|
@@ -123,16 +126,17 @@ module Node
|
|
123
126
|
end
|
124
127
|
end
|
125
128
|
|
129
|
+
# The `seek` method call can be omitted since it is the default strategy.
|
126
130
|
tree = CallableTree::Node::Root.new.append(
|
127
131
|
Node::JSON::Parser.new.append(
|
128
132
|
Node::JSON::Scraper.new(type: :animals),
|
129
133
|
Node::JSON::Scraper.new(type: :fruits)
|
130
|
-
),
|
134
|
+
),#.seek,
|
131
135
|
Node::XML::Parser.new.append(
|
132
136
|
Node::XML::Scraper.new(type: :animals),
|
133
137
|
Node::XML::Scraper.new(type: :fruits)
|
134
|
-
)
|
135
|
-
)
|
138
|
+
)#.seek
|
139
|
+
)#.seek
|
136
140
|
|
137
141
|
Dir.glob(__dir__ + '/docs/*') do |file|
|
138
142
|
options = { foo: :bar }
|
@@ -141,9 +145,9 @@ Dir.glob(__dir__ + '/docs/*') do |file|
|
|
141
145
|
end
|
142
146
|
```
|
143
147
|
|
144
|
-
Run `examples/
|
148
|
+
Run `examples/internal-seek.rb`:
|
145
149
|
```sh
|
146
|
-
% ruby examples/
|
150
|
+
% ruby examples/internal-seek.rb
|
147
151
|
{"Dog"=>"🐶", "Cat"=>"🐱"}
|
148
152
|
---
|
149
153
|
{"Dog"=>"🐶", "Cat"=>"🐱"}
|
@@ -154,13 +158,121 @@ Run `examples/example1.rb`:
|
|
154
158
|
---
|
155
159
|
```
|
156
160
|
|
161
|
+
#### `CallableTree::Node::Internal#broadcast` (experimental)
|
162
|
+
|
163
|
+
This strategy calls all child nodes of the internal node and ignores their `terminate?` methods, and then outputs their results as array.
|
164
|
+
|
165
|
+
`examples/internal-broadcast.rb`:
|
166
|
+
```ruby
|
167
|
+
module Node
|
168
|
+
class LessThan
|
169
|
+
include CallableTree::Node::Internal
|
170
|
+
|
171
|
+
def initialize(num)
|
172
|
+
@num = num
|
173
|
+
end
|
174
|
+
|
175
|
+
def match?(input)
|
176
|
+
super && input < @num
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
tree = CallableTree::Node::Root.new.append(
|
182
|
+
Node::LessThan.new(5).append(
|
183
|
+
lambda { |input, **| input * 2 }, # anonymous external node
|
184
|
+
lambda { |input, **| input + 1 } # anonymous external node
|
185
|
+
).broadcast,
|
186
|
+
Node::LessThan.new(10).append(
|
187
|
+
lambda { |input, **| input * 3 }, # anonymous external node
|
188
|
+
lambda { |input, **| input - 1 } # anonymous external node
|
189
|
+
).broadcast
|
190
|
+
).broadcast
|
191
|
+
|
192
|
+
(0..10).each do |input|
|
193
|
+
output = tree.call(input)
|
194
|
+
puts "#{input} -> #{output}"
|
195
|
+
end
|
196
|
+
|
197
|
+
```
|
198
|
+
|
199
|
+
Run `examples/internal-broadcast.rb`:
|
200
|
+
```sh
|
201
|
+
% ruby examples/internal-broadcast.rb
|
202
|
+
0 -> [[0, 1], [0, -1]]
|
203
|
+
1 -> [[2, 2], [3, 0]]
|
204
|
+
2 -> [[4, 3], [6, 1]]
|
205
|
+
3 -> [[6, 4], [9, 2]]
|
206
|
+
4 -> [[8, 5], [12, 3]]
|
207
|
+
5 -> [nil, [15, 4]]
|
208
|
+
6 -> [nil, [18, 5]]
|
209
|
+
7 -> [nil, [21, 6]]
|
210
|
+
8 -> [nil, [24, 7]]
|
211
|
+
9 -> [nil, [27, 8]]
|
212
|
+
10 -> [nil, nil]
|
213
|
+
```
|
214
|
+
|
215
|
+
#### `CallableTree::Node::Internal#compose` (experimental)
|
216
|
+
|
217
|
+
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.
|
218
|
+
|
219
|
+
`examples/internal-compose.rb`:
|
220
|
+
```ruby
|
221
|
+
module Node
|
222
|
+
class LessThan
|
223
|
+
include CallableTree::Node::Internal
|
224
|
+
|
225
|
+
def initialize(num)
|
226
|
+
@num = num
|
227
|
+
end
|
228
|
+
|
229
|
+
def match?(input)
|
230
|
+
super && input < @num
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
tree = CallableTree::Node::Root.new.append(
|
236
|
+
Node::LessThan.new(5).append(
|
237
|
+
proc { |input| input * 2 }, # anonymous external node
|
238
|
+
proc { |input| input + 1 } # anonymous external node
|
239
|
+
).compose,
|
240
|
+
Node::LessThan.new(10).append(
|
241
|
+
proc { |input| input * 3 }, # anonymous external node
|
242
|
+
proc { |input| input - 1 } # anonymous external node
|
243
|
+
).compose
|
244
|
+
).compose
|
245
|
+
|
246
|
+
(0..10).each do |input|
|
247
|
+
output = tree.call(input)
|
248
|
+
puts "#{input} -> #{output}"
|
249
|
+
end
|
250
|
+
|
251
|
+
```
|
252
|
+
|
253
|
+
Run `examples/internal-compose.rb`:
|
254
|
+
```sh
|
255
|
+
% ruby examples/internal-compose.rb
|
256
|
+
0 -> 2
|
257
|
+
1 -> 8
|
258
|
+
2 -> 14
|
259
|
+
3 -> 20
|
260
|
+
4 -> 26
|
261
|
+
5 -> 14
|
262
|
+
6 -> 17
|
263
|
+
7 -> 20
|
264
|
+
8 -> 23
|
265
|
+
9 -> 26
|
266
|
+
10 -> 10
|
267
|
+
```
|
268
|
+
|
157
269
|
### Advanced
|
158
270
|
|
159
271
|
#### `CallableTree::Node::External#verbosify`
|
160
272
|
|
161
|
-
If you want verbose
|
273
|
+
If you want verbose output results, call this method.
|
162
274
|
|
163
|
-
`examples/
|
275
|
+
`examples/external-verbosify.rb`:
|
164
276
|
```ruby
|
165
277
|
...
|
166
278
|
|
@@ -178,9 +290,9 @@ tree = CallableTree::Node::Root.new.append(
|
|
178
290
|
...
|
179
291
|
```
|
180
292
|
|
181
|
-
Run `examples/
|
293
|
+
Run `examples/external-verbosify.rb`:
|
182
294
|
```sh
|
183
|
-
% ruby examples/
|
295
|
+
% ruby examples/external-verbosify.rb
|
184
296
|
#<struct CallableTree::Node::External::Output
|
185
297
|
value={"Dog"=>"🐶", "Cat"=>"🐱"},
|
186
298
|
options={:foo=>:bar},
|
@@ -208,9 +320,9 @@ You can work around it by overriding the `identity` method of the node.
|
|
208
320
|
|
209
321
|
#### `CallableTree::Node#identity`
|
210
322
|
|
211
|
-
If you want to customize the node identity, override
|
323
|
+
If you want to customize the node identity, override this method.
|
212
324
|
|
213
|
-
`examples/
|
325
|
+
`examples/identity.rb`:
|
214
326
|
```ruby
|
215
327
|
module Node
|
216
328
|
class Identity
|
@@ -266,9 +378,9 @@ end
|
|
266
378
|
...
|
267
379
|
```
|
268
380
|
|
269
|
-
Run `examples/
|
381
|
+
Run `examples/identity.rb`:
|
270
382
|
```sh
|
271
|
-
% ruby examples/
|
383
|
+
% ruby examples/identity.rb
|
272
384
|
#<struct CallableTree::Node::External::Output
|
273
385
|
value={"Dog"=>"🐶", "Cat"=>"🐱"},
|
274
386
|
options={:foo=>:bar},
|
@@ -315,7 +427,7 @@ Run `examples/example3.rb`:
|
|
315
427
|
|
316
428
|
This is an example of logging.
|
317
429
|
|
318
|
-
`examples/
|
430
|
+
`examples/logging.rb`:
|
319
431
|
```ruby
|
320
432
|
module Node
|
321
433
|
module Logging
|
@@ -388,9 +500,9 @@ end
|
|
388
500
|
...
|
389
501
|
```
|
390
502
|
|
391
|
-
Run `examples/
|
503
|
+
Run `examples/logging.rb`:
|
392
504
|
```sh
|
393
|
-
% ruby examples/
|
505
|
+
% ruby examples/logging.rb
|
394
506
|
* Node::JSON::Parser: [matched: true]
|
395
507
|
* Node::JSON::Scraper(animals): [matched: true]
|
396
508
|
Input : {"animals"=>[{"name"=>"Dog", "emoji"=>"🐶"}, {"name"=>"Cat", "emoji"=>"🐱"}]}
|
@@ -455,7 +567,7 @@ Run `examples/example4.rb`:
|
|
455
567
|
|
456
568
|
#### `CallableTree::Node::Hooks::Call` (experimental)
|
457
569
|
|
458
|
-
`examples/
|
570
|
+
`examples/hooks-call.rb`:
|
459
571
|
```ruby
|
460
572
|
module Node
|
461
573
|
class HooksSample
|
@@ -493,9 +605,9 @@ Node::HooksSample.new
|
|
493
605
|
end
|
494
606
|
```
|
495
607
|
|
496
|
-
Run `examples/
|
608
|
+
Run `examples/hooks-call.rb`:
|
497
609
|
```sh
|
498
|
-
% ruby examples/
|
610
|
+
% ruby examples/hooks-call.rb
|
499
611
|
before_call input: 1
|
500
612
|
external input: 2
|
501
613
|
around_call input: 2
|
@@ -504,95 +616,6 @@ after_call output: 8
|
|
504
616
|
result: 16
|
505
617
|
```
|
506
618
|
|
507
|
-
#### `CallableTree::Node::Internal#broadcast` (experimental)
|
508
|
-
|
509
|
-
If you want to call all child nodes of the internal node in order to output their results as array, call it. The `broadcast` strategy ignores the `terminate?` method of the nodes.
|
510
|
-
|
511
|
-
`examples/example6.rb`:
|
512
|
-
```ruby
|
513
|
-
...
|
514
|
-
|
515
|
-
tree = CallableTree::Node::Root.new.append(
|
516
|
-
Node::JSON::Parser.new.append(
|
517
|
-
Node::JSON::Scraper.new(type: :animals),
|
518
|
-
Node::JSON::Scraper.new(type: :fruits)
|
519
|
-
).broadcast,
|
520
|
-
Node::XML::Parser.new.append(
|
521
|
-
Node::XML::Scraper.new(type: :animals),
|
522
|
-
Node::XML::Scraper.new(type: :fruits)
|
523
|
-
).broadcast
|
524
|
-
)
|
525
|
-
|
526
|
-
...
|
527
|
-
```
|
528
|
-
|
529
|
-
Run `examples/example6.rb`:
|
530
|
-
```sh
|
531
|
-
% ruby examples/example6.rb
|
532
|
-
[{"Dog"=>"🐶", "Cat"=>"🐱"}, nil]
|
533
|
-
---
|
534
|
-
[{"Dog"=>"🐶", "Cat"=>"🐱"}, nil]
|
535
|
-
---
|
536
|
-
[nil, {"Red Apple"=>"🍎", "Green Apple"=>"🍏"}]
|
537
|
-
---
|
538
|
-
[nil, {"Red Apple"=>"🍎", "Green Apple"=>"🍏"}]
|
539
|
-
---
|
540
|
-
```
|
541
|
-
|
542
|
-
#### `CallableTree::Node::Internal#compose` (experimental)
|
543
|
-
|
544
|
-
If you want to call all child nodes of the internal node in order to input the output of the previous node to the next node and output a single result , call it. The `compose` strategy ignores the `terminate?` method of the nodes.
|
545
|
-
|
546
|
-
`examples/example7.rb`:
|
547
|
-
```ruby
|
548
|
-
module Node
|
549
|
-
class LessThan
|
550
|
-
include CallableTree::Node::Internal
|
551
|
-
|
552
|
-
def initialize(num)
|
553
|
-
@num = num
|
554
|
-
end
|
555
|
-
|
556
|
-
def match?(input)
|
557
|
-
super && input < @num
|
558
|
-
end
|
559
|
-
end
|
560
|
-
end
|
561
|
-
|
562
|
-
tree = CallableTree::Node::Root.new.append(
|
563
|
-
Node::LessThan.new(5).append(
|
564
|
-
proc { |input| input * 2 }, # anonymous external node
|
565
|
-
proc { |input| input + 1 } # anonymous external node
|
566
|
-
).compose,
|
567
|
-
Node::LessThan.new(10).append(
|
568
|
-
proc { |input| input * 3 }, # anonymous external node
|
569
|
-
proc { |input| input - 1 } # anonymous external node
|
570
|
-
).compose
|
571
|
-
).compose
|
572
|
-
|
573
|
-
(0..10).each do |input|
|
574
|
-
output = tree.call(input)
|
575
|
-
puts "#{input} -> #{output}"
|
576
|
-
end
|
577
|
-
|
578
|
-
```
|
579
|
-
|
580
|
-
Run `examples/example7.rb`:
|
581
|
-
```sh
|
582
|
-
% ruby examples/example7.rb
|
583
|
-
0 -> 2
|
584
|
-
1 -> 8
|
585
|
-
2 -> 14
|
586
|
-
3 -> 20
|
587
|
-
4 -> 26
|
588
|
-
5 -> 14
|
589
|
-
6 -> 17
|
590
|
-
7 -> 20
|
591
|
-
8 -> 23
|
592
|
-
9 -> 26
|
593
|
-
10 -> 10
|
594
|
-
```
|
595
|
-
|
596
619
|
## Contributing
|
597
620
|
|
598
621
|
Bug reports and pull requests are welcome on GitHub at https://github.com/jsmmr/callable_tree.
|
data/callable_tree.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ['jsmmr']
|
9
9
|
spec.email = ['jsmmr@icloud.com']
|
10
10
|
|
11
|
-
spec.summary = 'Builds a tree by linking callable nodes. The nodes that match the
|
12
|
-
spec.description = 'Builds a tree by linking callable nodes. The nodes that match the
|
11
|
+
spec.summary = 'Builds a tree by linking callable nodes. The nodes that match the conditions are called in a chain from the root node to the leaf node. These are like nested `if` or `case` expressions.'
|
12
|
+
spec.description = 'Builds a tree by linking callable nodes. The nodes that match the conditiosn are called in a chain from the root node to the leaf node. These are like nested `if` or `case` expressions.'
|
13
13
|
spec.homepage = 'https://github.com/jsmmr/ruby_callable_tree'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'callable_tree'
|
2
|
+
|
3
|
+
module Node
|
4
|
+
class LessThan
|
5
|
+
include CallableTree::Node::Internal
|
6
|
+
|
7
|
+
def initialize(num)
|
8
|
+
@num = num
|
9
|
+
end
|
10
|
+
|
11
|
+
def match?(input)
|
12
|
+
super && input < @num
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
tree = CallableTree::Node::Root.new.append(
|
18
|
+
Node::LessThan.new(5).append(
|
19
|
+
lambda { |input, **| input * 2 }, # anonymous external node
|
20
|
+
lambda { |input, **| input + 1 } # anonymous external node
|
21
|
+
).broadcast,
|
22
|
+
Node::LessThan.new(10).append(
|
23
|
+
lambda { |input, **| input * 3 }, # anonymous external node
|
24
|
+
lambda { |input, **| input - 1 } # anonymous external node
|
25
|
+
).broadcast
|
26
|
+
).broadcast
|
27
|
+
|
28
|
+
(0..10).each do |input|
|
29
|
+
output = tree.call(input)
|
30
|
+
puts "#{input} -> #{output}"
|
31
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
@@ -9,12 +9,12 @@ module CallableTree
|
|
9
9
|
Proxy.new(callable)
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
|
12
|
+
def proxified?
|
13
|
+
false
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
|
16
|
+
def verbosified?
|
17
|
+
false
|
18
18
|
end
|
19
19
|
|
20
20
|
def verbosify
|
@@ -23,8 +23,8 @@ module CallableTree
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def identity
|
26
|
-
if
|
27
|
-
|
26
|
+
if proxified?
|
27
|
+
unproxify
|
28
28
|
else
|
29
29
|
self
|
30
30
|
end
|
@@ -36,11 +36,18 @@ module CallableTree
|
|
36
36
|
include External
|
37
37
|
|
38
38
|
def_delegators :@callable, :call
|
39
|
-
attr_reader :callable
|
40
39
|
|
41
40
|
def initialize(callable)
|
42
41
|
@callable = callable
|
43
42
|
end
|
43
|
+
|
44
|
+
def proxified?
|
45
|
+
true
|
46
|
+
end
|
47
|
+
|
48
|
+
def unproxify
|
49
|
+
@callable
|
50
|
+
end
|
44
51
|
end
|
45
52
|
|
46
53
|
private_constant :Proxy
|
metadata
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: callable_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jsmmr
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-06-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: Builds a tree by linking callable nodes. The nodes that match the
|
14
|
-
|
15
|
-
|
13
|
+
description: Builds a tree by linking callable nodes. The nodes that match the conditiosn
|
14
|
+
are called in a chain from the root node to the leaf node. These are like nested
|
15
|
+
`if` or `case` expressions.
|
16
16
|
email:
|
17
17
|
- jsmmr@icloud.com
|
18
18
|
executables: []
|
@@ -36,13 +36,13 @@ files:
|
|
36
36
|
- examples/docs/animals.xml
|
37
37
|
- examples/docs/fruits.json
|
38
38
|
- examples/docs/fruits.xml
|
39
|
-
- examples/
|
40
|
-
- examples/
|
41
|
-
- examples/
|
42
|
-
- examples/
|
43
|
-
- examples/
|
44
|
-
- examples/
|
45
|
-
- examples/
|
39
|
+
- examples/external-verbosify.rb
|
40
|
+
- examples/hooks-call.rb
|
41
|
+
- examples/identity.rb
|
42
|
+
- examples/internal-broadcast.rb
|
43
|
+
- examples/internal-compose.rb
|
44
|
+
- examples/internal-seek.rb
|
45
|
+
- examples/logging.rb
|
46
46
|
- lib/callable_tree.rb
|
47
47
|
- lib/callable_tree/node.rb
|
48
48
|
- lib/callable_tree/node/external.rb
|
@@ -60,7 +60,7 @@ licenses:
|
|
60
60
|
metadata:
|
61
61
|
homepage_uri: https://github.com/jsmmr/ruby_callable_tree
|
62
62
|
source_code_uri: https://github.com/jsmmr/ruby_callable_tree
|
63
|
-
changelog_uri: https://github.com/jsmmr/ruby_callable_tree/blob/v0.1.
|
63
|
+
changelog_uri: https://github.com/jsmmr/ruby_callable_tree/blob/v0.1.3/CHANGELOG.md
|
64
64
|
post_install_message:
|
65
65
|
rdoc_options: []
|
66
66
|
require_paths:
|
@@ -79,7 +79,7 @@ requirements: []
|
|
79
79
|
rubygems_version: 3.2.16
|
80
80
|
signing_key:
|
81
81
|
specification_version: 4
|
82
|
-
summary: Builds a tree by linking callable nodes. The nodes that match the
|
83
|
-
|
84
|
-
|
82
|
+
summary: Builds a tree by linking callable nodes. The nodes that match the conditions
|
83
|
+
are called in a chain from the root node to the leaf node. These are like nested
|
84
|
+
`if` or `case` expressions.
|
85
85
|
test_files: []
|
data/examples/example6.rb
DELETED
@@ -1,101 +0,0 @@
|
|
1
|
-
require 'callable_tree'
|
2
|
-
require 'json'
|
3
|
-
require 'rexml/document'
|
4
|
-
|
5
|
-
module Node
|
6
|
-
module JSON
|
7
|
-
class Parser
|
8
|
-
include CallableTree::Node::Internal
|
9
|
-
|
10
|
-
def match?(input, **options)
|
11
|
-
File.extname(input) == '.json'
|
12
|
-
end
|
13
|
-
|
14
|
-
def call(input, **options)
|
15
|
-
File.open(input) do |file|
|
16
|
-
json = ::JSON.load(file)
|
17
|
-
super(json, **options)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def terminate?(_output, **)
|
22
|
-
true
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
class Scraper
|
27
|
-
include CallableTree::Node::External
|
28
|
-
|
29
|
-
def initialize(type:)
|
30
|
-
@type = type
|
31
|
-
end
|
32
|
-
|
33
|
-
def match?(input, **options)
|
34
|
-
!!input[@type.to_s]
|
35
|
-
end
|
36
|
-
|
37
|
-
def call(input, **options)
|
38
|
-
input[@type.to_s]
|
39
|
-
.map { |element| [element['name'], element['emoji']] }
|
40
|
-
.to_h
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
module XML
|
46
|
-
class Parser
|
47
|
-
include CallableTree::Node::Internal
|
48
|
-
|
49
|
-
def match?(input, **options)
|
50
|
-
File.extname(input) == '.xml'
|
51
|
-
end
|
52
|
-
|
53
|
-
def call(input, **options)
|
54
|
-
File.open(input) do |file|
|
55
|
-
super(REXML::Document.new(file), **options)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def terminate?(_output, **)
|
60
|
-
true
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
class Scraper
|
65
|
-
include CallableTree::Node::External
|
66
|
-
|
67
|
-
def initialize(type:)
|
68
|
-
@type = type
|
69
|
-
end
|
70
|
-
|
71
|
-
def match?(input, **options)
|
72
|
-
!input.get_elements("//#{@type}").empty?
|
73
|
-
end
|
74
|
-
|
75
|
-
def call(input, **options)
|
76
|
-
input
|
77
|
-
.get_elements("//#{@type}")
|
78
|
-
.first
|
79
|
-
.map { |element| [element['name'], element['emoji']] }
|
80
|
-
.to_h
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
tree = CallableTree::Node::Root.new.append(
|
87
|
-
Node::JSON::Parser.new.append(
|
88
|
-
Node::JSON::Scraper.new(type: :animals),
|
89
|
-
Node::JSON::Scraper.new(type: :fruits)
|
90
|
-
).broadcast,
|
91
|
-
Node::XML::Parser.new.append(
|
92
|
-
Node::XML::Scraper.new(type: :animals),
|
93
|
-
Node::XML::Scraper.new(type: :fruits)
|
94
|
-
).broadcast
|
95
|
-
)
|
96
|
-
|
97
|
-
Dir.glob(__dir__ + '/docs/*') do |file|
|
98
|
-
options = { foo: :bar }
|
99
|
-
pp tree.call(file, **options)
|
100
|
-
puts '---'
|
101
|
-
end
|