Class: SyntaxTree::DynaSymbol

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

Overview

DynaSymbol represents a symbol literal that uses quotes to dynamically define its value.

:"#{variable}"

They can also be used as a special kind of dynamic hash key, as in:

{ "#{key}": value }

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(parts:, quote:, location:) ⇒ DynaSymbol

Returns a new instance of DynaSymbol.



4674
4675
4676
4677
4678
4679
# File 'lib/syntax_tree/node.rb', line 4674

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4672
4673
4674
# File 'lib/syntax_tree/node.rb', line 4672

def comments
  @comments
end

#partsObject (readonly)

Array[ StringDVar | StringEmbExpr | TStringContent ]

the parts of the

dynamic symbol



4666
4667
4668
# File 'lib/syntax_tree/node.rb', line 4666

def parts
  @parts
end

#quoteObject (readonly)

nil | String

the quote used to delimit the dynamic symbol



4669
4670
4671
# File 'lib/syntax_tree/node.rb', line 4669

def quote
  @quote
end

Instance Method Details

#===(other) ⇒ Object



4736
4737
4738
4739
# File 'lib/syntax_tree/node.rb', line 4736

def ===(other)
  other.is_a?(DynaSymbol) && ArrayMatch.call(parts, other.parts) &&
    quote === other.quote
end

#accept(visitor) ⇒ Object



4681
4682
4683
# File 'lib/syntax_tree/node.rb', line 4681

def accept(visitor)
  visitor.visit_dyna_symbol(self)
end

#child_nodesObject Also known as: deconstruct



4685
4686
4687
# File 'lib/syntax_tree/node.rb', line 4685

def child_nodes
  parts
end

#copy(parts: nil, quote: nil, location: nil) ⇒ Object



4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
# File 'lib/syntax_tree/node.rb', line 4689

def copy(parts: nil, quote: nil, location: nil)
  node =
    DynaSymbol.new(
      parts: parts || self.parts,
      quote: quote || self.quote,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



4703
4704
4705
# File 'lib/syntax_tree/node.rb', line 4703

def deconstruct_keys(_keys)
  { parts: parts, quote: quote, location: location, comments: comments }
end

#format(q) ⇒ Object



4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
# File 'lib/syntax_tree/node.rb', line 4707

def format(q)
  opening_quote, closing_quote = quotes(q)

  q.text(opening_quote)
  q.group do
    parts.each do |part|
      if part.is_a?(TStringContent)
        value = Quotes.normalize(part.value, closing_quote)
        first = true

        value.each_line(chomp: true) do |line|
          if first
            first = false
          else
            q.breakable_return
          end

          q.text(line)
        end

        q.breakable_return if value.end_with?("\n")
      else
        q.format(part)
      end
    end
  end
  q.text(closing_quote)
end