Class: Debug::DataModel::PrettyPrintStreamer

Inherits:
Object
  • Object
show all
Defined in:
lib/moon/packages/debug/data_model.rb

Overview

Streamer object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, from = nil) ⇒ PrettyPrintStreamer

Returns a new instance of PrettyPrintStreamer.


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/moon/packages/debug/data_model.rb', line 47

def initialize(stream, from = nil)
  @stream = stream
  @indent_level = 0
  @comment_delimiter = '#'
  @indent_delimiter = "\s\s"
  @list_delimiter = ','
  if from
    @comment_delimiter = from.comment_delimiter
    @indent_delimiter = from.indent_delimiter
    @list_delimiter = from.list_delimiter
  end
end

Instance Attribute Details

#comment_delimiterObject

Returns the value of attribute comment_delimiter


43
44
45
# File 'lib/moon/packages/debug/data_model.rb', line 43

def comment_delimiter
  @comment_delimiter
end

#indent_delimiterObject

Returns the value of attribute indent_delimiter


44
45
46
# File 'lib/moon/packages/debug/data_model.rb', line 44

def indent_delimiter
  @indent_delimiter
end

#indent_levelObject

Returns the value of attribute indent_level


42
43
44
# File 'lib/moon/packages/debug/data_model.rb', line 42

def indent_level
  @indent_level
end

#list_delimiterObject

Returns the value of attribute list_delimiter


45
46
47
# File 'lib/moon/packages/debug/data_model.rb', line 45

def list_delimiter
  @list_delimiter
end

Instance Method Details

#endlObject


64
65
66
# File 'lib/moon/packages/debug/data_model.rb', line 64

def endl
  "\n"
end

#indentObject


72
73
74
75
76
77
78
# File 'lib/moon/packages/debug/data_model.rb', line 72

def indent
  @indent_level += 1
  if block_given?
    yield
    unindent
  end
end

#new(s = stream) ⇒ Object


60
61
62
# File 'lib/moon/packages/debug/data_model.rb', line 60

def new(s = stream)
  self.class.new(s, self)
end

#unindentObject


68
69
70
# File 'lib/moon/packages/debug/data_model.rb', line 68

def unindent
  @indent_level = (@indent_level - 1).max(0)
end

#write(str) ⇒ Object


84
85
86
# File 'lib/moon/packages/debug/data_model.rb', line 84

def write(str)
  @stream.write str
end

#write_array(array, depth = 0) ⇒ Object


129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/moon/packages/debug/data_model.rb', line 129

def write_array(array, depth = 0)
  write_indented '[ '
  write_comment_line 'size: ' << array.size.to_s
  indent do
    array.each do |obj|
      write_object obj, depth.succ
      write @list_delimiter
      write_endl depth.succ
    end
  end
  write_indented ']'
end

#write_comment_line(str, depth = 0) ⇒ Object


106
107
108
109
# File 'lib/moon/packages/debug/data_model.rb', line 106

def write_comment_line(str, depth = 0)
  write @comment_delimiter
  write_line ' ' << str
end

#write_endl(depth = 0) ⇒ Object


97
98
99
# File 'lib/moon/packages/debug/data_model.rb', line 97

def write_endl(depth = 0)
  write endl
end

#write_field(field, key, value, depth = 0) ⇒ Object


111
112
113
114
115
# File 'lib/moon/packages/debug/data_model.rb', line 111

def write_field(field, key, value, depth = 0)
  write_indented '' << key.to_s << ': ' <<
    PrettyFormat.object_type(field.type) << ' = '
  write_object(value)
end

#write_hash(hash, depth = 0) ⇒ Object


142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/moon/packages/debug/data_model.rb', line 142

def write_hash(hash, depth = 0)
  write_indented '{ '
  write_comment_line 'size: ' << hash.size.to_s
  indent do
    hash.each do |key, value|
      write_object key, depth.succ
      write ' => '
      write_object obj, depth.succ
      write @list_delimiter
      write_endl depth.succ
    end
  end
  write_indented '}'
end

#write_indent(depth = 0) ⇒ Object


88
89
90
# File 'lib/moon/packages/debug/data_model.rb', line 88

def write_indent(depth = 0)
  write indent_str
end

#write_indented(str, depth = 0) ⇒ Object


92
93
94
95
# File 'lib/moon/packages/debug/data_model.rb', line 92

def write_indented(str, depth = 0)
  write_indent depth
  write str
end

#write_line(str, depth = 0) ⇒ Object


101
102
103
104
# File 'lib/moon/packages/debug/data_model.rb', line 101

def write_line(str, depth = 0)
  write str
  write_endl depth
end

#write_model(model, depth = 0) ⇒ Object


117
118
119
120
121
122
123
124
125
126
127
# File 'lib/moon/packages/debug/data_model.rb', line 117

def write_model(model, depth = 0)
  write_indented 'model ' << model.class.inspect << ' {' << endl
  indent do
    model.each_field_with_value do |key, field, value|
      write_field(field, key, value)
      write @list_delimiter
      write_endl
    end
  end
  write_indented '}'
end

#write_object(obj, depth = 0) ⇒ Object


157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/moon/packages/debug/data_model.rb', line 157

def write_object(obj, depth = 0)
  case obj
  when Array
    write_array obj, depth.succ
  when Hash
    write_hash obj, depth.succ
  when Moon::DataModel::Model
    write_model obj, depth.succ
  else
    write obj.inspect
  end
end