Class: Sablon::Statement::Loop

Inherits:
Struct
  • Object
show all
Defined in:
lib/sablon/operations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



14
15
16
# File 'lib/sablon/operations.rb', line 14

def block
  @block
end

#iterator_nameObject

Returns the value of attribute iterator_name

Returns:

  • (Object)

    the current value of iterator_name



14
15
16
# File 'lib/sablon/operations.rb', line 14

def iterator_name
  @iterator_name
end

#list_exprObject

Returns the value of attribute list_expr

Returns:

  • (Object)

    the current value of list_expr



14
15
16
# File 'lib/sablon/operations.rb', line 14

def list_expr
  @list_expr
end

Instance Method Details

#evaluate(env) ⇒ Object

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sablon/operations.rb', line 15

def evaluate(env)
  value = list_expr.evaluate(env.context)
  value = value.to_ary if value.respond_to?(:to_ary)
  raise ContextError, "The expression #{list_expr.inspect} should evaluate to an enumerable but was: #{value.inspect}" unless value.is_a?(Enumerable)

  content = value.flat_map do |item|
    iter_env = env.alter_context(iterator_name => item)
    block.process(iter_env)
  end
  update_unique_ids(env, content)
  block.replace(content.reverse)
end