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.



824
825
826
# File 'lib/syntax_tree/node.rb', line 824

def initialize(contents)
  @contents = contents
end

Instance Attribute Details

#contentsObject (readonly)

Args

the contents of the array



822
823
824
# File 'lib/syntax_tree/node.rb', line 822

def contents
  @contents
end

Instance Method Details

#format(q) ⇒ Object



828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
# File 'lib/syntax_tree/node.rb', line 828

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