Class: SyntaxTree::HashLiteral
Overview
HashLiteral represents a hash literal.
{ key => value }
Defined Under Namespace
Classes: EmptyWithCommentsFormatter
Instance Attribute Summary collapse
-
#assocs ⇒ Object
readonly
[Array[ Assoc | AssocSplat ]] the optional contents of the hash.
-
#comments ⇒ Object
readonly
[Array[ Comment | EmbDoc ]] the comments attached to this node.
-
#lbrace ⇒ Object
readonly
[LBrace] the left brace that opens this hash.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(lbrace: nil, assocs: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
- #format_key(q, key) ⇒ Object
-
#initialize(lbrace:, assocs:, location:) ⇒ HashLiteral
constructor
A new instance of HashLiteral.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(lbrace:, assocs:, location:) ⇒ HashLiteral
Returns a new instance of HashLiteral.
5588 5589 5590 5591 5592 5593 |
# File 'lib/syntax_tree/node.rb', line 5588 def initialize(lbrace:, assocs:, location:) @lbrace = lbrace @assocs = assocs @location = location @comments = [] end |
Instance Attribute Details
#assocs ⇒ Object (readonly)
[Array[ Assoc | AssocSplat ]] the optional contents of the hash
5583 5584 5585 |
# File 'lib/syntax_tree/node.rb', line 5583 def assocs @assocs end |
#comments ⇒ Object (readonly)
[Array[ Comment | EmbDoc ]] the comments attached to this node
5586 5587 5588 |
# File 'lib/syntax_tree/node.rb', line 5586 def comments @comments end |
#lbrace ⇒ Object (readonly)
[LBrace] the left brace that opens this hash
5580 5581 5582 |
# File 'lib/syntax_tree/node.rb', line 5580 def lbrace @lbrace end |
Instance Method Details
#===(other) ⇒ Object
5629 5630 5631 5632 |
# File 'lib/syntax_tree/node.rb', line 5629 def ===(other) other.is_a?(HashLiteral) && lbrace === other.lbrace && ArrayMatch.call(assocs, other.assocs) end |
#accept(visitor) ⇒ Object
5595 5596 5597 |
# File 'lib/syntax_tree/node.rb', line 5595 def accept(visitor) visitor.visit_hash(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
5599 5600 5601 |
# File 'lib/syntax_tree/node.rb', line 5599 def child_nodes [lbrace] + assocs end |
#copy(lbrace: nil, assocs: nil, location: nil) ⇒ Object
5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 |
# File 'lib/syntax_tree/node.rb', line 5603 def copy(lbrace: nil, assocs: nil, location: nil) node = HashLiteral.new( lbrace: lbrace || self.lbrace, assocs: assocs || self.assocs, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
5617 5618 5619 |
# File 'lib/syntax_tree/node.rb', line 5617 def deconstruct_keys(_keys) { lbrace: lbrace, assocs: assocs, location: location, comments: comments } end |
#format(q) ⇒ Object
5621 5622 5623 5624 5625 5626 5627 |
# File 'lib/syntax_tree/node.rb', line 5621 def format(q) if q.parent.is_a?(Assoc) format_contents(q) else q.group { format_contents(q) } end end |
#format_key(q, key) ⇒ Object
5634 5635 5636 |
# File 'lib/syntax_tree/node.rb', line 5634 def format_key(q, key) (@key_formatter ||= HashKeyFormatter.for(self)).format_key(q, key) end |