Class: SyntaxTree::MethodAddBlock

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

Overview

MethodAddBlock represents a method call with a block argument.

method {}

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(call:, block:, location:) ⇒ MethodAddBlock

Returns a new instance of MethodAddBlock.



7460
7461
7462
7463
7464
7465
# File 'lib/syntax_tree/node.rb', line 7460

def initialize(call:, block:, location:)
  @call = call
  @block = block
  @location = location
  @comments = []
end

Instance Attribute Details

#blockObject (readonly)

Block

the block being sent with the method call



7455
7456
7457
# File 'lib/syntax_tree/node.rb', line 7455

def block
  @block
end

#callObject (readonly)

Call | Command | CommandCall

the method call



7452
7453
7454
# File 'lib/syntax_tree/node.rb', line 7452

def call
  @call
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7458
7459
7460
# File 'lib/syntax_tree/node.rb', line 7458

def comments
  @comments
end

Instance Method Details

#===(other) ⇒ Object



7509
7510
7511
7512
# File 'lib/syntax_tree/node.rb', line 7509

def ===(other)
  other.is_a?(MethodAddBlock) && call === other.call &&
    block === other.block
end

#accept(visitor) ⇒ Object



7467
7468
7469
# File 'lib/syntax_tree/node.rb', line 7467

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

#child_nodesObject Also known as: deconstruct



7471
7472
7473
# File 'lib/syntax_tree/node.rb', line 7471

def child_nodes
  [call, block]
end

#copy(call: nil, block: nil, location: nil) ⇒ Object



7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
# File 'lib/syntax_tree/node.rb', line 7475

def copy(call: nil, block: nil, location: nil)
  node =
    MethodAddBlock.new(
      call: call || self.call,
      block: block || self.block,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



7489
7490
7491
# File 'lib/syntax_tree/node.rb', line 7489

def deconstruct_keys(_keys)
  { call: call, block: block, location: location, comments: comments }
end

#format(q) ⇒ Object



7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
# File 'lib/syntax_tree/node.rb', line 7493

def format(q)
  # If we're at the top of a call chain, then we're going to do some
  # specialized printing in case we can print it nicely. We _only_ do this
  # at the top of the chain to avoid weird recursion issues.
  if CallChainFormatter.chained?(call) &&
       !CallChainFormatter.chained?(q.parent)
    q.group do
      q
        .if_break { CallChainFormatter.new(self).format(q) }
        .if_flat { format_contents(q) }
    end
  else
    format_contents(q)
  end
end

#format_contents(q) ⇒ Object



7514
7515
7516
7517
# File 'lib/syntax_tree/node.rb', line 7514

def format_contents(q)
  q.format(call)
  q.format(block)
end