Class: TPPlus::Nodes::CaseNode

Inherits:
Object
  • Object
show all
Defined in:
lib/tp_plus/nodes/case_node.rb

Instance Method Summary collapse

Constructor Details

#initialize(var, conditions, else_condition) ⇒ CaseNode

Returns a new instance of CaseNode.



4
5
6
7
8
# File 'lib/tp_plus/nodes/case_node.rb', line 4

def initialize(var, conditions, else_condition)
  @var = var
  @conditions = conditions
  @else_condition = else_condition
end

Instance Method Details

#else_condition(context) ⇒ Object



10
11
12
13
14
# File 'lib/tp_plus/nodes/case_node.rb', line 10

def else_condition(context)
  return "" if @else_condition.nil?

  " ;\n#{@else_condition.eval(context)}"
end

#eval(context) ⇒ Object



28
29
30
# File 'lib/tp_plus/nodes/case_node.rb', line 28

def eval(context)
  "SELECT #{@var.eval(context)}#{@conditions.shift.eval(context, no_indent: true)}#{other_conditions(context)}#{else_condition(context)}"
end

#other_conditions(context) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tp_plus/nodes/case_node.rb', line 16

def other_conditions(context)
  return "" if @conditions.empty?

  s = " ;\n"
  @conditions.reject! {|c| c.nil? }.each do |c|
    s += c.eval(context)
    s += " ;\n" unless c == @conditions.last
  end

  s
end