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.



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

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



5065
5066
5067
# File 'lib/syntax_tree/node.rb', line 5065

def comments
  @comments
end

#keywordObject (readonly)

Kw

the ensure keyword that began this node



5059
5060
5061
# File 'lib/syntax_tree/node.rb', line 5059

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to be executed



5062
5063
5064
# File 'lib/syntax_tree/node.rb', line 5062

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



5116
5117
5118
5119
# File 'lib/syntax_tree/node.rb', line 5116

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [keyword, statements]
end

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



5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
# File 'lib/syntax_tree/node.rb', line 5082

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



5096
5097
5098
5099
5100
5101
5102
5103
# File 'lib/syntax_tree/node.rb', line 5096

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

#format(q) ⇒ Object



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

def format(q)
  q.format(keyword)

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