Class: Cel::List

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

Instance Attribute Summary collapse

Attributes inherited from Literal

#type, #value

Instance Method Summary collapse

Methods inherited from Literal

to_cel_type

Methods included from CelMethods

included

Constructor Details

#initialize(value, depth: 1) ⇒ List

Returns a new instance of List.



7
8
9
10
11
12
13
# File 'lib/cel/ast/elements/list.rb', line 7

def initialize(value, depth: 1)
  value = value.map do |v|
    Literal.to_cel_type(v)
  end
  super(TYPES[:list], value)
  @depth = depth
end

Instance Attribute Details

#depthObject (readonly)

Returns the value of attribute depth.



5
6
7
# File 'lib/cel/ast/elements/list.rb', line 5

def depth
  @depth
end

Instance Method Details

#by_max_depthObject



44
45
46
47
48
49
50
51
# File 'lib/cel/ast/elements/list.rb', line 44

def by_max_depth
  max = @value.max { |a1, a2| calc_depth(a1, 0) <=> calc_depth(a2, 0) }

  # return the last value if all options have the same depth
  return @value.last if (max == @value.first) && (calc_depth(max, 0) == calc_depth(@value.last, 0))

  max
end

#to_aryObject



28
29
30
# File 'lib/cel/ast/elements/list.rb', line 28

def to_ary
  [self]
end

#to_ruby_typeObject



32
33
34
# File 'lib/cel/ast/elements/list.rb', line 32

def to_ruby_type
  value.map(&:to_ruby_type)
end

#to_sObject



53
54
55
# File 'lib/cel/ast/elements/list.rb', line 53

def to_s
  "[#{@value.map(&:to_s).join(", ")}]"
end