Module: CallableTree::Node::Hooks::Matcher

Included in:
Root
Defined in:
lib/callable_tree/node/hooks/matcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#after_matcher_callbacksObject



84
85
86
# File 'lib/callable_tree/node/hooks/matcher.rb', line 84

def after_matcher_callbacks
  @after_matcher_callbacks ||= []
end

#around_matcher_callbacksObject



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

def around_matcher_callbacks
  @around_matcher_callbacks ||= []
end

#before_matcher_callbacksObject



76
77
78
# File 'lib/callable_tree/node/hooks/matcher.rb', line 76

def before_matcher_callbacks
  @before_matcher_callbacks ||= []
end

Class Method Details

.included(_subclass) ⇒ Object



7
8
9
# File 'lib/callable_tree/node/hooks/matcher.rb', line 7

def self.included(_subclass)
  raise ::CallableTree::Error, "#{self} must be prepended"
end

Instance Method Details

#after_matcher(&block) ⇒ Object



29
30
31
# File 'lib/callable_tree/node/hooks/matcher.rb', line 29

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

#after_matcher!(&block) ⇒ Object



33
34
35
36
# File 'lib/callable_tree/node/hooks/matcher.rb', line 33

def after_matcher!(&block)
  after_matcher_callbacks << block
  self
end

#around_matcher(&block) ⇒ Object



20
21
22
# File 'lib/callable_tree/node/hooks/matcher.rb', line 20

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

#around_matcher!(&block) ⇒ Object



24
25
26
27
# File 'lib/callable_tree/node/hooks/matcher.rb', line 24

def around_matcher!(&block)
  around_matcher_callbacks << block
  self
end

#before_matcher(&block) ⇒ Object



11
12
13
# File 'lib/callable_tree/node/hooks/matcher.rb', line 11

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

#before_matcher!(&block) ⇒ Object



15
16
17
18
# File 'lib/callable_tree/node/hooks/matcher.rb', line 15

def before_matcher!(&block)
  before_matcher_callbacks << block
  self
end

#match?(*inputs, **options) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/callable_tree/node/hooks/matcher.rb', line 38

def match?(*inputs, **options)
  input_head, *input_tail = inputs

  input_head = before_matcher_callbacks.reduce(input_head) do |input_head, callable|
    callable.call(input_head, *input_tail, **options, _node_: self)
  end

  matched =
    if around_matcher_callbacks.empty?
      super(input_head, *input_tail, **options)
    else
      around_matcher_callbacks_head, *around_matcher_callbacks_tail = around_matcher_callbacks
      matcher = proc { super(input_head, *input_tail, **options) }

      matched =
        around_matcher_callbacks_head
        .call(
          input_head,
          *input_tail,
          **options,
          _node_: self
        ) { matcher.call }

      around_matcher_callbacks_tail.reduce(matched) do |matched, callable|
        callable.call(
          input_head,
          *input_tail,
          **options,
          _node_: self
        ) { matched }
      end
    end

  after_matcher_callbacks.reduce(matched) do |matched, callable|
    callable.call(matched, **options, _node_: self)
  end
end