Class: Liquid2::ArrayLiteral

Inherits:
Expression show all
Defined in:
lib/liquid2/expressions/array.rb

Overview

An array literal.

Instance Attribute Summary

Attributes inherited from Expression

#token

Instance Method Summary collapse

Methods inherited from Expression

#scope

Constructor Details

#initialize(token, items) ⇒ ArrayLiteral

Returns a new instance of ArrayLiteral.

Parameters:

  • token ([Symbol, String?, Integer])
  • items (Array<Expression>)


10
11
12
13
# File 'lib/liquid2/expressions/array.rb', line 10

def initialize(token, items)
  super(token)
  @items = items
end

Instance Method Details

#childrenObject



43
# File 'lib/liquid2/expressions/array.rb', line 43

def children = @items

#evaluate(context) ⇒ untyped

Parameters:

Returns:

  • (untyped)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/liquid2/expressions/array.rb', line 17

def evaluate(context)
  result = [] # : Array[untyped]
  @items.each do |item|
    value = context.evaluate(item)
    if item.is_a?(ArraySpread)
      case value
      when Array
        result.concat(value)
      when Hash, String
        result << value
      when Enumerable
        result.concat(value.to_a)
      else
        if value.respond_to?(:each)
          result.concat(value.each)
        else
          result << value
        end
      end
    else
      result << value
    end
  end
  result
end