Class: SyntaxTree::ARefField

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

Overview

ARefField represents assigning values into collections at specific indices. Put another way, it’s any time you’re calling the method #[]=. The ARefField node itself is just the left side of the assignment, and they’re always wrapped in assign nodes.

collection[index] = value

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(collection:, index:, location:) ⇒ ARefField

Returns a new instance of ARefField.



655
656
657
658
659
660
# File 'lib/syntax_tree/node.rb', line 655

def initialize(collection:, index:, location:)
  @collection = collection
  @index = index
  @location = location
  @comments = []
end

Instance Attribute Details

#collectionObject (readonly)

Node

the value being indexed



647
648
649
# File 'lib/syntax_tree/node.rb', line 647

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



653
654
655
# File 'lib/syntax_tree/node.rb', line 653

def comments
  @comments
end

#indexObject (readonly)

nil | Args

the value being passed within the brackets



650
651
652
# File 'lib/syntax_tree/node.rb', line 650

def index
  @index
end

Instance Method Details

#===(other) ⇒ Object



710
711
712
713
# File 'lib/syntax_tree/node.rb', line 710

def ===(other)
  other.is_a?(ARefField) && collection === other.collection &&
    index === other.index
end

#accept(visitor) ⇒ Object



662
663
664
# File 'lib/syntax_tree/node.rb', line 662

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

#child_nodesObject Also known as: deconstruct



666
667
668
# File 'lib/syntax_tree/node.rb', line 666

def child_nodes
  [collection, index]
end

#copy(collection: nil, index: nil, location: nil) ⇒ Object



670
671
672
673
674
675
676
677
678
679
680
# File 'lib/syntax_tree/node.rb', line 670

def copy(collection: nil, index: nil, location: nil)
  node =
    ARefField.new(
      collection: collection || self.collection,
      index: index || self.index,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



684
685
686
687
688
689
690
691
# File 'lib/syntax_tree/node.rb', line 684

def deconstruct_keys(_keys)
  {
    collection: collection,
    index: index,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'lib/syntax_tree/node.rb', line 693

def format(q)
  q.group do
    q.format(collection)
    q.text("[")

    if index
      q.indent do
        q.breakable_empty
        q.format(index)
      end
      q.breakable_empty
    end

    q.text("]")
  end
end