Class: SyntaxTree::Assoc

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

Overview

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 AssocNew nodes.

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(key:, value:, location:, comments: []) ⇒ Assoc

Returns a new instance of Assoc.



1123
1124
1125
1126
1127
1128
# File 'lib/syntax_tree/node.rb', line 1123

def initialize(key:, value:, location:, comments: [])
  @key = key
  @value = value
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1121
1122
1123
# File 'lib/syntax_tree/node.rb', line 1121

def comments
  @comments
end

#keyObject (readonly)

untyped

the key of this pair



1115
1116
1117
# File 'lib/syntax_tree/node.rb', line 1115

def key
  @key
end

#valueObject (readonly)

untyped

the value of this pair



1118
1119
1120
# File 'lib/syntax_tree/node.rb', line 1118

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



1130
1131
1132
# File 'lib/syntax_tree/node.rb', line 1130

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

#child_nodesObject Also known as: deconstruct



1134
1135
1136
# File 'lib/syntax_tree/node.rb', line 1134

def child_nodes
  [key, value]
end

#deconstruct_keys(keys) ⇒ Object



1140
1141
1142
# File 'lib/syntax_tree/node.rb', line 1140

def deconstruct_keys(keys)
  { key: key, value: value, location: location, comments: comments }
end

#format(q) ⇒ Object



1144
1145
1146
1147
1148
1149
1150
# File 'lib/syntax_tree/node.rb', line 1144

def format(q)
  if value&.is_a?(HashLiteral)
    format_contents(q)
  else
    q.group { format_contents(q) }
  end
end