Class: Aws2MD::HorizontalTable

Inherits:
Table
  • Object
show all
Defined in:
lib/aws2md/table.rb

Instance Method Summary collapse

Methods inherited from Table

#scalar?, #separate_keys

Constructor Details

#initialize(json) ⇒ HorizontalTable

Returns a new instance of HorizontalTable.



38
39
40
# File 'lib/aws2md/table.rb', line 38

def initialize(json)
  @json = json
end

Instance Method Details

#build_table(title, current, level = 0) ⇒ Object



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
# File 'lib/aws2md/table.rb', line 62

def build_table(title, current, level = 0)
  unless title.nil?
    puts ""
    heading = "#" * level
    puts "#{heading} #{title}"
  end

  if current.is_a?(Hash)
    # Keys with scalar values will be used as table columns
    # Other keys will be passed to build_table for recursive processing
    header_keys, more_keys = separate_keys(current)

    unless header_keys.empty?
      rows = []
      header_keys.each do |key|
        rows << current[key]
      end

      print_table_for_hash(header_keys, rows)
    end

    more_keys.each do |key|
      build_table(key, current[key], level + 1)
    end
  else current.is_a?(Array)
    current.each_with_index do |hash, i|
      header_keys, more_keys = separate_keys(hash)

      rows = []
      header_keys.each do |key|
        rows << hash[key]
      end

      print_table_for_array(header_keys, rows, level, title, i)

      more_keys.each do |key|
        build_table(key, hash[key], level + 2)
      end
    end
  end
end


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/aws2md/table.rb', line 50

def print_table_for_array(header_keys, rows, level, title, i)
  table = Terminal::Table.new :rows => [rows]
  table.headings = header_keys
  table.style = { :border => :markdown }

  heading = "#" * (level + 1)

  puts ""
  puts "#{heading} #{title.singularize}.#{i}"
  puts table
end


42
43
44
45
46
47
48
# File 'lib/aws2md/table.rb', line 42

def print_table_for_hash(header_keys, rows)
  table = Terminal::Table.new :rows => [rows]
  table.headings = header_keys
  table.style = { :border => :markdown }
  puts ""
  puts table
end

#runObject



104
105
106
# File 'lib/aws2md/table.rb', line 104

def run
  build_table(nil, @json)
end