Class: Cel::Map

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

Instance Attribute Summary

Attributes inherited from Literal

#type, #value

Instance Method Summary collapse

Methods inherited from Literal

to_cel_type

Constructor Details

#initialize(value) ⇒ Map

Returns a new instance of Map.



318
319
320
321
322
323
324
# File 'lib/cel/ast/elements.rb', line 318

def initialize(value)
  value = value.to_h do |k, v|
    [Literal.to_cel_type(k), Literal.to_cel_type(v)]
  end
  super(MapType.new(value), value)
  check
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



345
346
347
348
349
350
351
# File 'lib/cel/ast/elements.rb', line 345

def method_missing(meth, *args)
  return super unless @value

  key = @value.keys.find { |k| k.to_s == meth.to_s } or return super

  @value[key]
end

Instance Method Details

#==(other) ⇒ Object



326
327
328
329
330
331
# File 'lib/cel/ast/elements.rb', line 326

def ==(other)
  super || (
    other.respond_to?(:to_hash) &&
    @value.zip(other).all? { |(x1, y1), (x2, y2)| x1 == x2 && y1 == y2 }
  )
end

#respond_to_missing?(meth, *args) ⇒ Boolean

Returns:

  • (Boolean)


341
342
343
# File 'lib/cel/ast/elements.rb', line 341

def respond_to_missing?(meth, *args)
  super || (@value && @value.keys.any? { |k| k.to_s == meth.to_s })
end

#to_aryObject



333
334
335
# File 'lib/cel/ast/elements.rb', line 333

def to_ary
  [self]
end

#to_ruby_typeObject



337
338
339
# File 'lib/cel/ast/elements.rb', line 337

def to_ruby_type
  value.to_h { |*args| args.map(&:to_ruby_type) }
end