Class: Aws2MD::VerticalTable
Instance Method Summary collapse
- #build_table(title, current, level = 0) ⇒ Object
- #header_to_rows(header_keys, hash) ⇒ Object
-
#initialize(json) ⇒ VerticalTable
constructor
A new instance of VerticalTable.
- #print_table_for_array(rows, title, level, i) ⇒ Object
- #print_table_for_hash(rows) ⇒ Object
- #run ⇒ Object
Methods inherited from Table
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 |
#print_table_for_array(rows, title, level, i) ⇒ Object
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 |
#print_table_for_hash(rows) ⇒ Object
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 |
#run ⇒ Object
169 170 171 |
# File 'lib/aws2md/table.rb', line 169 def run build_table(nil, @json) end |