Module: CallableTree::Node::Internal
- Extended by:
- Forwardable
- Includes:
- CallableTree::Node
- Included in:
- Root
- Defined in:
- lib/callable_tree/node/internal.rb,
lib/callable_tree/node/internal/builder.rb,
lib/callable_tree/node/internal/strategy.rb,
lib/callable_tree/node/internal/strategy/seek.rb,
lib/callable_tree/node/internal/strategy/compose.rb,
lib/callable_tree/node/internal/strategy/broadcast.rb
Defined Under Namespace
Modules: Strategy
Classes: Builder
Instance Attribute Summary
#parent
Class Method Summary
collapse
Instance Method Summary
collapse
#ancestors, #depth, #identity, #root?, #routes, #terminate?
Class Method Details
.included(mod) ⇒ Object
9
10
11
12
13
14
|
# File 'lib/callable_tree/node/internal.rb', line 9
def self.included(mod)
if mod.include?(External)
raise ::CallableTree::Error,
"#{mod} cannot include #{self} together with #{External}"
end
end
|
Instance Method Details
#append(*callables) ⇒ Object
26
27
28
|
# File 'lib/callable_tree/node/internal.rb', line 26
def append(*callables)
clone.append!(*callables)
end
|
#append!(*callables) ⇒ Object
30
31
32
33
34
35
36
|
# File 'lib/callable_tree/node/internal.rb', line 30
def append!(*callables)
callables
.map { |callable| nodeify(callable) }
.tap { |nodes| child_nodes.push(*nodes) }
self
end
|
#broadcast ⇒ Object
Also known as:
broadcastable
116
117
118
119
120
121
122
123
124
|
# File 'lib/callable_tree/node/internal.rb', line 116
def broadcast
if broadcast?
self
else
clone.tap do |node|
node.strategy = Strategy::Broadcast.new
end
end
end
|
#broadcast! ⇒ Object
Also known as:
broadcastable!
126
127
128
129
130
|
# File 'lib/callable_tree/node/internal.rb', line 126
def broadcast!
tap do |node|
node.strategy = Strategy::Broadcast.new unless broadcast?
end
end
|
#broadcast? ⇒ Boolean
Also known as:
broadcastable?
112
113
114
|
# File 'lib/callable_tree/node/internal.rb', line 112
def broadcast?
strategy.is_a?(Strategy::Broadcast)
end
|
#call(*inputs, **options) ⇒ Object
84
85
86
|
# File 'lib/callable_tree/node/internal.rb', line 84
def call(*inputs, **options)
strategy.call(child_nodes, *inputs, **options)
end
|
#children ⇒ Object
18
19
20
|
# File 'lib/callable_tree/node/internal.rb', line 18
def children
[*child_nodes]
end
|
#children! ⇒ Object
22
23
24
|
# File 'lib/callable_tree/node/internal.rb', line 22
def children!
child_nodes
end
|
#compose ⇒ Object
Also known as:
composable
140
141
142
143
144
145
146
147
148
|
# File 'lib/callable_tree/node/internal.rb', line 140
def compose
if compose?
self
else
clone.tap do |node|
node.strategy = Strategy::Compose.new
end
end
end
|
#compose! ⇒ Object
Also known as:
composable!
150
151
152
153
154
|
# File 'lib/callable_tree/node/internal.rb', line 150
def compose!
tap do |node|
node.strategy = Strategy::Compose.new unless compose?
end
end
|
#compose? ⇒ Boolean
Also known as:
composable?
136
137
138
|
# File 'lib/callable_tree/node/internal.rb', line 136
def compose?
strategy.is_a?(Strategy::Compose)
end
|
#external? ⇒ Boolean
170
171
172
|
# File 'lib/callable_tree/node/internal.rb', line 170
def external?
false
end
|
#find(recursive: false, &block) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/callable_tree/node/internal.rb', line 38
def find(recursive: false, &block)
node = child_nodes.find(&block)
return node if node
if recursive
child_nodes
.lazy
.select { |node| node.internal? }
.map { |node| node.find(recursive: true, &block) }
.reject(&:nil?)
.first
end
end
|
#internal? ⇒ Boolean
166
167
168
|
# File 'lib/callable_tree/node/internal.rb', line 166
def internal?
true
end
|
#match? ⇒ Boolean
80
81
82
|
# File 'lib/callable_tree/node/internal.rb', line 80
def match?(*, **)
!child_nodes.empty?
end
|
#outline(&block) ⇒ Object
160
161
162
163
164
|
# File 'lib/callable_tree/node/internal.rb', line 160
def outline(&block)
key = block ? block.call(self) : identity
value = child_nodes.reduce({}) { |memo, node| memo.merge!(node.outline(&block)) }
{ key => value }
end
|
#reject(recursive: false, &block) ⇒ Object
52
53
54
|
# File 'lib/callable_tree/node/internal.rb', line 52
def reject(recursive: false, &block)
clone.reject!(recursive: recursive, &block)
end
|
#reject!(recursive: false, &block) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/callable_tree/node/internal.rb', line 56
def reject!(recursive: false, &block)
child_nodes.reject!(&block)
if recursive
child_nodes.each do |node|
node.reject!(recursive: true, &block) if node.internal?
end
end
self
end
|
#seek ⇒ Object
Also known as:
seekable
92
93
94
95
96
97
98
99
100
|
# File 'lib/callable_tree/node/internal.rb', line 92
def seek
if seek?
self
else
clone.tap do |node|
node.strategy = Strategy::Seek.new
end
end
end
|
#seek! ⇒ Object
Also known as:
seekable!
102
103
104
105
106
|
# File 'lib/callable_tree/node/internal.rb', line 102
def seek!
tap do |node|
node.strategy = Strategy::Seek.new unless seek?
end
end
|
#seek? ⇒ Boolean
Also known as:
seekable?
88
89
90
|
# File 'lib/callable_tree/node/internal.rb', line 88
def seek?
strategy.is_a?(Strategy::Seek)
end
|
#shake(&block) ⇒ Object
68
69
70
|
# File 'lib/callable_tree/node/internal.rb', line 68
def shake(&block)
clone.shake!(&block)
end
|
#shake!(&block) ⇒ Object
72
73
74
75
76
77
78
|
# File 'lib/callable_tree/node/internal.rb', line 72
def shake!(&block)
reject!(&block) if block_given?
reject! do |node|
node.internal? && node.shake!(&block).child_nodes.empty?
end
end
|