Class: Fabulator::Expr::IfExpr

Inherits:
Object
  • Object
show all
Defined in:
lib/fabulator/expr/if_expr.rb

Instance Method Summary collapse

Constructor Details

#initialize(t, a, b) ⇒ IfExpr

Returns a new instance of IfExpr.



4
5
6
7
8
# File 'lib/fabulator/expr/if_expr.rb', line 4

def initialize(t, a, b)
  @test = t
  @then_expr = a
  @else_expr = b
end

Instance Method Details

#run(context, autovivify = false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/fabulator/expr/if_expr.rb', line 10

def run(context, autovivify = false)
  res = @test.run(context.merge)

  if res.nil? || res.empty? || !res.first.value
    res = @else_expr.nil? ? [] : @else_expr.run(context.merge, autovivify)
  else
    res = @then_expr.run(context.merge, autovivify)
  end
  return res
end