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.



5174
5175
5176
5177
5178
5179
# File 'lib/syntax_tree/node.rb', line 5174

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



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

def comments
  @comments
end

#keywordObject (readonly)

Kw

the ensure keyword that began this node



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

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to be executed



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

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



5223
5224
5225
5226
# File 'lib/syntax_tree/node.rb', line 5223

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



5185
5186
5187
# File 'lib/syntax_tree/node.rb', line 5185

def child_nodes
  [keyword, statements]
end

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



5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
# File 'lib/syntax_tree/node.rb', line 5189

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



5203
5204
5205
5206
5207
5208
5209
5210
# File 'lib/syntax_tree/node.rb', line 5203

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

#format(q) ⇒ Object



5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
# File 'lib/syntax_tree/node.rb', line 5212

def format(q)
  q.format(keyword)

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