Sha256: b426b89643609801a1c820c27c724398617b81e04e7989e97641ba042d99bcb9

Contents?: true

Size: 1.23 KB

Versions: 82

Compression:

Stored size: 1.23 KB

Contents

# Base class for formatted textual dump of a "model"
#
class Puppet::Pops::Model::TreeDumper
  attr_accessor :indent_count
  def initialize initial_indentation = 0
    @@dump_visitor ||= Puppet::Pops::Visitor.new(nil,"dump",0,0)
    @indent_count = initial_indentation
  end

  def dump(o)
    format(do_dump(o))
  end

  def do_dump(o)
    @@dump_visitor.visit_this(self, o)
  end

  def indent
    "  " * indent_count
  end

  def format(x)
    result = ""
    parts = format_r(x)
    parts.each_index do |i|
      if i > 0
        # separate with space unless previous ends with whitepsace or (
        result << ' ' if parts[i] != ")" && parts[i-1] !~ /.*(?:\s+|\()$/ && parts[i] !~ /^\s+/
      end
      result << parts[i].to_s
    end
    result
  end

  def format_r(x)
    result = []
    case x
    when :break
      result << "\n" + indent
    when :indent
      @indent_count += 1
    when :dedent
      @indent_count -= 1
    when Array
      result << '('
      result += x.collect {|a| format_r(a) }.flatten
      result << ')'
    when Symbol
      result << x.to_s # Allows Symbols in arrays e.g. ["text", =>, "text"]
    else
      result << x
    end
    result
  end

  def is_nop? o
    o.nil? || o.is_a?(Puppet::Pops::Model::Nop)
  end
end

Version data entries

82 entries across 82 versions & 2 rubygems

Version Path
puppet-retrospec-0.12.2 vendor/gems/puppet-3.7.3/lib/puppet/pops/model/tree_dumper.rb
puppet-3.8.7 lib/puppet/pops/model/tree_dumper.rb
puppet-3.8.7-x86-mingw32 lib/puppet/pops/model/tree_dumper.rb
puppet-3.8.7-x64-mingw32 lib/puppet/pops/model/tree_dumper.rb
puppet-3.8.6 lib/puppet/pops/model/tree_dumper.rb
puppet-3.8.6-x86-mingw32 lib/puppet/pops/model/tree_dumper.rb
puppet-retrospec-0.12.1 vendor/gems/puppet-3.7.3/lib/puppet/pops/model/tree_dumper.rb
puppet-3.8.6-x64-mingw32 lib/puppet/pops/model/tree_dumper.rb
puppet-retrospec-0.12.0 vendor/gems/puppet-3.7.3/lib/puppet/pops/model/tree_dumper.rb
puppet-3.8.5 lib/puppet/pops/model/tree_dumper.rb
puppet-3.8.5-x86-mingw32 lib/puppet/pops/model/tree_dumper.rb
puppet-3.8.5-x64-mingw32 lib/puppet/pops/model/tree_dumper.rb
puppet-3.8.4 lib/puppet/pops/model/tree_dumper.rb
puppet-3.8.4-x86-mingw32 lib/puppet/pops/model/tree_dumper.rb
puppet-3.8.4-x64-mingw32 lib/puppet/pops/model/tree_dumper.rb
puppet-retrospec-0.11.0 vendor/gems/puppet-3.7.3/lib/puppet/pops/model/tree_dumper.rb
puppet-retrospec-0.10.0 vendor/gems/puppet-3.7.3/lib/puppet/pops/model/tree_dumper.rb
puppet-retrospec-0.9.1 vendor/gems/puppet-3.7.3/lib/puppet/pops/model/tree_dumper.rb
puppet-retrospec-0.9.0 vendor/gems/puppet-3.7.3/lib/puppet/pops/model/tree_dumper.rb
puppet-retrospec-0.8.1 vendor/gems/puppet-3.7.3/lib/puppet/pops/model/tree_dumper.rb