Class: Cel::Literal

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/cel/ast/elements.rb

Direct Known Subclasses

Bool, Bytes, Duration, List, Map, Null, Number, String, Timestamp

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value) ⇒ Literal

Returns a new instance of Literal.



149
150
151
152
153
154
# File 'lib/cel/ast/elements.rb', line 149

def initialize(type, value)
  @type = type.is_a?(Type) ? type : TYPES[type]
  @value = value
  super(value)
  check
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



147
148
149
# File 'lib/cel/ast/elements.rb', line 147

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



147
148
149
# File 'lib/cel/ast/elements.rb', line 147

def value
  @value
end

Class Method Details

.to_cel_type(val) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/cel/ast/elements.rb', line 161

def self.to_cel_type(val)
  val = Protobuf.convert_from_protobuf(val) if val.is_a?(Google::Protobuf::MessageExts)

  case val
  when Literal, Identifier
    val
    # TODO: should support byte streams?
  when ::String
    String.new(val)
  when ::Symbol
    Identifier.new(val)
  when ::Integer
    Number.new(:int, val)
  when ::Float, ::BigDecimal
    Number.new(:double, val)
  when ::Hash
    Map.new(val)
  when ::Array
    List.new(val)
  when true, false
    Bool.new(val)
  when nil
    Null.new
  when Time
    Timestamp.new(val)
  else
    raise BindingError, "can't convert #{val} to CEL type"
  end
end

Instance Method Details

#==(other) ⇒ Object



156
157
158
159
# File 'lib/cel/ast/elements.rb', line 156

def ==(other)
  other = other.value if other.is_a?(Literal)
  @value == other || super
end

#to_ruby_typeObject



191
192
193
# File 'lib/cel/ast/elements.rb', line 191

def to_ruby_type
  @value
end