callable_tree 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -2
- data/Gemfile.lock +1 -1
- data/README.md +32 -32
- 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-seek.rb → internal-seekable.rb} +5 -5
- 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 +12 -6
- data/lib/callable_tree/node/external/builder.rb +1 -1
- data/lib/callable_tree/node/internal/builder.rb +1 -1
- data/lib/callable_tree/node/internal.rb +12 -0
- data/lib/callable_tree/version.rb +1 -1
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83716fbdef2881db33e8f046f056a17d8a8866423951493902088bf1af53218f
|
4
|
+
data.tar.gz: 8906217b71482e1717798329d172640a50f7cc73613a2187beba0702bf8b8d74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e5ece858eb3347c41ea5f573e37c972d1a127d9fa234cd2512b18ef9e882b33d0da900b46c8f8e539fcee36fccb6643d21bb0945169019e7734232907c412c1
|
7
|
+
data.tar.gz: ce58af4b926c9201f4133f02da848dcfff7962f9fa08588c1d4b4b61765b9c5ffaf03f2cfb4367da7d14a80bc1dcd55ea4b53de39d4075be25ef6e2abaf9ae7e
|
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,24 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.3.5] - 2022-03-20
|
4
|
+
|
5
|
+
- Add `CallableTree::Node::Internal#seekable?` as an alias for `CallableTree::Node::Internal#seek?`.
|
6
|
+
- Add `CallableTree::Node::Internal#seekable` as an alias for `CallableTree::Node::Internal#seek`.
|
7
|
+
- Add `CallableTree::Node::Internal#seekable!` as an alias for `CallableTree::Node::Internal#seek!`.
|
8
|
+
- Add `CallableTree::Node::Internal#broadcastable?` as an alias for `CallableTree::Node::Internal#broadcast?`.
|
9
|
+
- Add `CallableTree::Node::Internal#broadcastable` as an alias for `CallableTree::Node::Internal#broadcast`.
|
10
|
+
- Add `CallableTree::Node::Internal#broadcastable!` as an alias for `CallableTree::Node::Internal#broadcast!`.
|
11
|
+
- Add `CallableTree::Node::Internal#composable?` as an alias for `CallableTree::Node::Internal#compose?`.
|
12
|
+
- Add `CallableTree::Node::Internal#composable` as an alias for `CallableTree::Node::Internal#compose`.
|
13
|
+
- Add `CallableTree::Node::Internal#composable!` as an alias for `CallableTree::Node::Internal#compose!`.
|
14
|
+
- (Experimental) Add `CallableTree::Node::Internal::Builder#terminator` to use instead of `CallableTree::Node::Internal::Builder#terminater`.
|
15
|
+
See `examples/builder/*.rb` for details.
|
16
|
+
|
3
17
|
## [0.3.4] - 2022-03-13
|
4
18
|
|
5
|
-
- Add `CallableTree::Node::Internal::Builder`.
|
19
|
+
- (Experimental) Add `CallableTree::Node::Internal::Builder`.
|
6
20
|
See `examples/builder/*.rb` for details.
|
7
|
-
- Add `CallableTree::Node::External::Builder`.
|
21
|
+
- (Experimental) Add `CallableTree::Node::External::Builder`.
|
8
22
|
See `examples/builder/*.rb` for details.
|
9
23
|
|
10
24
|
## [0.3.3] - 2022-02-19
|
data/Gemfile.lock
CHANGED
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
|
@@ -621,7 +621,7 @@ result: 16
|
|
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
|
|
@@ -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
|
)
|
@@ -18,7 +18,7 @@ module JSONParser
|
|
18
18
|
block.call(json, **options)
|
19
19
|
end
|
20
20
|
end
|
21
|
-
.
|
21
|
+
.terminator do
|
22
22
|
true
|
23
23
|
end
|
24
24
|
.build
|
@@ -38,7 +38,7 @@ module XMLParser
|
|
38
38
|
block.call(REXML::Document.new(file), **options)
|
39
39
|
end
|
40
40
|
end
|
41
|
-
.
|
41
|
+
.terminator do
|
42
42
|
true
|
43
43
|
end
|
44
44
|
.build
|
@@ -79,12 +79,12 @@ module XMLScraper
|
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
|
-
tree = CallableTree::Node::Root.new.append(
|
83
|
-
JSONParser.build.new.append(
|
82
|
+
tree = CallableTree::Node::Root.new.seekable.append(
|
83
|
+
JSONParser.build.new.seekable.append(
|
84
84
|
JSONScraper.build(:animals).new,
|
85
85
|
JSONScraper.build(:fruits).new
|
86
86
|
),
|
87
|
-
XMLParser.build.new.append(
|
87
|
+
XMLParser.build.new.seekable.append(
|
88
88
|
XMLScraper.build(:animals).new,
|
89
89
|
XMLScraper.build(:fruits).new
|
90
90
|
)
|
@@ -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
|
)
|
@@ -14,7 +14,13 @@ module CallableTree
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def terminater(&block)
|
17
|
-
|
17
|
+
warn 'Use CallableTree::Node::Internal::Builder#terminator instead.'
|
18
|
+
@terminator = block
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def terminator(&block)
|
23
|
+
@terminator = block
|
18
24
|
self
|
19
25
|
end
|
20
26
|
|
@@ -26,13 +32,13 @@ module CallableTree
|
|
26
32
|
def build(node_type:)
|
27
33
|
matcher = @matcher
|
28
34
|
caller = @caller
|
29
|
-
|
35
|
+
terminator = @terminator
|
30
36
|
hookable = @hookable
|
31
37
|
|
32
38
|
validate(
|
33
39
|
matcher: matcher,
|
34
40
|
caller: caller,
|
35
|
-
|
41
|
+
terminator: terminator
|
36
42
|
)
|
37
43
|
|
38
44
|
::Class
|
@@ -56,9 +62,9 @@ module CallableTree
|
|
56
62
|
end
|
57
63
|
end
|
58
64
|
|
59
|
-
if
|
65
|
+
if terminator
|
60
66
|
define_method(:terminate?) do |output, *inputs, **options|
|
61
|
-
|
67
|
+
terminator.call(output, *inputs, **options) do |output, *inputs, **options|
|
62
68
|
super(output, *inputs, **options)
|
63
69
|
end
|
64
70
|
end
|
@@ -68,7 +74,7 @@ module CallableTree
|
|
68
74
|
|
69
75
|
private
|
70
76
|
|
71
|
-
def validate(matcher:, caller:,
|
77
|
+
def validate(matcher:, caller:, terminator:)
|
72
78
|
raise ::CallableTree::Error, 'Not implemented'
|
73
79
|
end
|
74
80
|
end
|
@@ -98,6 +98,10 @@ module CallableTree
|
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
+
alias_method :seekable?, :seek?
|
102
|
+
alias_method :seekable, :seek
|
103
|
+
alias_method :seekable!, :seek!
|
104
|
+
|
101
105
|
def broadcast?
|
102
106
|
strategy.is_a?(Strategy::Broadcast)
|
103
107
|
end
|
@@ -118,6 +122,10 @@ module CallableTree
|
|
118
122
|
end
|
119
123
|
end
|
120
124
|
|
125
|
+
alias_method :broadcastable?, :broadcast?
|
126
|
+
alias_method :broadcastable, :broadcast
|
127
|
+
alias_method :broadcastable!, :broadcast!
|
128
|
+
|
121
129
|
def compose?
|
122
130
|
strategy.is_a?(Strategy::Compose)
|
123
131
|
end
|
@@ -138,6 +146,10 @@ module CallableTree
|
|
138
146
|
end
|
139
147
|
end
|
140
148
|
|
149
|
+
alias_method :composable?, :compose?
|
150
|
+
alias_method :composable, :compose
|
151
|
+
alias_method :composable!, :compose!
|
152
|
+
|
141
153
|
def outline(&block)
|
142
154
|
key = block ? block.call(self) : identity
|
143
155
|
value = child_nodes.reduce({}) { |memo, node| memo.merge!(node.outline(&block)) }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: callable_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jsmmr
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-03-
|
11
|
+
date: 2022-03-20 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Builds a tree by linking callable nodes. The nodes that match the conditions
|
14
14
|
are called in a chain from the root node to the leaf node. These are like nested
|
@@ -34,9 +34,9 @@ files:
|
|
34
34
|
- bin/setup
|
35
35
|
- callable_tree.gemspec
|
36
36
|
- examples/builder/hooks-call.rb
|
37
|
-
- examples/builder/internal-
|
38
|
-
- examples/builder/internal-
|
39
|
-
- examples/builder/internal-
|
37
|
+
- examples/builder/internal-broadcastable.rb
|
38
|
+
- examples/builder/internal-composable.rb
|
39
|
+
- examples/builder/internal-seekable.rb
|
40
40
|
- examples/docs/animals.json
|
41
41
|
- examples/docs/animals.xml
|
42
42
|
- examples/docs/fruits.json
|
@@ -44,9 +44,9 @@ files:
|
|
44
44
|
- examples/external-verbosify.rb
|
45
45
|
- examples/hooks-call.rb
|
46
46
|
- examples/identity.rb
|
47
|
-
- examples/internal-
|
48
|
-
- examples/internal-
|
49
|
-
- examples/internal-
|
47
|
+
- examples/internal-broadcastable.rb
|
48
|
+
- examples/internal-composable.rb
|
49
|
+
- examples/internal-seekable.rb
|
50
50
|
- examples/logging.rb
|
51
51
|
- lib/callable_tree.rb
|
52
52
|
- lib/callable_tree/node.rb
|
@@ -69,7 +69,7 @@ licenses:
|
|
69
69
|
metadata:
|
70
70
|
homepage_uri: https://github.com/jsmmr/ruby_callable_tree
|
71
71
|
source_code_uri: https://github.com/jsmmr/ruby_callable_tree
|
72
|
-
changelog_uri: https://github.com/jsmmr/ruby_callable_tree/blob/v0.3.
|
72
|
+
changelog_uri: https://github.com/jsmmr/ruby_callable_tree/blob/v0.3.5/CHANGELOG.md
|
73
73
|
post_install_message:
|
74
74
|
rdoc_options: []
|
75
75
|
require_paths:
|