class SyntaxTree::Assoc

Assoc represents a key-value pair within a hash. It is a child node of either an AssocListFromArgs or a BareAssocHash.

{ key1: value1, key2: value2 }

In the above example, the would be two Assoc nodes.

Attributes

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

key[R]
Node

the key of this pair

value[R]
nil | Node

the value of this pair

Public Class Methods

new(key:, value:, location:) click to toggle source
# File lib/syntax_tree/node.rb, line 1507
def initialize(key:, value:, location:)
  @key = key
  @value = value
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 1548
def ===(other)
  other.is_a?(Assoc) && key === other.key && value === other.value
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 1514
def accept(visitor)
  visitor.visit_assoc(self)
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 1518
def child_nodes
  [key, value]
end
Also aliased as: deconstruct
copy(key: nil, value: nil, location: nil) click to toggle source
# File lib/syntax_tree/node.rb, line 1522
def copy(key: nil, value: nil, location: nil)
  node =
    Assoc.new(
      key: key || self.key,
      value: value || self.value,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end
deconstruct()
Alias for: child_nodes
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/node.rb, line 1536
def deconstruct_keys(_keys)
  { key: key, value: value, location: location, comments: comments }
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 1540
def format(q)
  if value.is_a?(HashLiteral)
    format_contents(q)
  else
    q.group { format_contents(q) }
  end
end