Class: SyntaxTree::Assoc
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
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#key ⇒ Object
readonly
- untyped
-
the key of this pair.
-
#value ⇒ Object
readonly
- untyped
-
the value of this pair.
Attributes inherited from Node
Instance Method Summary collapse
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(key:, value:, location:, comments: []) ⇒ Assoc
constructor
A new instance of Assoc.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(key:, value:, location:, comments: []) ⇒ Assoc
Returns a new instance of Assoc.
1379 1380 1381 1382 1383 1384 |
# File 'lib/syntax_tree/node.rb', line 1379 def initialize(key:, value:, location:, comments: []) @key = key @value = value @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
1377 1378 1379 |
# File 'lib/syntax_tree/node.rb', line 1377 def comments @comments end |
#key ⇒ Object (readonly)
- untyped
-
the key of this pair
1371 1372 1373 |
# File 'lib/syntax_tree/node.rb', line 1371 def key @key end |
#value ⇒ Object (readonly)
- untyped
-
the value of this pair
1374 1375 1376 |
# File 'lib/syntax_tree/node.rb', line 1374 def value @value end |
Instance Method Details
#child_nodes ⇒ Object Also known as: deconstruct
1386 1387 1388 |
# File 'lib/syntax_tree/node.rb', line 1386 def child_nodes [key, value] end |
#deconstruct_keys(keys) ⇒ Object
1392 1393 1394 |
# File 'lib/syntax_tree/node.rb', line 1392 def deconstruct_keys(keys) { key: key, value: value, location: location, comments: comments } end |
#format(q) ⇒ Object
1396 1397 1398 1399 1400 1401 1402 |
# File 'lib/syntax_tree/node.rb', line 1396 def format(q) if value&.is_a?(HashLiteral) format_contents(q) else q.group { format_contents(q) } end end |
#pretty_print(q) ⇒ Object
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 |
# File 'lib/syntax_tree/node.rb', line 1404 def pretty_print(q) q.group(2, "(", ")") do q.text("assoc") q.breakable q.pp(key) if value q.breakable q.pp(value) end q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
1420 1421 1422 1423 1424 1425 1426 1427 1428 |
# File 'lib/syntax_tree/node.rb', line 1420 def to_json(*opts) { type: :assoc, key: key, value: value, loc: location, cmts: comments }.to_json(*opts) end |