Class: SyntaxTree::Field

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

Overview

Field is always the child of an assignment. It represents assigning to a “field” on an object.

object.variable = value

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(parent:, operator:, name:, location:, comments: []) ⇒ Field

Returns a new instance of Field.



4447
4448
4449
4450
4451
4452
4453
# File 'lib/syntax_tree/node.rb', line 4447

def initialize(parent:, operator:, name:, location:, comments: [])
  @parent = parent
  @operator = operator
  @name = name
  @location = location
  @comments = comments
end

Instance Attribute Details

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



4445
4446
4447
# File 'lib/syntax_tree/node.rb', line 4445

def comments
  @comments
end

#name ⇒ Object (readonly)

[Const | Ident] the name of the field being assigned



4442
4443
4444
# File 'lib/syntax_tree/node.rb', line 4442

def name
  @name
end

#operator ⇒ Object (readonly)

[:"::" | Op | Period] the operator being used for the assignment



4439
4440
4441
# File 'lib/syntax_tree/node.rb', line 4439

def operator
  @operator
end

#parent ⇒ Object (readonly)

[untyped] the parent object that owns the field being assigned



4436
4437
4438
# File 'lib/syntax_tree/node.rb', line 4436

def parent
  @parent
end

Instance Method Details

#accept(visitor) ⇒ Object



4455
4456
4457
# File 'lib/syntax_tree/node.rb', line 4455

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

#child_nodes ⇒ Object Also known as: deconstruct



4459
4460
4461
# File 'lib/syntax_tree/node.rb', line 4459

def child_nodes
  [parent, (operator if operator != :"::"), name]
end

#deconstruct_keys(_keys) ⇒ Object



4465
4466
4467
4468
4469
4470
4471
4472
4473
# File 'lib/syntax_tree/node.rb', line 4465

def deconstruct_keys(_keys)
  {
    parent: parent,
    operator: operator,
    name: name,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



4475
4476
4477
4478
4479
4480
4481
# File 'lib/syntax_tree/node.rb', line 4475

def format(q)
  q.group do
    q.format(parent)
    q.format(CallOperatorFormatter.new(operator), stackable: false)
    q.format(name)
  end
end