Class: SyntaxTree::ArrayLiteral::QWordsFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Formats an array of multiple simple string literals into the %w syntax.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents) ⇒ QWordsFormatter

Returns a new instance of QWordsFormatter.



1036
1037
1038
# File 'lib/syntax_tree/node.rb', line 1036

def initialize(contents)
  @contents = contents
end

Instance Attribute Details

#contentsObject (readonly)

Args

the contents of the array



1034
1035
1036
# File 'lib/syntax_tree/node.rb', line 1034

def contents
  @contents
end

Instance Method Details

#format(q) ⇒ Object



1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
# File 'lib/syntax_tree/node.rb', line 1040

def format(q)
  q.text("%w[")
  q.group do
    q.indent do
      q.breakable_empty
      q.seplist(contents.parts, BREAKABLE_SPACE_SEPARATOR) do |part|
        if part.is_a?(StringLiteral)
          q.format(part.parts.first)
        else
          q.text(part.value[1..])
        end
      end
    end
    q.breakable_empty
  end
  q.text("]")
end