Class: ConfigToolkit::PrettyPrintWriter::Visitor

Inherits:
HashArrayVisitor show all
Defined in:
lib/configtoolkit/prettyprintwriter.rb

Overview

This class is used for printing the elements of the structure passed to write. See the comments for HashArrayVisitor for details on how this visitor works.

Defined Under Namespace

Classes: StackFrame

Constant Summary collapse

NESTING_INDENT =

The amount by which to indent for each nesting level when pretty printing.

" " * 4

Instance Method Summary collapse

Methods inherited from HashArrayVisitor

#stack_empty?, #stack_top, #visit

Constructor Details

#initialize(stream) ⇒ Visitor

Description:

This constructs a Visitor to pretty pretty structures to stream stream.

Parameters:

stream

The stream to which to write



57
58
59
60
61
# File 'lib/configtoolkit/prettyprintwriter.rb', line 57

def initialize(stream)
  @stream = stream

  super(StackFrame.new("", 0))
end

Instance Method Details

#construct_new_stack_frame(longest_param_name_length) ⇒ Object

Description:

This method returns a new stack frame with longest parameter name length longest_param_name_length. The indentation for the new frame automatically is constructed from the current indentation.

Parameters:

longest_param_name_length

The length of the longest parameter name in the container associated with the frame

Returns:

A new stack frame



78
79
80
81
82
# File 'lib/configtoolkit/prettyprintwriter.rb', line 78

def construct_new_stack_frame(longest_param_name_length)
  indent = stack_top().indent + NESTING_INDENT

  return StackFrame.new(indent, longest_param_name_length)
end

#enter_array(array) ⇒ Object



112
113
114
115
116
# File 'lib/configtoolkit/prettyprintwriter.rb', line 112

def enter_array(array)
  @stream << "["

  return construct_new_stack_frame(stack_top().longest_param_name_length)
end

#enter_hash(hash) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/configtoolkit/prettyprintwriter.rb', line 84

def enter_hash(hash)
  @stream << "{"
  
  longest_param_name_length = 0
  hash.each_key do |key|
    key_size = key.to_s().size()
    if(key_size > longest_param_name_length)
      longest_param_name_length = key_size
    end
  end
  
  return construct_new_stack_frame(longest_param_name_length)
end

#leave_array(popped_stack_frame) ⇒ Object



130
131
132
# File 'lib/configtoolkit/prettyprintwriter.rb', line 130

def leave_array(popped_stack_frame)
  @stream << "\n#{stack_top.indent}" << "]"
end

#leave_hash(popped_stack_frame) ⇒ Object



98
99
100
# File 'lib/configtoolkit/prettyprintwriter.rb', line 98

def leave_hash(popped_stack_frame)
  @stream << "\n#{stack_top.indent}" << "}"
end

#visit_array_element(value, index, is_container) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/configtoolkit/prettyprintwriter.rb', line 118

def visit_array_element(value, index, is_container)
  if(index != 0)
    @stream << ","
  end

  @stream << "\n#{stack_top.indent}"

  if(!is_container)
    @stream << "#{value}"
  end
end

#visit_hash_element(key, value, index, is_container) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/configtoolkit/prettyprintwriter.rb', line 102

def visit_hash_element(key, value, index, is_container)
  @stream << "\n#{stack_top.indent}"
  @stream << key.to_s.ljust(stack_top.longest_param_name_length)
  @stream << ": "
  
  if(!is_container)
    @stream << "#{value}"
  end
end