Class: SyntaxTree::ERB::Block

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

Overview

This is a base class for a block that contains:

  • an opening

  • optional elements

  • optional closing

Direct Known Subclasses

ErbBlock, ErbControl, HtmlNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#format, #pretty_print, #skip?

Constructor Details

#initialize(opening:, location:, elements: nil, closing: nil) ⇒ Block

Returns a new instance of Block.



146
147
148
149
150
151
# File 'lib/syntax_tree/erb/nodes.rb', line 146

def initialize(opening:, location:, elements: nil, closing: nil)
  @opening = opening
  @elements = elements || []
  @closing = closing
  @location = location
end

Instance Attribute Details

#closingObject (readonly)

Returns the value of attribute closing.



145
146
147
# File 'lib/syntax_tree/erb/nodes.rb', line 145

def closing
  @closing
end

#elementsObject (readonly)

Returns the value of attribute elements.



145
146
147
# File 'lib/syntax_tree/erb/nodes.rb', line 145

def elements
  @elements
end

#locationObject (readonly)

Returns the value of attribute location.



145
146
147
# File 'lib/syntax_tree/erb/nodes.rb', line 145

def location
  @location
end

#openingObject (readonly)

Returns the value of attribute opening.



145
146
147
# File 'lib/syntax_tree/erb/nodes.rb', line 145

def opening
  @opening
end

Instance Method Details

#accept(visitor) ⇒ Object



153
154
155
# File 'lib/syntax_tree/erb/nodes.rb', line 153

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

#child_nodesObject Also known as: deconstruct



157
158
159
# File 'lib/syntax_tree/erb/nodes.rb', line 157

def child_nodes
  [opening, *elements, closing].compact
end

#deconstruct_keys(keys) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/syntax_tree/erb/nodes.rb', line 173

def deconstruct_keys(keys)
  {
    opening: opening,
    elements: elements,
    closing: closing,
    location: location
  }
end

#new_lineObject



161
162
163
# File 'lib/syntax_tree/erb/nodes.rb', line 161

def new_line
  closing.new_line if closing.respond_to?(:new_line)
end

#without_new_lineObject



165
166
167
168
169
# File 'lib/syntax_tree/erb/nodes.rb', line 165

def without_new_line
  self.class.new(
    **deconstruct_keys([]).merge(closing: closing&.without_new_line)
  )
end