Class: Tabelr::TableFormator
- Inherits:
-
Object
- Object
- Tabelr::TableFormator
- Defined in:
- lib/tabelr/table_formater.rb
Instance Method Summary collapse
- #analyse ⇒ Object
- #bank ⇒ Object
- #divider ⇒ Object
- #dump(output) ⇒ Object
- #format(text, count) ⇒ Object
- #go(json, output) ⇒ Object
-
#initialize ⇒ TableFormator
constructor
A new instance of TableFormator.
- #max(a, b) ⇒ Object
- #parse(json) ⇒ Object
- #stash(data) ⇒ Object
Constructor Details
#initialize ⇒ TableFormator
Returns a new instance of TableFormator.
4 5 6 7 |
# File 'lib/tabelr/table_formater.rb', line 4 def initialize @lines = [] @line = [] end |
Instance Method Details
#analyse ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/tabelr/table_formater.rb', line 36 def analyse @max_len = [] @lines.each do |line| line.each_with_index do |item, i| @max_len[i] = max @max_len[i], item.to_s.length end end end |
#bank ⇒ Object
19 20 21 22 |
# File 'lib/tabelr/table_formater.rb', line 19 def bank @lines << @line.dup @line.clear end |
#divider ⇒ Object
69 70 71 72 73 |
# File 'lib/tabelr/table_formater.rb', line 69 def divider s = "+" @max_len.each { |n| s += "-" * (n+2) + "+" } s += "\n" end |
#dump(output) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/tabelr/table_formater.rb', line 45 def dump output output.printf divider @lines.each_with_index do |line, index| output.printf divider if index == 1 line.each_with_index do |item, i| output.printf format item.to_s, @max_len[i] end output.printf "|\n" end output.printf divider output.flush end |
#format(text, count) ⇒ Object
65 66 67 |
# File 'lib/tabelr/table_formater.rb', line 65 def format text, count "| #{text} " + " " * (count-text.length) end |
#go(json, output) ⇒ Object
9 10 11 12 13 |
# File 'lib/tabelr/table_formater.rb', line 9 def go json, output parse json analyse dump output end |
#max(a, b) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/tabelr/table_formater.rb', line 58 def max a, b return a if b.nil? return b if a.nil? return a if a > b b end |
#parse(json) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/tabelr/table_formater.rb', line 24 def parse json # header json['data'].first.each_key { |key| stash key } bank # content/row json['data'].each do |line| line.each_value { |value| stash value } bank end end |
#stash(data) ⇒ Object
15 16 17 |
# File 'lib/tabelr/table_formater.rb', line 15 def stash data @line << data end |