Class: NodeQuery::Compiler::Regexp

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/node_query/compiler/regexp.rb

Overview

Regexp represents a ruby regexp value.

Constant Summary

Constants included from Comparable

Comparable::ARRAY_VALID_OPERATORS, Comparable::NUMBER_VALID_OPERATORS, Comparable::REGEXP_VALID_OPERATORS, Comparable::SIMPLE_VALID_OPERATORS, Comparable::STRING_VALID_OPERATORS

Instance Method Summary collapse

Methods included from Comparable

#actual_value, #expected_value, #is_equal?, #valid_operator?

Constructor Details

#initialize(value:, adapter:) ⇒ Regexp

Initialize a Regexp.

Parameters:



11
12
13
14
# File 'lib/node_query/compiler/regexp.rb', line 11

def initialize(value:, adapter:)
  @value = value
  @adapter = adapter
end

Instance Method Details

#match?(node, _base_node, operator = '=~') ⇒ Boolean

Check if the regexp value matches the node value.

Parameters:

  • node (Node)

    the node

  • _base_node (Node)

    the base node for evaluated value

  • operator (String) (defaults to: '=~')

    the operator

Returns:

  • (Boolean)

    true if the regexp value matches the node value, otherwise, false.



21
22
23
24
25
26
27
28
29
# File 'lib/node_query/compiler/regexp.rb', line 21

def match?(node, _base_node, operator = '=~')
  match =
    if @adapter.is_node?(node)
      @value.match(@adapter.get_source(node))
    else
      @value.match(node.to_s)
    end
  operator == '=~' ? match : !match
end

#to_sObject



37
38
39
# File 'lib/node_query/compiler/regexp.rb', line 37

def to_s
  @value.to_s
end

#valid_operatorsArray

Get valid operators.

Returns:

  • (Array)

    valid operators



33
34
35
# File 'lib/node_query/compiler/regexp.rb', line 33

def valid_operators
  REGEXP_VALID_OPERATORS
end