Class: Dentaku::AST::Case
Instance Method Summary collapse
- #dependencies(context = {}) ⇒ Object
-
#initialize(*nodes) ⇒ Case
constructor
A new instance of Case.
- #value(context = {}) ⇒ Object
Methods inherited from Node
Constructor Details
#initialize(*nodes) ⇒ Case
Returns a new instance of Case.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dentaku/ast/case.rb', line 11 def initialize(*nodes) @switch = nodes.shift unless @switch.is_a?(AST::CaseSwitchVariable) raise ParseError.for(:node_invalid), 'Case missing switch variable' end @conditions = nodes @else = nil @else = @conditions.pop if @conditions.last.is_a?(AST::CaseElse) @conditions.each do |condition| unless condition.is_a?(AST::CaseConditional) raise ParseError.for(:node_invalid), "#{condition} is not a CaseConditional" end end end |
Instance Method Details
#dependencies(context = {}) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/dentaku/ast/case.rb', line 45 def dependencies(context = {}) # TODO: should short-circuit switch_dependencies(context) + condition_dependencies(context) + else_dependencies(context) end |
#value(context = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/dentaku/ast/case.rb', line 30 def value(context = {}) switch_value = @switch.value(context) @conditions.each do |condition| if condition.when.value(context) == switch_value return condition.then.value(context) end end if @else return @else.value(context) else raise ArgumentError.for(:invalid_value), "No block matched the switch value '#{switch_value}'" end end |