Class: Synvert::Core::NodeQuery::Compiler::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/synvert/core/node_query/compiler/attribute.rb

Overview

Attribute is a pair of key, value and operator,

Instance Method Summary collapse

Constructor Details

#initialize(key:, value:, operator: '==') ⇒ Attribute

Initialize a Attribute.

Parameters:

  • key (String)

    the key

  • value

    the value can be any class implement Comparable

  • operator (Symbol) (defaults to: '==')

    the operator



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

def initialize(key:, value:, operator: '==')
  @key = key
  @value = value
  @operator = operator
end

Instance Method Details

#match?(node) ⇒ Boolean

Check if the node matches the attribute.

Parameters:

Returns:



19
20
21
22
# File 'lib/synvert/core/node_query/compiler/attribute.rb', line 19

def match?(node)
  @value.base_node = node if @value.is_a?(DynamicAttribute)
  node && @value.match?(node.child_node_by_name(@key), @operator)
end

#to_sObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/synvert/core/node_query/compiler/attribute.rb', line 24

def to_s
  case @operator
  when '^=', '$=', '*=', '!=', '=~', '!~', '>=', '>', '<=', '<'
    "#{@key}#{@operator}#{@value}"
  when 'in'
    "#{@key} in (#{@value})"
  when 'not_in'
    "#{@key} not in (#{@value})"
  when 'includes'
    "#{@key} includes #{@value}"
  else
    "#{@key}=#{@value}"
  end
end