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.



5120
5121
5122
5123
5124
5125
# File 'lib/syntax_tree.rb', line 5120

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



5118
5119
5120
# File 'lib/syntax_tree.rb', line 5118

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



5115
5116
5117
# File 'lib/syntax_tree.rb', line 5115

def location
  @location
end

#partsObject (readonly)

Array[ StringDVar | StringEmbExpr | TStringContent ]

the parts of the

dynamic symbol



5109
5110
5111
# File 'lib/syntax_tree.rb', line 5109

def parts
  @parts
end

#quoteObject (readonly)

String

the quote used to delimit the dynamic symbol



5112
5113
5114
# File 'lib/syntax_tree.rb', line 5112

def quote
  @quote
end

Instance Method Details

#child_nodesObject



5127
5128
5129
# File 'lib/syntax_tree.rb', line 5127

def child_nodes
  parts
end

#format(q) ⇒ Object



5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
# File 'lib/syntax_tree.rb', line 5131

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



5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
# File 'lib/syntax_tree.rb', line 5149

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



5160
5161
5162
5163
5164
5165
5166
5167
5168
# File 'lib/syntax_tree.rb', line 5160

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