Class: Rjq::AST::If

Inherits:
Node
  • Object
show all
Defined in:
lib/rjq/ast.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#source_span

Instance Method Summary collapse

Methods inherited from Node

#assign_value, #invalid_path_message, #paths, #take, #with_source_span

Constructor Details

#initialize(condition, then_branch, else_branch) ⇒ If

Returns a new instance of If.



673
674
675
676
677
# File 'lib/rjq/ast.rb', line 673

def initialize(condition, then_branch, else_branch)
  @condition = condition
  @then_branch = then_branch
  @else_branch = else_branch
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



671
672
673
# File 'lib/rjq/ast.rb', line 671

def condition
  @condition
end

#else_branchObject (readonly)

Returns the value of attribute else_branch.



671
672
673
# File 'lib/rjq/ast.rb', line 671

def else_branch
  @else_branch
end

#then_branchObject (readonly)

Returns the value of attribute then_branch.



671
672
673
# File 'lib/rjq/ast.rb', line 671

def then_branch
  @then_branch
end

Instance Method Details

#eval(input, context) ⇒ Object



679
680
681
682
683
684
# File 'lib/rjq/ast.rb', line 679

def eval(input, context)
  @condition.eval(input, context).flat_map do |value|
    branch = Value.truthy?(value) ? @then_branch : @else_branch
    branch.eval(input, context)
  end
end