Class: SyntaxTree::Command

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

Overview

Command represents a method call with arguments and no parentheses. Note that Command nodes only happen when there is no explicit receiver for this method.

method argument

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(message:, arguments:, block:, location:) ⇒ Command

Returns a new instance of Command.



3420
3421
3422
3423
3424
3425
3426
# File 'lib/syntax_tree/node.rb', line 3420

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

Instance Attribute Details

#argumentsObject (readonly)

Args

the arguments being sent with the message



3412
3413
3414
# File 'lib/syntax_tree/node.rb', line 3412

def arguments
  @arguments
end

#blockObject (readonly)

nil | Block

the optional block being passed to the method



3415
3416
3417
# File 'lib/syntax_tree/node.rb', line 3415

def block
  @block
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3418
3419
3420
# File 'lib/syntax_tree/node.rb', line 3418

def comments
  @comments
end

#messageObject (readonly)

Const | Ident

the message being sent to the implicit receiver



3409
3410
3411
# File 'lib/syntax_tree/node.rb', line 3409

def message
  @message
end

Instance Method Details

#===(other) ⇒ Object



3470
3471
3472
3473
# File 'lib/syntax_tree/node.rb', line 3470

def ===(other)
  other.is_a?(Command) && message === other.message &&
    arguments === other.arguments && block === other.block
end

#accept(visitor) ⇒ Object



3428
3429
3430
# File 'lib/syntax_tree/node.rb', line 3428

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

#child_nodesObject Also known as: deconstruct



3432
3433
3434
# File 'lib/syntax_tree/node.rb', line 3432

def child_nodes
  [message, arguments, block]
end

#copy(message: nil, arguments: nil, block: nil, location: nil) ⇒ Object



3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
# File 'lib/syntax_tree/node.rb', line 3436

def copy(message: nil, arguments: nil, block: nil, location: nil)
  node =
    Command.new(
      message: message || self.message,
      arguments: arguments || self.arguments,
      block: block || self.block,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



3451
3452
3453
3454
3455
3456
3457
3458
3459
# File 'lib/syntax_tree/node.rb', line 3451

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

#format(q) ⇒ Object



3461
3462
3463
3464
3465
3466
3467
3468
# File 'lib/syntax_tree/node.rb', line 3461

def format(q)
  q.group do
    q.format(message)
    align(q, self) { q.format(arguments) }
  end

  q.format(block) if block
end