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 Assoc nodes.
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#key ⇒ Object
readonly
- Node
-
the key of this pair.
-
#value ⇒ Object
readonly
- nil | Node
-
the value of this pair.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(key: nil, value: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(key:, value:, location:) ⇒ Assoc
constructor
A new instance of Assoc.
Methods inherited from Node
#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(key:, value:, location:) ⇒ Assoc
Returns a new instance of Assoc.
1507 1508 1509 1510 1511 1512 |
# File 'lib/syntax_tree/node.rb', line 1507 def initialize(key:, value:, location:) @key = key @value = value @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
1505 1506 1507 |
# File 'lib/syntax_tree/node.rb', line 1505 def comments @comments end |
#key ⇒ Object (readonly)
- Node
-
the key of this pair
1499 1500 1501 |
# File 'lib/syntax_tree/node.rb', line 1499 def key @key end |
#value ⇒ Object (readonly)
- nil | Node
-
the value of this pair
1502 1503 1504 |
# File 'lib/syntax_tree/node.rb', line 1502 def value @value end |
Instance Method Details
#===(other) ⇒ Object
1548 1549 1550 |
# File 'lib/syntax_tree/node.rb', line 1548 def ===(other) other.is_a?(Assoc) && key === other.key && value === other.value end |
#accept(visitor) ⇒ Object
1514 1515 1516 |
# File 'lib/syntax_tree/node.rb', line 1514 def accept(visitor) visitor.visit_assoc(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
1518 1519 1520 |
# File 'lib/syntax_tree/node.rb', line 1518 def child_nodes [key, value] end |
#copy(key: nil, value: nil, location: nil) ⇒ Object
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 |
# 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_keys(_keys) ⇒ Object
1536 1537 1538 |
# File 'lib/syntax_tree/node.rb', line 1536 def deconstruct_keys(_keys) { key: key, value: value, location: location, comments: comments } end |
#format(q) ⇒ Object
1540 1541 1542 1543 1544 1545 1546 |
# 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 |