Class: Luoma::TernaryExpression

Inherits:
Expression show all
Defined in:
lib/luoma/expression.rb,
sig/luoma/expression.rbs

Instance Attribute Summary

Attributes inherited from Expression

#span, #token

Instance Method Summary collapse

Methods inherited from Expression

#scope

Constructor Details

#initialize(token, consequence, condition, alternative) ⇒ TernaryExpression

(t_token, Expression, Expression, Expression?) -> void

Parameters:



98
99
100
101
102
103
104
# File 'lib/luoma/expression.rb', line 98

def initialize(token, consequence, condition, alternative)
  super(token)
  @consequence = consequence
  @condition = condition
  @alternative = alternative
  @span = Luoma.span(@consequence.token, (@alternative || @condition).span)
end

Instance Method Details

#childrenObject



115
116
117
# File 'lib/luoma/expression.rb', line 115

def children
  [@consequence, @condition, @alternative].compact
end

#evaluate(context) ⇒ Object

Signature:

  • (RenderContext) -> untyped



107
108
109
110
111
112
113
# File 'lib/luoma/expression.rb', line 107

def evaluate(context)
  if context.env.truthy?(@condition.evaluate(context), context)
    @consequence.evaluate(context)
  else
    @alternative&.evaluate(context) || :nothing
  end
end

#to_sObject

Signature:

  • () -> String



120
121
122
123
124
125
126
# File 'lib/luoma/expression.rb', line 120

def to_s
  if @alternative
    "#{@consequence} if #{@condition} else #{@alternative}"
  else
    "#{@consequence} if #{@condition}"
  end
end