Class: Twig::Node::Expression::Hash
- Includes:
- SupportDefinedTest
- Defined in:
- lib/twig/node/expression/hash.rb
Instance Attribute Summary
Attributes inherited from Base
#attributes, #lineno, #nodes, #source_context, #tag
Instance Method Summary collapse
- #add_element(value, key = nil) ⇒ Object
- #compile(compiler) ⇒ Object
-
#initialize(elements, lineno) ⇒ Hash
constructor
A new instance of Hash.
- #key_value_pairs ⇒ Object
Methods included from SupportDefinedTest
#define_test_enabled?, #enable_defined_test
Methods inherited from Base
#explicit_parentheses?, #set_explicit_parentheses
Methods inherited from Base
#empty?, #length, #template_name, #to_s
Constructor Details
#initialize(elements, lineno) ⇒ Hash
Returns a new instance of Hash.
9 10 11 12 13 |
# File 'lib/twig/node/expression/hash.rb', line 9 def initialize(elements, lineno) super(elements, {}, lineno) @index = -1 end |
Instance Method Details
#add_element(value, key = nil) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/twig/node/expression/hash.rb', line 17 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
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/twig/node/expression/hash.rb', line 26 def compile(compiler) if define_test_enabled? return compiler.repr(true) end compiler. raw('{'). indent first = true key_value_pairs.each do |key, value| unless first compiler.raw(', ') end first = false unless value.is_a?(Expression::Unary::HashSpread) case key when Variable::Context key = Unary::StringCast.new(key, key.lineno) when Variable::Local key_value = key.attributes[:name] key = Constant.new(key_value, key.lineno) when Constant key.attributes[:value] end compiler. subcompile(key). raw(' => ') end compiler. subcompile(value) end compiler. outdent. raw('}') end |
#key_value_pairs ⇒ Object
69 70 71 |
# File 'lib/twig/node/expression/hash.rb', line 69 def key_value_pairs nodes.each_value.each_slice(2) end |