Class: Gitlab::TemplateParser::AST::If

Inherits:
Struct
  • Object
show all
Defined in:
lib/gitlab/template_parser/ast.rb

Overview

An ‘if` expression, with an optional `else` clause.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#conditionObject

Returns the value of attribute condition

Returns:

  • (Object)

    the current value of condition



91
92
93
# File 'lib/gitlab/template_parser/ast.rb', line 91

def condition
  @condition
end

#false_bodyObject

Returns the value of attribute false_body

Returns:

  • (Object)

    the current value of false_body



91
92
93
# File 'lib/gitlab/template_parser/ast.rb', line 91

def false_body
  @false_body
end

#true_bodyObject

Returns the value of attribute true_body

Returns:

  • (Object)

    the current value of true_body



91
92
93
# File 'lib/gitlab/template_parser/ast.rb', line 91

def true_body
  @true_body
end

Instance Method Details

#evaluate(state, data) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/gitlab/template_parser/ast.rb', line 92

def evaluate(state, data)
  result =
    if truthy?(condition.evaluate(state, data))
      true_body.evaluate(state, data)
    elsif false_body
      false_body.evaluate(state, data)
    end

  result.to_s
end

#truthy?(value) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
107
# File 'lib/gitlab/template_parser/ast.rb', line 103

def truthy?(value)
  # We treat empty collections and such as false, removing the need for
  # some sort of `if length(x) > 0` expression.
  value.respond_to?(:empty?) ? !value.empty? : !!value
end