Class: Querly::Pattern::Expr::Constant

Inherits:
Base
  • Object
show all
Defined in:
lib/querly/pattern/expr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, #=~, #attributes

Constructor Details

#initialize(path:) ⇒ Constant

Returns a new instance of Constant.



45
46
47
# File 'lib/querly/pattern/expr.rb', line 45

def initialize(path:)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



43
44
45
# File 'lib/querly/pattern/expr.rb', line 43

def path
  @path
end

Instance Method Details

#test_constant(node, path) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/querly/pattern/expr.rb', line 57

def test_constant(node, path)
  if node
    case node.type
    when :const
      parent = node.children[0]
      name = node.children[1]

      if name == path.last
        path.count == 1 || test_constant(parent, path.take(path.count - 1))
      end
    when :cbase
      path.empty?
    end
  else
    path.empty?
  end
end

#test_node(node) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/querly/pattern/expr.rb', line 49

def test_node(node)
  if path
    test_constant node, path
  else
    node&.type == :const
  end
end