Class: Fear::Extractor::Matcher Abstract Private

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/fear/extractor/matcher.rb,
lib/fear/extractor/matcher/and.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This class is abstract.

abstract matcher to inherit from.

Defined Under Namespace

Classes: And

Instance Method Summary collapse

Constructor Details

#initialize(node:, **attributes) ⇒ Matcher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Matcher.

Parameters:



12
13
14
15
16
# File 'lib/fear/extractor/matcher.rb', line 12

def initialize(node:, **attributes)
  @input = node.input
  @input_position = node.interval.first
  super(attributes)
end

Instance Method Details

#and(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
# File 'lib/fear/extractor/matcher.rb', line 25

def and(other)
  And.new(self, other)
end

#call(arg) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



21
22
23
# File 'lib/fear/extractor/matcher.rb', line 21

def call(arg)
  call_or_else(arg, &PartialFunction::EMPTY)
end

#call_or_else(arg) {|arg| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • arg (any)

Yields:

  • (arg)

    if function not defined



31
32
33
34
35
36
37
# File 'lib/fear/extractor/matcher.rb', line 31

def call_or_else(arg)
  if defined_at?(arg)
    bindings(arg)
  else
    yield arg
  end
end

#failure_reason(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Shows why matcher has failed. Use it for debugging.

Examples:

Fear['[1, 2, _]'].failure_reason([1, 3, 4])
# it will show that the second element hasn't match


44
45
46
47
48
49
50
# File 'lib/fear/extractor/matcher.rb', line 44

def failure_reason(other)
  if defined_at?(other)
    Fear.none
  else
    Fear.some("Expected `#{other.inspect}` to match:\n#{input}\n#{"~" * input_position}^")
  end
end