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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

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

Returns a new instance of Case.



3120
3121
3122
3123
3124
3125
3126
# File 'lib/syntax_tree/node.rb', line 3120

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



3118
3119
3120
# File 'lib/syntax_tree/node.rb', line 3118

def comments
  @comments
end

#consequentObject (readonly)

In | When

the next clause in the chain



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

def consequent
  @consequent
end

#keywordObject (readonly)

Kw

the keyword that opens this expression



3109
3110
3111
# File 'lib/syntax_tree/node.rb', line 3109

def keyword
  @keyword
end

#valueObject (readonly)

nil | Node

optional value being switched on



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

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



3178
3179
3180
3181
# File 'lib/syntax_tree/node.rb', line 3178

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

#accept(visitor) ⇒ Object



3128
3129
3130
# File 'lib/syntax_tree/node.rb', line 3128

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

#child_nodesObject Also known as: deconstruct



3132
3133
3134
# File 'lib/syntax_tree/node.rb', line 3132

def child_nodes
  [keyword, value, consequent]
end

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



3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
# File 'lib/syntax_tree/node.rb', line 3136

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



3151
3152
3153
3154
3155
3156
3157
3158
3159
# File 'lib/syntax_tree/node.rb', line 3151

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

#format(q) ⇒ Object



3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
# File 'lib/syntax_tree/node.rb', line 3161

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