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

Constant Summary collapse

EMPTY_HASH =

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

{}.freeze
EMPTY_ARRAY =

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

[].freeze

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:



15
16
17
18
19
# File 'lib/fear/extractor/matcher.rb', line 15

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.



28
29
30
# File 'lib/fear/extractor/matcher.rb', line 28

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.



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

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



34
35
36
37
38
39
40
# File 'lib/fear/extractor/matcher.rb', line 34

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


47
48
49
50
51
52
53
# File 'lib/fear/extractor/matcher.rb', line 47

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