Class: SyntaxTree::Case

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Case represents the beginning of a case chain.

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

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(keyword:, value:, consequent:, location:, comments: []) ⇒ Case

Returns a new instance of Case.



2259
2260
2261
2262
2263
2264
2265
# File 'lib/syntax_tree/node.rb', line 2259

def initialize(keyword:, value:, consequent:, location:, comments: [])
  @keyword = keyword
  @value = value
  @consequent = consequent
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2257
2258
2259
# File 'lib/syntax_tree/node.rb', line 2257

def comments
  @comments
end

#consequentObject (readonly)

In | When

the next clause in the chain



2254
2255
2256
# File 'lib/syntax_tree/node.rb', line 2254

def consequent
  @consequent
end

#keywordObject (readonly)

Kw

the keyword that opens this expression



2248
2249
2250
# File 'lib/syntax_tree/node.rb', line 2248

def keyword
  @keyword
end

#valueObject (readonly)

nil | untyped

optional value being switched on



2251
2252
2253
# File 'lib/syntax_tree/node.rb', line 2251

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



2267
2268
2269
# File 'lib/syntax_tree/node.rb', line 2267

def accept(visitor)
  visitor.visit_case(self)
end

#child_nodesObject Also known as: deconstruct



2271
2272
2273
# File 'lib/syntax_tree/node.rb', line 2271

def child_nodes
  [keyword, value, consequent]
end

#deconstruct_keys(keys) ⇒ Object



2277
2278
2279
2280
2281
2282
2283
2284
2285
# File 'lib/syntax_tree/node.rb', line 2277

def deconstruct_keys(keys)
  {
    keyword: keyword,
    value: value,
    consequent: consequent,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
# File 'lib/syntax_tree/node.rb', line 2287

def format(q)
  q.group do
    q.format(keyword)

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

    q.breakable(force: true)
    q.format(consequent)
    q.breakable(force: true)

    q.text("end")
  end
end