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, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of DynaSymbol.



3917
3918
3919
3920
3921
3922
# File 'lib/syntax_tree/node.rb', line 3917

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

Instance Attribute Details

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



3915
3916
3917
# File 'lib/syntax_tree/node.rb', line 3915

def comments
  @comments
end

#parts ⇒ Object (readonly)

[Array[ StringDVar | StringEmbExpr | TStringContent ]] the parts of the dynamic symbol



3909
3910
3911
# File 'lib/syntax_tree/node.rb', line 3909

def parts
  @parts
end

#quote ⇒ Object (readonly)

[String] the quote used to delimit the dynamic symbol



3912
3913
3914
# File 'lib/syntax_tree/node.rb', line 3912

def quote
  @quote
end

Instance Method Details

#accept(visitor) ⇒ Object



3924
3925
3926
# File 'lib/syntax_tree/node.rb', line 3924

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

#child_nodes ⇒ Object Also known as: deconstruct



3928
3929
3930
# File 'lib/syntax_tree/node.rb', line 3928

def child_nodes
  parts
end

#deconstruct_keys(_keys) ⇒ Object



3934
3935
3936
# File 'lib/syntax_tree/node.rb', line 3934

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

#format(q) ⇒ Object



3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
# File 'lib/syntax_tree/node.rb', line 3938

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