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:) ⇒ Regexp

Initialize a Regexp.

Parameters:

  • value (Regexp)

    the regexp value



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

def initialize(value:)
  @value = value
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.



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

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

#to_sObject



35
36
37
# File 'lib/node_query/compiler/regexp.rb', line 35

def to_s
  @value.to_s
end

#valid_operatorsArray

Get valid operators.

Returns:

  • (Array)

    valid operators



31
32
33
# File 'lib/node_query/compiler/regexp.rb', line 31

def valid_operators
  REGEXP_VALID_OPERATORS
end