Module: CallableTree::Node::Internal

Extended by:
Forwardable
Includes:
CallableTree::Node, Strategyable
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/strategyable.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, Strategyable Classes: Builder

Constant Summary

Constants included from Strategyable

Strategyable::DEFAUTL_FACTORY

Instance Attribute Summary collapse

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?

Instance Attribute Details

#child_nodesObject



103
104
105
# File 'lib/callable_tree/node/internal.rb', line 103

def child_nodes
  @child_nodes ||= []
end

Class Method Details

.included(mod) ⇒ Object



10
11
12
13
14
15
# File 'lib/callable_tree/node/internal.rb', line 10

def self.included(mod)
  return unless mod.include?(External)

  raise ::CallableTree::Error,
        "#{mod} cannot include #{self} together with #{External}"
end

Instance Method Details

#append(*callables) ⇒ Object



27
28
29
# File 'lib/callable_tree/node/internal.rb', line 27

def append(*callables)
  clone.append!(*callables)
end

#append!(*callables) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/callable_tree/node/internal.rb', line 31

def append!(*callables)
  callables
    .map { |callable| nodeify(callable) }
    .tap { |nodes| child_nodes.push(*nodes) }

  self
end

#call(*inputs, **options) ⇒ Object



85
86
87
# File 'lib/callable_tree/node/internal.rb', line 85

def call(*inputs, **options)
  strategy.call(child_nodes, *inputs, **options)
end

#childrenObject



19
20
21
# File 'lib/callable_tree/node/internal.rb', line 19

def children
  [*child_nodes]
end

#children!Object



23
24
25
# File 'lib/callable_tree/node/internal.rb', line 23

def children!
  child_nodes
end

#external?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/callable_tree/node/internal.rb', line 99

def external?
  false
end

#find(recursive: false, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/callable_tree/node/internal.rb', line 39

def find(recursive: false, &block)
  node = child_nodes.find(&block)
  return node if node

  return unless recursive

  child_nodes
    .lazy
    .select(&:internal?)
    .map { |node| node.find(recursive: true, &block) }
    .reject(&:nil?)
    .first
end

#internal?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/callable_tree/node/internal.rb', line 95

def internal?
  true
end

#match?Boolean

Returns:

  • (Boolean)


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

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

#outline(&block) ⇒ Object



89
90
91
92
93
# File 'lib/callable_tree/node/internal.rb', line 89

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



53
54
55
# File 'lib/callable_tree/node/internal.rb', line 53

def reject(recursive: false, &block)
  clone.reject!(recursive: recursive, &block)
end

#reject!(recursive: false, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/callable_tree/node/internal.rb', line 57

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

#shake(&block) ⇒ Object



69
70
71
# File 'lib/callable_tree/node/internal.rb', line 69

def shake(&block)
  clone.shake!(&block)
end

#shake!(&block) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/callable_tree/node/internal.rb', line 73

def shake!(&block)
  reject!(&block) if block_given?

  reject! do |node|
    node.internal? && node.shake!(&block).child_nodes.empty?
  end
end