Class: SyntaxTree::BareAssocHash

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

BareAssocHash represents a hash of contents being passed as a method argument (and therefore has omitted braces). It’s very similar to an AssocListFromArgs node.

method(key1: value1, key2: value2)

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

#initialize(assocs:, location:, comments: []) ⇒ BareAssocHash

Returns a new instance of BareAssocHash.



1676
1677
1678
1679
1680
# File 'lib/syntax_tree/node.rb', line 1676

def initialize(assocs:, location:, comments: [])
  @assocs = assocs
  @location = location
  @comments = comments
end

Instance Attribute Details

#assocsObject (readonly)

Array[ AssocNew | AssocSplat ]


1671
1672
1673
# File 'lib/syntax_tree/node.rb', line 1671

def assocs
  @assocs
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1674
1675
1676
# File 'lib/syntax_tree/node.rb', line 1674

def comments
  @comments
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



1682
1683
1684
# File 'lib/syntax_tree/node.rb', line 1682

def child_nodes
  assocs
end

#deconstruct_keys(keys) ⇒ Object



1688
1689
1690
# File 'lib/syntax_tree/node.rb', line 1688

def deconstruct_keys(keys)
  { assocs: assocs, location: location, comments: comments }
end

#format(q) ⇒ Object



1692
1693
1694
# File 'lib/syntax_tree/node.rb', line 1692

def format(q)
  q.seplist(assocs) { |assoc| q.format(assoc) }
end

#format_key(q, key) ⇒ Object



1696
1697
1698
# File 'lib/syntax_tree/node.rb', line 1696

def format_key(q, key)
  (@key_formatter ||= HashKeyFormatter.for(self)).format_key(q, key)
end

#pretty_print(q) ⇒ Object



1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
# File 'lib/syntax_tree/node.rb', line 1700

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("bare_assoc_hash")

    q.breakable
    q.group(2, "(", ")") { q.seplist(assocs) { |assoc| q.pp(assoc) } }

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



1711
1712
1713
1714
1715
1716
1717
1718
# File 'lib/syntax_tree/node.rb', line 1711

def to_json(*opts)
  {
    type: :bare_assoc_hash,
    assocs: assocs,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end