Class: Sablon::Processor::Document::OperationConstruction

Inherits:
Object
  • Object
show all
Defined in:
lib/sablon/processor/document.rb

Instance Method Summary collapse

Constructor Details

#initialize(fields) ⇒ OperationConstruction

Returns a new instance of OperationConstruction.



145
146
147
148
# File 'lib/sablon/processor/document.rb', line 145

def initialize(fields)
  @fields = fields
  @operations = []
end

Instance Method Details

#consume(allow_insertion) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/sablon/processor/document.rb', line 157

def consume(allow_insertion)
  @field = @fields.shift
  return unless @field
  case @field.expression
  when /^=/
    if allow_insertion
      Statement::Insertion.new(Expression.parse(@field.expression[1..-1]), @field)
    end
  when /([^ ]+):each\(([^ ]+)\)/
    block = consume_block("#{$1}:endEach")
    Statement::Loop.new(Expression.parse($1), $2, block)
  when /([^ ]+):if\(([^)]+)\)/
    block = consume_block("#{$1}:endIf")
    Statement::Condition.new(Expression.parse($1), block, $2)
  when /([^ ]+):if/
    block = consume_block("#{$1}:endIf")
    Statement::Condition.new(Expression.parse($1), block)
  when /comment/
    block = consume_block("endComment")
    Statement::Comment.new(block)
  end
end

#consume_block(end_expression) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/sablon/processor/document.rb', line 180

def consume_block(end_expression)
  start_field = end_field = @field
  while end_field && end_field.expression != end_expression
    consume(false)
    end_field = @field
  end

  if end_field
    Block.enclosed_by start_field, end_field
  else
    raise TemplateError, "Could not find end field for «#{start_field.expression}». Was looking for «#{end_expression}»"
  end
end

#operationsObject



150
151
152
153
154
155
# File 'lib/sablon/processor/document.rb', line 150

def operations
  while @fields.any?
    @operations << consume(true)
  end
  @operations.compact
end