Class: Querly::Pattern::Expr::Literal

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(type:, values: nil) ⇒ Literal

Returns a new instance of Literal.



86
87
88
89
# File 'lib/querly/pattern/expr.rb', line 86

def initialize(type:, values: nil)
  @type = type
  @values = values ? Array(values) : nil
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



83
84
85
# File 'lib/querly/pattern/expr.rb', line 83

def type
  @type
end

#valuesObject (readonly)

Returns the value of attribute values.



84
85
86
# File 'lib/querly/pattern/expr.rb', line 84

def values
  @values
end

Instance Method Details

#test_node(node) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/querly/pattern/expr.rb', line 103

def test_node(node)
  case node&.type
  when :int
    return false unless type == :int || type == :number
    test_value(node.children.first)

  when :float
    return false unless type == :float || type == :number
    test_value(node.children.first)

  when :true
    type == :bool && (values == nil || values == [true])

  when :false
    type == :bool && (values == nil || values == [false])

  when :str
    return false unless type == :string
    test_value(node.children.first)

  when :sym
    return false unless type == :symbol
    test_value(node.children.first)

  when :regexp
    return false unless type == :regexp
    test_value(node.children.first)

  end
end

#test_value(object) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/querly/pattern/expr.rb', line 95

def test_value(object)
  if values
    values.any? {|value| value === object }
  else
    true
  end
end

#with_values(values) ⇒ Object



91
92
93
# File 'lib/querly/pattern/expr.rb', line 91

def with_values(values)
  self.class.new(type: type, values: values)
end