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.



4053
4054
4055
4056
4057
4058
# File 'lib/syntax_tree/node.rb', line 4053

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



4051
4052
4053
# File 'lib/syntax_tree/node.rb', line 4051

def comments
  @comments
end

#partsObject (readonly)

Array[ StringDVar | StringEmbExpr | TStringContent ]

the parts of the

dynamic symbol



4045
4046
4047
# File 'lib/syntax_tree/node.rb', line 4045

def parts
  @parts
end

#quoteObject (readonly)

String

the quote used to delimit the dynamic symbol



4048
4049
4050
# File 'lib/syntax_tree/node.rb', line 4048

def quote
  @quote
end

Instance Method Details

#accept(visitor) ⇒ Object



4060
4061
4062
# File 'lib/syntax_tree/node.rb', line 4060

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

#child_nodesObject Also known as: deconstruct



4064
4065
4066
# File 'lib/syntax_tree/node.rb', line 4064

def child_nodes
  parts
end

#deconstruct_keys(_keys) ⇒ Object



4070
4071
4072
# File 'lib/syntax_tree/node.rb', line 4070

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

#format(q) ⇒ Object



4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
# File 'lib/syntax_tree/node.rb', line 4074

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

  q.text(opening_quote)
  q.group do
    parts.each do |part|
      if part.is_a?(TStringContent)
        value = Quotes.normalize(part.value, closing_quote)
        first = true

        value.each_line(chomp: true) do |line|
          if first
            first = false
          else
            q.breakable_return
          end

          q.text(line)
        end

        q.breakable_return if value.end_with?("\n")
      else
        q.format(part)
      end
    end
  end
  q.text(closing_quote)
end