Class: Gitlab::TemplateParser::AST::If
- Inherits:
-
Struct
- Object
- Struct
- Gitlab::TemplateParser::AST::If
- Defined in:
- lib/gitlab/template_parser/ast.rb
Overview
An ‘if` expression, with an optional `else` clause.
Instance Attribute Summary collapse
-
#condition ⇒ Object
Returns the value of attribute condition.
-
#false_body ⇒ Object
Returns the value of attribute false_body.
-
#true_body ⇒ Object
Returns the value of attribute true_body.
Instance Method Summary collapse
Instance Attribute Details
#condition ⇒ Object
Returns the value of attribute condition
91 92 93 |
# File 'lib/gitlab/template_parser/ast.rb', line 91 def condition @condition end |
#false_body ⇒ Object
Returns the value of attribute false_body
91 92 93 |
# File 'lib/gitlab/template_parser/ast.rb', line 91 def false_body @false_body end |
#true_body ⇒ Object
Returns the value of attribute 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
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 |