Class: Aws2MD::VerticalTable

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) ⇒ VerticalTable

Returns a new instance of VerticalTable.



110
111
112
# File 'lib/aws2md/table.rb', line 110

def initialize(json)
  @json = json
end

Instance Method Details

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



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

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

  if current.is_a?(Hash)
    header_keys, more_keys = separate_keys(current)

    unless header_keys.empty?
      rows = header_to_rows(header_keys, current)
      print_table_for_hash(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_to_rows(header_keys, hash)

      print_table_for_array(rows, title, level, i)

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

#header_to_rows(header_keys, hash) ⇒ Object



165
166
167
# File 'lib/aws2md/table.rb', line 165

def header_to_rows(header_keys, hash)
  header_keys.map { |key| [key, hash[key]] }
end


122
123
124
125
126
127
128
129
130
131
# File 'lib/aws2md/table.rb', line 122

def print_table_for_array(rows, title, level, i)
  table = Terminal::Table.new :rows => rows
  table.headings = ["Key", "Value"]
  table.style = { :border => :markdown }
  heading = "#" * (level + 1)

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


114
115
116
117
118
119
120
# File 'lib/aws2md/table.rb', line 114

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

#runObject



169
170
171
# File 'lib/aws2md/table.rb', line 169

def run
  build_table(nil, @json)
end