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

Constructor Details

#initialize(keyword:, statements:, location:) ⇒ Ensure

Returns a new instance of Ensure.



5076
5077
5078
5079
5080
5081
# File 'lib/syntax_tree/node.rb', line 5076

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



5074
5075
5076
# File 'lib/syntax_tree/node.rb', line 5074

def comments
  @comments
end

#keywordObject (readonly)

Kw

the ensure keyword that began this node



5068
5069
5070
# File 'lib/syntax_tree/node.rb', line 5068

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to be executed



5071
5072
5073
# File 'lib/syntax_tree/node.rb', line 5071

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



5125
5126
5127
5128
# File 'lib/syntax_tree/node.rb', line 5125

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

#accept(visitor) ⇒ Object



5083
5084
5085
# File 'lib/syntax_tree/node.rb', line 5083

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

#child_nodesObject Also known as: deconstruct



5087
5088
5089
# File 'lib/syntax_tree/node.rb', line 5087

def child_nodes
  [keyword, statements]
end

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



5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
# File 'lib/syntax_tree/node.rb', line 5091

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



5105
5106
5107
5108
5109
5110
5111
5112
# File 'lib/syntax_tree/node.rb', line 5105

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

#format(q) ⇒ Object



5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
# File 'lib/syntax_tree/node.rb', line 5114

def format(q)
  q.format(keyword)

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