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

Attributes included from CallableTree::Node

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CallableTree::Node

#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(terminable: false) ⇒ Object Also known as: broadcastable



114
115
116
117
118
119
120
# File 'lib/callable_tree/node/internal.rb', line 114

def broadcast(terminable: false)
  if strategy == Strategy::Broadcast.new(terminable: terminable)
    self
  else
    clone.broadcast!(terminable: terminable)
  end
end

#broadcast!(terminable: false) ⇒ Object Also known as: broadcastable!



122
123
124
125
126
# File 'lib/callable_tree/node/internal.rb', line 122

def broadcast!(terminable: false)
  self.strategy = Strategy::Broadcast.new(terminable: terminable)

  self
end

#broadcast?Boolean Also known as: broadcastable?

Returns:

  • (Boolean)


110
111
112
# File 'lib/callable_tree/node/internal.rb', line 110

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

#childrenObject



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(terminable: false) ⇒ Object Also known as: composable



136
137
138
139
140
141
142
# File 'lib/callable_tree/node/internal.rb', line 136

def compose(terminable: false)
  if strategy == Strategy::Compose.new(terminable: terminable)
    self
  else
    clone.compose!(terminable: terminable)
  end
end

#compose!(terminable: false) ⇒ Object Also known as: composable!



144
145
146
147
148
# File 'lib/callable_tree/node/internal.rb', line 144

def compose!(terminable: false)
  self.strategy = Strategy::Compose.new(terminable: terminable)

  self
end

#compose?Boolean Also known as: composable?

Returns:

  • (Boolean)


132
133
134
# File 'lib/callable_tree/node/internal.rb', line 132

def compose?
  strategy.is_a?(Strategy::Compose)
end

#external?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/callable_tree/node/internal.rb', line 164

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(&:internal?)
      .map { |node| node.find(recursive: true, &block) }
      .reject(&:nil?)
      .first
  end
end

#internal?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/callable_tree/node/internal.rb', line 160

def internal?
  true
end

#match?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/callable_tree/node/internal.rb', line 80

def match?(*, **)
  !child_nodes.empty?
end

#outline(&block) ⇒ Object



154
155
156
157
158
# File 'lib/callable_tree/node/internal.rb', line 154

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(terminable: true) ⇒ Object Also known as: seekable



92
93
94
95
96
97
98
# File 'lib/callable_tree/node/internal.rb', line 92

def seek(terminable: true)
  if strategy == Strategy::Seek.new(terminable: terminable)
    self
  else
    clone.seek!(terminable: terminable)
  end
end

#seek!(terminable: true) ⇒ Object Also known as: seekable!



100
101
102
103
104
# File 'lib/callable_tree/node/internal.rb', line 100

def seek!(terminable: true)
  self.strategy = Strategy::Seek.new(terminable: terminable)

  self
end

#seek?Boolean Also known as: seekable?

Returns:

  • (Boolean)


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