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

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of Case.



2660
2661
2662
2663
2664
2665
2666
# File 'lib/syntax_tree/node.rb', line 2660

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



2658
2659
2660
# File 'lib/syntax_tree/node.rb', line 2658

def comments
  @comments
end

#consequentObject (readonly)

In | When

the next clause in the chain



2655
2656
2657
# File 'lib/syntax_tree/node.rb', line 2655

def consequent
  @consequent
end

#keywordObject (readonly)

Kw

the keyword that opens this expression



2649
2650
2651
# File 'lib/syntax_tree/node.rb', line 2649

def keyword
  @keyword
end

#valueObject (readonly)

nil | untyped

optional value being switched on



2652
2653
2654
# File 'lib/syntax_tree/node.rb', line 2652

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



2668
2669
2670
# File 'lib/syntax_tree/node.rb', line 2668

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

#child_nodesObject Also known as: deconstruct



2672
2673
2674
# File 'lib/syntax_tree/node.rb', line 2672

def child_nodes
  [keyword, value, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



2678
2679
2680
2681
2682
2683
2684
2685
2686
# File 'lib/syntax_tree/node.rb', line 2678

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

#format(q) ⇒ Object



2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
# File 'lib/syntax_tree/node.rb', line 2688

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