Class: Sablon::Statement::Condition

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

Instance Method Summary collapse

Constructor Details

#initialize(conditions) ⇒ Condition

Returns a new instance of Condition.



57
58
59
60
61
62
63
64
# File 'lib/sablon/operations.rb', line 57

def initialize(conditions)
  @conditions = conditions
  @else_block = nil
  return unless @conditions.last[:block].start_field.expression =~ /:else/
  #
  # store the else block separately because it is always "true"
  @else_block = @conditions.pop[:block]
end

Instance Method Details

#evaluate(env) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sablon/operations.rb', line 66

def evaluate(env)
  #
  # process conditional blocks, if and elsif(s)
  any_true = eval_conditional_blocks(env)
  #
  # clear the blocks for any remaining conditions
  @conditions.map { |cond| cond[:block].replace([]) }
  return unless @else_block
  #
  # apply the else clause if none of the conditions were true
  if any_true
    @else_block.replace([])
  elsif @else_block
    @else_block.replace(@else_block.process(env).reverse)
  end
end