Class: Sablon::Statement::Condition

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



28
29
30
# File 'lib/sablon/operations.rb', line 28

def block
  @block
end

#conditon_exprObject

Returns the value of attribute conditon_expr

Returns:

  • (Object)

    the current value of conditon_expr



28
29
30
# File 'lib/sablon/operations.rb', line 28

def conditon_expr
  @conditon_expr
end

#predicateObject

Returns the value of attribute predicate

Returns:

  • (Object)

    the current value of predicate



28
29
30
# File 'lib/sablon/operations.rb', line 28

def predicate
  @predicate
end

Instance Method Details

#evaluate(context) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/sablon/operations.rb', line 29

def evaluate(context)
  value = conditon_expr.evaluate(context)
  if truthy?(predicate ? value.public_send(predicate) : value)
    block.replace(block.process(context).reverse)
  else
    block.replace([])
  end
end

#truthy?(value) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/sablon/operations.rb', line 38

def truthy?(value)
  case value
  when Array;
    !value.empty?
  else
    !!value
  end
end