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.



7472
7473
7474
7475
7476
7477
# File 'lib/syntax_tree/node.rb', line 7472

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



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

def block
  @block
end

#callObject (readonly)

Call | Command | CommandCall

the method call



7464
7465
7466
# File 'lib/syntax_tree/node.rb', line 7464

def call
  @call
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

Instance Method Details

#===(other) ⇒ Object



7521
7522
7523
7524
# File 'lib/syntax_tree/node.rb', line 7521

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

#accept(visitor) ⇒ Object



7479
7480
7481
# File 'lib/syntax_tree/node.rb', line 7479

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

#child_nodesObject Also known as: deconstruct



7483
7484
7485
# File 'lib/syntax_tree/node.rb', line 7483

def child_nodes
  [call, block]
end

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



7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
# File 'lib/syntax_tree/node.rb', line 7487

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



7501
7502
7503
# File 'lib/syntax_tree/node.rb', line 7501

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

#format(q) ⇒ Object



7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
# File 'lib/syntax_tree/node.rb', line 7505

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



7526
7527
7528
7529
# File 'lib/syntax_tree/node.rb', line 7526

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