Class: Puppet::Pops::Model::TreeDumper
- Defined in:
- lib/puppet/pops/model/tree_dumper.rb
Overview
Base class for formatted textual dump of a “model”
Direct Known Subclasses
Instance Attribute Summary collapse
Instance Method Summary collapse
- #do_dump(o) ⇒ Object
- #dump(o) ⇒ Object
- #format(x) ⇒ Object
- #format_r(x) ⇒ Object
- #indent ⇒ Object
-
#initialize(initial_indentation = 0) ⇒ TreeDumper
constructor
A new instance of TreeDumper.
- #is_nop?(o) ⇒ Boolean
Constructor Details
#initialize(initial_indentation = 0) ⇒ TreeDumper
Returns a new instance of TreeDumper.
6 7 8 9 |
# File 'lib/puppet/pops/model/tree_dumper.rb', line 6 def initialize initial_indentation = 0 @@dump_visitor ||= Puppet::Pops::Visitor.new(nil,"dump",0,0) @indent_count = initial_indentation end |
Instance Attribute Details
#indent_count ⇒ Object
5 6 7 |
# File 'lib/puppet/pops/model/tree_dumper.rb', line 5 def indent_count @indent_count end |
Instance Method Details
#do_dump(o) ⇒ Object
15 16 17 |
# File 'lib/puppet/pops/model/tree_dumper.rb', line 15 def do_dump(o) @@dump_visitor.visit_this_0(self, o) end |
#dump(o) ⇒ Object
11 12 13 |
# File 'lib/puppet/pops/model/tree_dumper.rb', line 11 def dump(o) format(do_dump(o)) end |
#format(x) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/puppet/pops/model/tree_dumper.rb', line 23 def format(x) result = ''.dup parts = format_r(x) parts.each_index do |i| if i > 0 # separate with space unless previous ends with whitespace or ( result << ' ' if parts[i] != ")" && parts[i-1] !~ /.*(?:\s+|\()$/ && parts[i] !~ /^\s+/ end result << parts[i].to_s end result end |
#format_r(x) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/puppet/pops/model/tree_dumper.rb', line 36 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 |
#indent ⇒ Object
19 20 21 |
# File 'lib/puppet/pops/model/tree_dumper.rb', line 19 def indent " " * indent_count end |