Method: Opulent::Compiler#each_node

Defined in:
lib/opulent/compiler/control.rb

#each_node(node, indent) ⇒ Object

Generate the code for a while control structure

Parameters:

  • node (Array)

    Node code generation data

  • indent (Fixnum)

    Size of the indentation to be added



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/opulent/compiler/control.rb', line 127

def each_node(node, indent)
  # Process named variables for each structure
  variables = node[@value][1].clone

  # The each structure accept missing arguments as well, therefore we need to
  # substitute them with our defaults
  #
  # each in iterable
  # each value in iterable
  # each key, value in iterable

  # Value argument name provided only
  if variables.length == 1
    variables.unshift Settings::DEFAULT_EACH_KEY

  # Missing key and value arguments
  elsif variables.empty?
    variables[0] = Settings::DEFAULT_EACH_KEY
    variables[1] = Settings::DEFAULT_EACH_VALUE
  end

  # Choose whether to apply each with index (Arrays) or each (Hashes) methods
  #buffer_eval "_opulent_send_method = (#{node[@value][1]}.is_a?(Array) ? :each_with_index : :each)"
  case node[@value][0][0]
  when '[]'
    buffer_eval "#{node[@value][0][1]}.each_with_index do |#{variables.reverse.join ', '}|"
  else
    buffer_eval "#{node[@value][0][1]}.each do |#{variables.join ', '}|"
  end

  # Evaluate child nodes
  node[@children].each do |child|
    root child, indent
  end

  # End
  buffer_eval 'end'
end