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



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

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



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

def comments
  @comments
end

#keywordObject (readonly)

Kw

the ensure keyword that began this node



5054
5055
5056
# File 'lib/syntax_tree/node.rb', line 5054

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to be executed



5057
5058
5059
# File 'lib/syntax_tree/node.rb', line 5057

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



5111
5112
5113
5114
# File 'lib/syntax_tree/node.rb', line 5111

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [keyword, statements]
end

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



5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
# File 'lib/syntax_tree/node.rb', line 5077

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



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

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

#format(q) ⇒ Object



5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
# File 'lib/syntax_tree/node.rb', line 5100

def format(q)
  q.format(keyword)

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