Class: Synvert::Core::NodeQuery::Compiler::Regexp

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/synvert/core/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, #valid_operator?

Constructor Details

#initialize(value:) ⇒ Regexp

Initialize a Regexp.

Parameters:

  • value (Regexp)

    the regexp value



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

def initialize(value:)
  @value = value
end

Instance Method Details

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

Check if the regexp value matches the node value.

Parameters:

Returns:

  • (Boolean)

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



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

def match?(node, operator = '=~')
  match =
    if node.is_a?(::Parser::AST::Node)
      @value.match(node.to_source)
    else
      @value.match(node.to_s)
    end
  operator == '=~' ? match : !match
end

#to_sObject



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

def to_s
  @value.to_s
end

#valid_operatorsObject

Get valid operators.



29
30
31
# File 'lib/synvert/core/node_query/compiler/regexp.rb', line 29

def valid_operators
  REGEXP_VALID_OPERATORS
end