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



2785
2786
2787
2788
2789
2790
2791
# File 'lib/syntax_tree/node.rb', line 2785

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



2783
2784
2785
# File 'lib/syntax_tree/node.rb', line 2783

def comments
  @comments
end

#consequentObject (readonly)

In | When

the next clause in the chain



2780
2781
2782
# File 'lib/syntax_tree/node.rb', line 2780

def consequent
  @consequent
end

#keywordObject (readonly)

Kw

the keyword that opens this expression



2774
2775
2776
# File 'lib/syntax_tree/node.rb', line 2774

def keyword
  @keyword
end

#valueObject (readonly)

nil | untyped

optional value being switched on



2777
2778
2779
# File 'lib/syntax_tree/node.rb', line 2777

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



2793
2794
2795
# File 'lib/syntax_tree/node.rb', line 2793

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

#child_nodesObject Also known as: deconstruct



2797
2798
2799
# File 'lib/syntax_tree/node.rb', line 2797

def child_nodes
  [keyword, value, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



2803
2804
2805
2806
2807
2808
2809
2810
2811
# File 'lib/syntax_tree/node.rb', line 2803

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

#format(q) ⇒ Object



2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
# File 'lib/syntax_tree/node.rb', line 2813

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