File: printer.rb

package info (click to toggle)
ruby-morpher 0.2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 520 kB
  • sloc: ruby: 2,366; makefile: 4
file content (233 lines) | stat: -rw-r--r-- 4,384 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
module Morpher

  # Evaluation and Evaluator pretty printer
  class Printer
    include Adamantium::Flat, Concord.new(:object, :output, :indent_level)

    INDENT = '  '.freeze

    REGISTRY = {}

    # Run pretty printer on object
    #
    # @param [Object] object
    #   the object to be pretty printed
    # @param [IO] output
    #   the output to write to
    # @param [Fixnum] indent_level
    #   the current indentation level
    #
    # @return [self]
    #
    # @api private
    #
    def self.run(object, output, indent_level = 0)
      printer = new(object, output, indent_level)
      block = lookup(object)
      printer.instance_eval(&block)
    end

    # Perform type lookup
    #
    # FIXME: Instanciate type lookup once and allow caching.
    #
    # @param [Evaluation, Evaluator] object
    #
    # @return [Proc]
    #   if found
    #
    # @raise [PrinterMissingException]
    #   otherwise
    #
    # @api private
    #
    def self.lookup(object)
      TypeLookup.new(REGISTRY).call(object)
    end

  private

    # Visit a child
    #
    # @param [Node] child
    #
    # @api private
    #
    # @return [undefined]
    #
    def visit_child(child)
      self.class.run(child, output, indent_level.succ)
    end

    # Visit a child by name
    #
    # @param [Symbol] name
    #   the attribute name of the child to visit
    #
    # @return [undefined]
    #
    # @api private
    #
    def visit(name)
      child = object.public_send(name)
      child_label(name)
      visit_child(child)
    end

    # Visit many children
    #
    # @param [Symbol] name
    #   the name of the collection attribute with children to visit
    #
    # @return [undefined]
    #
    # @api private
    #
    def visit_many(name)
      children = object.public_send(name)
      child_label(name)
      children.each do |child|
        visit_child(child)
      end
    end

    # Print attribute class
    #
    # @param [Symbol] name
    #
    # @return [undefined]
    #
    # @api private
    #
    def attribute_class(name)
      label_value(name, object.public_send(name).class)
    end

    # Print inspected attribute value with label
    #
    # @return [undefined]
    #
    # @api private
    #
    def attribute(name)
      label_value(name, object.public_send(name))
    end

    # Print attributes of object
    #
    # @return [undefined]
    #
    # @api private
    #
    def attributes(*names)
      names.each do |name|
        attribute(name)
      end
    end

    # Print name of object
    #
    # @return [undefined]
    #
    # @api private
    #
    def name
      puts(object.class.name)
    end

    # Return string indented with current level
    #
    # @param [String] content
    #
    # @return [String]
    #
    # @api private
    #
    def indented(content)
      "#{indentation_prefix}#{content}"
    end

    # Return indentation prefix
    #
    # @return [String]
    #
    # @api private
    #
    def indentation_prefix
      INDENT * indent_level
    end
    memoize :indentation_prefix

    # Add content to output at current indentation and close line
    #
    # @param [String] content
    #
    # @return [undefined]
    #
    # @api private
    #
    def puts(string)
      output.puts(indented(string))
    end

    # Write content to output at current indentation
    #
    # @param [String] content
    #
    # @return [undefined]
    #
    # @api private
    #
    def write(string)
      output.write(indented(string))
    end

    # Write child label to output at current indentation
    #
    # @param [String] label
    #
    # @return [undefined]
    #
    # @api private
    #
    def child_label(string)
      puts("#{string}:")
    end

    # Call block inside indented context
    #
    # @return [undefined]
    #
    # @api private
    #
    def indent(&block)
      printer = new(object, output, indent_level.succ)
      printer.instance_eval(&block)
    end

    # Return new printer
    #
    # @return [Printer]
    #
    # @api private
    #
    def new(*arguments)
      self.class.new(*arguments)
    end

    # Print label with value
    #
    # @param [String] label
    # @param [Object] value
    #
    # @return [undefined]
    #
    # @api private
    #
    def label_value(label, value)
      write("#{label}: ")
      output.puts(value.inspect)
    end

  end # Printer
end # Morpher