Class: NodeQuery::Compiler::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/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: '=', adapter:) ⇒ Attribute

Initialize a Attribute.



11
12
13
14
15
16
# File 'lib/node_query/compiler/attribute.rb', line 11

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

Instance Method Details

#match?(node, base_node) ⇒ Boolean

Check if the node matches the attribute.



22
23
24
# File 'lib/node_query/compiler/attribute.rb', line 22

def match?(node, base_node)
  node && @value.match?(NodeQuery::Helper.get_target_node(node, @key, @adapter), base_node, @operator)
end

#to_sObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/node_query/compiler/attribute.rb', line 26

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}"
  when 'not_includes'
    "#{@key} not includes #{@value}"
  else
    "#{@key}=#{@value}"
  end
end