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.
1869 1870 1871 1872 1873 1874 |
# File 'lib/syntax_tree.rb', line 1869 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
1867 1868 1869 |
# File 'lib/syntax_tree.rb', line 1867 def comments @comments end |
#key ⇒ Object (readonly)
- untyped
-
the key of this pair
1858 1859 1860 |
# File 'lib/syntax_tree.rb', line 1858 def key @key end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
1864 1865 1866 |
# File 'lib/syntax_tree.rb', line 1864 def location @location end |
#value ⇒ Object (readonly)
- untyped
-
the value of this pair
1861 1862 1863 |
# File 'lib/syntax_tree.rb', line 1861 def value @value end |
Instance Method Details
#child_nodes ⇒ Object
1876 1877 1878 |
# File 'lib/syntax_tree.rb', line 1876 def child_nodes [key, value] end |
#format(q) ⇒ Object
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 |
# File 'lib/syntax_tree.rb', line 1880 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
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 |
# File 'lib/syntax_tree.rb', line 1892 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
1906 1907 1908 1909 1910 1911 1912 1913 1914 |
# File 'lib/syntax_tree.rb', line 1906 def to_json(*opts) { type: :assoc, key: key, value: value, loc: location, cmts: comments }.to_json(*opts) end |