Class: SyntaxTree::RescueEx

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

RescueEx represents the list of exceptions being rescued in a rescue clause.

begin
rescue Exception => exception
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(exceptions:, variable:, location:) ⇒ RescueEx

Returns a new instance of RescueEx.



9360
9361
9362
9363
9364
9365
# File 'lib/syntax_tree/node.rb', line 9360

def initialize(exceptions:, variable:, location:)
  @exceptions = exceptions
  @variable = variable
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9358
9359
9360
# File 'lib/syntax_tree/node.rb', line 9358

def comments
  @comments
end

#exceptionsObject (readonly)

nil | Node

the list of exceptions being rescued



9351
9352
9353
# File 'lib/syntax_tree/node.rb', line 9351

def exceptions
  @exceptions
end

#variableObject (readonly)

nil | Field | VarField

the expression being used to capture the raised

exception



9355
9356
9357
# File 'lib/syntax_tree/node.rb', line 9355

def variable
  @variable
end

Instance Method Details

#===(other) ⇒ Object



9412
9413
9414
9415
# File 'lib/syntax_tree/node.rb', line 9412

def ===(other)
  other.is_a?(RescueEx) && exceptions === other.exceptions &&
    variable === other.variable
end

#accept(visitor) ⇒ Object



9367
9368
9369
# File 'lib/syntax_tree/node.rb', line 9367

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

#child_nodesObject Also known as: deconstruct



9371
9372
9373
# File 'lib/syntax_tree/node.rb', line 9371

def child_nodes
  [*exceptions, variable]
end

#copy(exceptions: nil, variable: nil, location: nil) ⇒ Object



9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
# File 'lib/syntax_tree/node.rb', line 9375

def copy(exceptions: nil, variable: nil, location: nil)
  node =
    RescueEx.new(
      exceptions: exceptions || self.exceptions,
      variable: variable || self.variable,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



9389
9390
9391
9392
9393
9394
9395
9396
# File 'lib/syntax_tree/node.rb', line 9389

def deconstruct_keys(_keys)
  {
    exceptions: exceptions,
    variable: variable,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
# File 'lib/syntax_tree/node.rb', line 9398

def format(q)
  q.group do
    if exceptions
      q.text(" ")
      q.format(exceptions)
    end

    if variable
      q.text(" => ")
      q.format(variable)
    end
  end
end