Class: SyntaxTree::Assoc
- Inherits:
-
Object
- Object
- SyntaxTree::Assoc
- Defined in:
- lib/syntax_tree.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
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#key ⇒ Object
readonly
- untyped
-
the key of this pair.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#value ⇒ Object
readonly
- untyped
-
the value of this pair.
Instance Method Summary collapse
- #child_nodes ⇒ 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.
1759 1760 1761 1762 1763 1764 |
# File 'lib/syntax_tree.rb', line 1759 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
1757 1758 1759 |
# File 'lib/syntax_tree.rb', line 1757 def comments @comments end |
#key ⇒ Object (readonly)
- untyped
-
the key of this pair
1748 1749 1750 |
# File 'lib/syntax_tree.rb', line 1748 def key @key end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
1754 1755 1756 |
# File 'lib/syntax_tree.rb', line 1754 def location @location end |
#value ⇒ Object (readonly)
- untyped
-
the value of this pair
1751 1752 1753 |
# File 'lib/syntax_tree.rb', line 1751 def value @value end |
Instance Method Details
#child_nodes ⇒ Object
1766 1767 1768 |
# File 'lib/syntax_tree.rb', line 1766 def child_nodes [key, value] end |
#format(q) ⇒ Object
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 |
# File 'lib/syntax_tree.rb', line 1770 def format(q) contents = -> do q.parent.format_key(q, key) q.indent do q.breakable q.format(value) end end value.is_a?(HashLiteral) ? contents.call : q.group(&contents) end |
#pretty_print(q) ⇒ Object
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 |
# File 'lib/syntax_tree.rb', line 1782 def pretty_print(q) q.group(2, "(", ")") do q.text("assoc") q.breakable q.pp(key) q.breakable q.pp(value) q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
1796 1797 1798 1799 1800 1801 1802 1803 1804 |
# File 'lib/syntax_tree.rb', line 1796 def to_json(*opts) { type: :assoc, key: key, value: value, loc: location, cmts: comments }.to_json(*opts) end |