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.



5157
5158
5159
5160
5161
5162
# File 'lib/syntax_tree.rb', line 5157

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



5155
5156
5157
# File 'lib/syntax_tree.rb', line 5155

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



5152
5153
5154
# File 'lib/syntax_tree.rb', line 5152

def location
  @location
end

#partsObject (readonly)

Array[ StringDVar | StringEmbExpr | TStringContent ]

the parts of the

dynamic symbol



5146
5147
5148
# File 'lib/syntax_tree.rb', line 5146

def parts
  @parts
end

#quoteObject (readonly)

String

the quote used to delimit the dynamic symbol



5149
5150
5151
# File 'lib/syntax_tree.rb', line 5149

def quote
  @quote
end

Instance Method Details

#child_nodesObject



5164
5165
5166
# File 'lib/syntax_tree.rb', line 5164

def child_nodes
  parts
end

#format(q) ⇒ Object



5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
# File 'lib/syntax_tree.rb', line 5168

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



5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
# File 'lib/syntax_tree.rb', line 5186

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



5197
5198
5199
5200
5201
5202
5203
5204
5205
# File 'lib/syntax_tree.rb', line 5197

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