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:



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

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.



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

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.



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

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



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

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


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

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