Class: Twig::Node::Expression::Array

Inherits:
Base
  • Object
show all
Defined in:
lib/twig/node/expression/array.rb

Instance Attribute Summary

Attributes inherited from Base

#attributes, #lineno, #nodes, #source_context, #tag

Instance Method Summary collapse

Methods inherited from Base

#explicit_parentheses?, #set_explicit_parentheses

Methods inherited from Base

#template_name

Constructor Details

#initialize(elements, lineno) ⇒ Array

Returns a new instance of Array.



7
8
9
10
11
# File 'lib/twig/node/expression/array.rb', line 7

def initialize(elements, lineno)
  super(elements, {}, lineno)

  @index = -1
end

Instance Method Details

#add_element(value, key = nil) ⇒ Object

Parameters:



15
16
17
18
19
20
21
22
# File 'lib/twig/node/expression/array.rb', line 15

def add_element(value, key = nil)
  if key.nil?
    @index += 1
    key = Constant.new(@index, value.lineno)
  end

  nodes.add(key, value)
end

#compile(compiler) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/twig/node/expression/array.rb', line 24

def compile(compiler)
  compiler.
    raw('{').
    indent

  key_value_pairs.each do |key, value|
    compiler.
      subcompile(key).
      raw(' => ').
      subcompile(value).
      raw(', ')
  end

  compiler.
    outdent.
    raw('}')
end