class SyntaxTree::Case

Case represents the beginning of a case chain.

case value
when 1
  "one"
when 2
  "two"
else
  "number"
end

Attributes

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

consequent[R]
In | When

the next clause in the chain

keyword[R]
Kw

the keyword that opens this expression

value[R]
nil | Node

optional value being switched on

Public Class Methods

new(keyword:, value:, consequent:, location:) click to toggle source
# File lib/syntax_tree/node.rb, line 3132
def initialize(keyword:, value:, consequent:, location:)
  @keyword = keyword
  @value = value
  @consequent = consequent
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 3190
def ===(other)
  other.is_a?(Case) && keyword === other.keyword && value === other.value &&
    consequent === other.consequent
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 3140
def accept(visitor)
  visitor.visit_case(self)
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 3144
def child_nodes
  [keyword, value, consequent]
end
Also aliased as: deconstruct
copy(keyword: nil, value: nil, consequent: nil, location: nil) click to toggle source
# File lib/syntax_tree/node.rb, line 3148
def copy(keyword: nil, value: nil, consequent: nil, location: nil)
  node =
    Case.new(
      keyword: keyword || self.keyword,
      value: value || self.value,
      consequent: consequent || self.consequent,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end
deconstruct()
Alias for: child_nodes
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/node.rb, line 3163
def deconstruct_keys(_keys)
  {
    keyword: keyword,
    value: value,
    consequent: consequent,
    location: location,
    comments: comments
  }
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 3173
def format(q)
  q.group do
    q.format(keyword)

    if value
      q.text(" ")
      q.format(value)
    end

    q.breakable_force
    q.format(consequent)
    q.breakable_force

    q.text("end")
  end
end