Class: Aptible::CLI::Renderer::Text

Inherits:
Base
  • Object
show all
Includes:
ActiveSupport::Inflector
Defined in:
lib/aptible/cli/renderer/text.rb

Constant Summary collapse

POST_PROCESSED_KEYS =
{
  'Tls' => 'TLS',
  'Dns' => 'DNS',
  'Ip' => 'IP'
}.freeze

Instance Method Summary collapse

Instance Method Details

#render(node) ⇒ Object



59
60
61
62
63
64
# File 'lib/aptible/cli/renderer/text.rb', line 59

def render(node)
  io = StringIO.new
  visit(node, io)
  io.rewind
  io.read
end

#visit(node, io) ⇒ Object



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
# File 'lib/aptible/cli/renderer/text.rb', line 13

def visit(node, io)
  case node
  when Formatter::Root
    visit(node.root, io)
  when Formatter::KeyedObject
    visit(node.children.fetch(node.key), io)
  when Formatter::Object
    # TODO: We should have a way to fail in tests if we're going to
    # nest an object under another object (or at least handle it
    # decently).
    #
    # Right now, it provides unusable output like this:
    #
    # Foo: Bar: bar
    # Qux: qux
    #
    # (when rendering { foo => { bar => bar }, qux => qux })
    #
    # The solution to this problem is typically to make sure the
    # children are KeyedObject instances so they can render properly,
    # but we need to warn in tests that this is required.
    node.children.each_pair do |k, c|
      io.print "#{format_key(k)}: "
      visit(c, io)
    end
  when Formatter::GroupedKeyedList
    enum = spacer_enumerator
    node.groups.each_pair.sort_by(&:first).each do |key, group|
      io.print enum.next
      io.print '=== '
      nodes = group.map { |n| n.children.fetch(node.key) }
      visit(key, io)
      output_list(nodes, io)
    end
  when Formatter::KeyedList
    nodes = node.children.map { |n| n.children.fetch(node.key) }
    output_list(nodes, io)
  when Formatter::List
    output_list(node.children, io)
  when Formatter::Value
    io.puts node.value
  else
    raise "Unhandled node: #{node.inspect}"
  end
end