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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of DynaSymbol.



3496
3497
3498
3499
3500
3501
# File 'lib/syntax_tree/node.rb', line 3496

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



3494
3495
3496
# File 'lib/syntax_tree/node.rb', line 3494

def comments
  @comments
end

#partsObject (readonly)

Array[ StringDVar | StringEmbExpr | TStringContent ]

the parts of the

dynamic symbol



3488
3489
3490
# File 'lib/syntax_tree/node.rb', line 3488

def parts
  @parts
end

#quoteObject (readonly)

String

the quote used to delimit the dynamic symbol



3491
3492
3493
# File 'lib/syntax_tree/node.rb', line 3491

def quote
  @quote
end

Instance Method Details

#accept(visitor) ⇒ Object



3503
3504
3505
# File 'lib/syntax_tree/node.rb', line 3503

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

#child_nodesObject Also known as: deconstruct



3507
3508
3509
# File 'lib/syntax_tree/node.rb', line 3507

def child_nodes
  parts
end

#deconstruct_keys(keys) ⇒ Object



3513
3514
3515
# File 'lib/syntax_tree/node.rb', line 3513

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

#format(q) ⇒ Object



3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
# File 'lib/syntax_tree/node.rb', line 3517

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