Class: SyntaxTree::Ensure

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

Overview

Ensure represents the use of the ensure keyword and its subsequent statements.

begin
ensure
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:, statements:, location:) ⇒ Ensure

Returns a new instance of Ensure.



5171
5172
5173
5174
5175
5176
# File 'lib/syntax_tree/node.rb', line 5171

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5169
5170
5171
# File 'lib/syntax_tree/node.rb', line 5169

def comments
  @comments
end

#keywordObject (readonly)

Kw

the ensure keyword that began this node



5163
5164
5165
# File 'lib/syntax_tree/node.rb', line 5163

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to be executed



5166
5167
5168
# File 'lib/syntax_tree/node.rb', line 5166

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



5220
5221
5222
5223
# File 'lib/syntax_tree/node.rb', line 5220

def ===(other)
  other.is_a?(Ensure) && keyword === other.keyword &&
    statements === other.statements
end

#accept(visitor) ⇒ Object



5178
5179
5180
# File 'lib/syntax_tree/node.rb', line 5178

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

#child_nodesObject Also known as: deconstruct



5182
5183
5184
# File 'lib/syntax_tree/node.rb', line 5182

def child_nodes
  [keyword, statements]
end

#copy(keyword: nil, statements: nil, location: nil) ⇒ Object



5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
# File 'lib/syntax_tree/node.rb', line 5186

def copy(keyword: nil, statements: nil, location: nil)
  node =
    Ensure.new(
      keyword: keyword || self.keyword,
      statements: statements || self.statements,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



5200
5201
5202
5203
5204
5205
5206
5207
# File 'lib/syntax_tree/node.rb', line 5200

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

#format(q) ⇒ Object



5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
# File 'lib/syntax_tree/node.rb', line 5209

def format(q)
  q.format(keyword)

  unless statements.empty?
    q.indent do
      q.breakable_force
      q.format(statements)
    end
  end
end