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:) ⇒ Case

Returns a new instance of Case.



3055
3056
3057
3058
3059
3060
3061
# File 'lib/syntax_tree/node.rb', line 3055

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3053
3054
3055
# File 'lib/syntax_tree/node.rb', line 3053

def comments
  @comments
end

#consequentObject (readonly)

In | When

the next clause in the chain



3050
3051
3052
# File 'lib/syntax_tree/node.rb', line 3050

def consequent
  @consequent
end

#keywordObject (readonly)

Kw

the keyword that opens this expression



3044
3045
3046
# File 'lib/syntax_tree/node.rb', line 3044

def keyword
  @keyword
end

#valueObject (readonly)

nil | untyped

optional value being switched on



3047
3048
3049
# File 'lib/syntax_tree/node.rb', line 3047

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



3113
3114
3115
3116
# File 'lib/syntax_tree/node.rb', line 3113

def ===(other)
  other.is_a?(Case) && keyword === other.keyword && value === other.value &&
    consequent === other.consequent
end

#accept(visitor) ⇒ Object



3063
3064
3065
# File 'lib/syntax_tree/node.rb', line 3063

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

#child_nodesObject Also known as: deconstruct



3067
3068
3069
# File 'lib/syntax_tree/node.rb', line 3067

def child_nodes
  [keyword, value, consequent]
end

#copy(keyword: nil, value: nil, consequent: nil, location: nil) ⇒ Object



3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
# File 'lib/syntax_tree/node.rb', line 3071

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_keys(_keys) ⇒ Object



3086
3087
3088
3089
3090
3091
3092
3093
3094
# File 'lib/syntax_tree/node.rb', line 3086

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

#format(q) ⇒ Object



3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
# File 'lib/syntax_tree/node.rb', line 3096

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