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

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of Assoc.



1240
1241
1242
1243
1244
1245
# File 'lib/syntax_tree/node.rb', line 1240

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



1238
1239
1240
# File 'lib/syntax_tree/node.rb', line 1238

def comments
  @comments
end

#keyObject (readonly)

untyped

the key of this pair



1232
1233
1234
# File 'lib/syntax_tree/node.rb', line 1232

def key
  @key
end

#valueObject (readonly)

untyped

the value of this pair



1235
1236
1237
# File 'lib/syntax_tree/node.rb', line 1235

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



1247
1248
1249
# File 'lib/syntax_tree/node.rb', line 1247

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

#child_nodesObject Also known as: deconstruct



1251
1252
1253
# File 'lib/syntax_tree/node.rb', line 1251

def child_nodes
  [key, value]
end

#deconstruct_keys(_keys) ⇒ Object



1257
1258
1259
# File 'lib/syntax_tree/node.rb', line 1257

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

#format(q) ⇒ Object



1261
1262
1263
1264
1265
1266
1267
# File 'lib/syntax_tree/node.rb', line 1261

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