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.



2603
2604
2605
2606
2607
2608
2609
# File 'lib/syntax_tree/node.rb', line 2603

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



2601
2602
2603
# File 'lib/syntax_tree/node.rb', line 2601

def comments
  @comments
end

#consequentObject (readonly)

In | When

the next clause in the chain



2598
2599
2600
# File 'lib/syntax_tree/node.rb', line 2598

def consequent
  @consequent
end

#keywordObject (readonly)

Kw

the keyword that opens this expression



2592
2593
2594
# File 'lib/syntax_tree/node.rb', line 2592

def keyword
  @keyword
end

#valueObject (readonly)

nil | untyped

optional value being switched on



2595
2596
2597
# File 'lib/syntax_tree/node.rb', line 2595

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



2611
2612
2613
# File 'lib/syntax_tree/node.rb', line 2611

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

#child_nodesObject Also known as: deconstruct



2615
2616
2617
# File 'lib/syntax_tree/node.rb', line 2615

def child_nodes
  [keyword, value, consequent]
end

#deconstruct_keys(keys) ⇒ Object



2621
2622
2623
2624
2625
2626
2627
2628
2629
# File 'lib/syntax_tree/node.rb', line 2621

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

#format(q) ⇒ Object



2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
# File 'lib/syntax_tree/node.rb', line 2631

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