Class: SyntaxTree::DynaSymbol

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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

Instance Method Summary collapse

Constructor Details

#initialize(parts:, quote:, location:, comments: []) ⇒ DynaSymbol

Returns a new instance of DynaSymbol.



4824
4825
4826
4827
4828
4829
# File 'lib/syntax_tree.rb', line 4824

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4822
4823
4824
# File 'lib/syntax_tree.rb', line 4822

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4819
4820
4821
# File 'lib/syntax_tree.rb', line 4819

def location
  @location
end

#partsObject (readonly)

Array[ StringDVar | StringEmbExpr | TStringContent ]

the parts of the

dynamic symbol



4813
4814
4815
# File 'lib/syntax_tree.rb', line 4813

def parts
  @parts
end

#quoteObject (readonly)

String

the quote used to delimit the dynamic symbol



4816
4817
4818
# File 'lib/syntax_tree.rb', line 4816

def quote
  @quote
end

Instance Method Details

#child_nodesObject



4831
4832
4833
# File 'lib/syntax_tree.rb', line 4831

def child_nodes
  parts
end

#format(q) ⇒ Object



4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
# File 'lib/syntax_tree.rb', line 4835

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

  q.group(0, opening_quote, closing_quote) do
    parts.each do |part|
      if part.is_a?(TStringContent)
        value = Quotes.normalize(part.value, closing_quote)
        separator = -> { q.breakable(force: true, indent: false) }
        q.seplist(value.split(/\r?\n/, -1), separator) do |text|
          q.text(text)
        end
      else
        q.format(part)
      end
    end
  end
end

#pretty_print(q) ⇒ Object



4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
# File 'lib/syntax_tree.rb', line 4853

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

    q.breakable
    q.group(2, "(", ")") { q.seplist(parts) { |part| q.pp(part) } }

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

#to_json(*opts) ⇒ Object



4864
4865
4866
4867
4868
4869
4870
4871
4872
# File 'lib/syntax_tree.rb', line 4864

def to_json(*opts)
  {
    type: :dyna_symbol,
    parts: parts,
    quote: quote,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end