Module: CallableTree::Node::Builder

Included in:
External::Builder, Internal::Builder
Defined in:
lib/callable_tree/node/builder.rb

Instance Method Summary collapse

Instance Method Details

#build(node_type:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/callable_tree/node/builder.rb', line 37

def build(node_type:)
  matcher = @matcher
  caller = @caller
  terminator = @terminator
  identifier = @identifier
  hookable = @hookable

  validate(
    matcher: matcher,
    caller: caller,
    terminator: terminator
  )

  ::Class
    .new do
      include node_type
      if hookable
        prepend Hooks::Matcher
        prepend Hooks::Caller
        prepend Hooks::Terminator
      end

      if matcher
        define_method(:match?) do |*inputs, **options|
          matcher.call(*inputs, **options, _node_: self) do |*inputs, **options|
            super(*inputs, **options)
          end
        end
      end

      if caller
        define_method(:call) do |*inputs, **options|
          caller.call(*inputs, **options, _node_: self) do |*inputs, **options|
            super(*inputs, **options)
          end
        end
      end

      if terminator
        define_method(:terminate?) do |output, *inputs, **options|
          terminator.call(output, *inputs, **options, _node_: self) do |output, *inputs, **options|
            super(output, *inputs, **options)
          end
        end
      end

      if identifier
        define_method(:identity) do
          identifier.call(_node_: self) { super() }
        end
      end
    end
end

#caller(&block) ⇒ Object



11
12
13
14
# File 'lib/callable_tree/node/builder.rb', line 11

def caller(&block)
  @caller = block
  self
end

#hookable(hookable = true) ⇒ Object



32
33
34
35
# File 'lib/callable_tree/node/builder.rb', line 32

def hookable(hookable = true)
  @hookable = hookable
  self
end

#identifier(&block) ⇒ Object



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

def identifier(&block)
  @identifier = block
  self
end

#matcher(&block) ⇒ Object



6
7
8
9
# File 'lib/callable_tree/node/builder.rb', line 6

def matcher(&block)
  @matcher = block
  self
end

#terminater(&block) ⇒ Object



16
17
18
19
20
# File 'lib/callable_tree/node/builder.rb', line 16

def terminater(&block)
  warn 'Use CallableTree::Node::Internal::Builder#terminator instead.'
  @terminator = block
  self
end

#terminator(&block) ⇒ Object



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

def terminator(&block)
  @terminator = block
  self
end