Class: Melbourne::AST::When

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

Overview

A when statement in a case statement as in:

case a
  when 1 then
    # something
  when 2
    # something
end

Instance Attribute Summary collapse

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Node

#ascii_graph

Constructor Details

#initialize(line, conditions, body) ⇒ When



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/melbourne/ast/control_flow.rb', line 83

def initialize(line, conditions, body)
  @line = line
  @body = body || Nil.new(line)
  @splat = nil
  @single = nil

  if conditions.kind_of? ArrayLiteral
    if conditions.body.last.kind_of? When
      last = conditions.body.pop
      if last.conditions.kind_of? ArrayLiteral
        conditions.body.concat last.conditions.body
      elsif last.single
        @splat = SplatWhen.new line, last.single
      else
        @splat = SplatWhen.new line, last.conditions
      end
    end

    if conditions.body.size == 1 and !@splat
      @single = conditions.body.first
    else
      @conditions = conditions
    end
  else
    @conditions = conditions
  end
end

Instance Attribute Details

#bodyObject

The body for the when statement



73
74
75
# File 'lib/melbourne/ast/control_flow.rb', line 73

def body
  @body
end

#conditionsObject

The conditions for which the when statement is true or nil if only a single value is specified



69
70
71
# File 'lib/melbourne/ast/control_flow.rb', line 69

def conditions
  @conditions
end

#singleObject

The single value for the when statement if only a single value is specified, nil otherwise



77
78
79
# File 'lib/melbourne/ast/control_flow.rb', line 77

def single
  @single
end

#splatObject

Any splat (+*something+) that is specified as a condition for the when statement or nil of no splat is specified



81
82
83
# File 'lib/melbourne/ast/control_flow.rb', line 81

def splat
  @splat
end