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.



778
779
780
# File 'lib/syntax_tree/node.rb', line 778

def initialize(contents)
  @contents = contents
end

Instance Attribute Details

#contentsObject (readonly)

Args

the contents of the array



776
777
778
# File 'lib/syntax_tree/node.rb', line 776

def contents
  @contents
end

Instance Method Details

#format(q) ⇒ Object



782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
# File 'lib/syntax_tree/node.rb', line 782

def format(q)
  q.group(0, "%w[", "]") do
    q.indent do
      q.breakable("")
      q.seplist(contents.parts, -> { q.breakable }) do |part|
        if part.is_a?(StringLiteral)
          q.format(part.parts.first)
        else
          q.text(part.value[1..])
        end
      end
    end
    q.breakable("")
  end
end