Class: Mutiny::Output::Table
- Inherits:
-
Object
- Object
- Mutiny::Output::Table
- Defined in:
- lib/mutiny/output/table.rb
Instance Method Summary collapse
- #add_row(cells) ⇒ Object
- #add_rows(rows) ⇒ Object
- #cell_to_s(cell, column_index) ⇒ Object
-
#initialize ⇒ Table
constructor
A new instance of Table.
- #row_to_s(cells) ⇒ Object
- #to_s ⇒ Object
- #width_for_column(index) ⇒ Object
Constructor Details
#initialize ⇒ Table
Returns a new instance of Table.
4 5 6 |
# File 'lib/mutiny/output/table.rb', line 4 def initialize @rows = [] end |
Instance Method Details
#add_row(cells) ⇒ Object
12 13 14 |
# File 'lib/mutiny/output/table.rb', line 12 def add_row(cells) @rows << cells end |
#add_rows(rows) ⇒ Object
8 9 10 |
# File 'lib/mutiny/output/table.rb', line 8 def add_rows(rows) rows.each { |r| add_row(r) } end |
#cell_to_s(cell, column_index) ⇒ Object
26 27 28 |
# File 'lib/mutiny/output/table.rb', line 26 def cell_to_s(cell, column_index) cell.to_s.ljust(width_for_column(column_index)) end |
#row_to_s(cells) ⇒ Object
20 21 22 23 24 |
# File 'lib/mutiny/output/table.rb', line 20 def row_to_s(cells) "| " + cells.each_with_index.map { |cell, index| cell_to_s(cell, index) }.join(" | ") + " |" end |
#to_s ⇒ Object
16 17 18 |
# File 'lib/mutiny/output/table.rb', line 16 def to_s @rows.map { |r| row_to_s(r) }.join("\n") end |
#width_for_column(index) ⇒ Object
30 31 32 |
# File 'lib/mutiny/output/table.rb', line 30 def width_for_column(index) @rows.map { |r| r[index].to_s.size }.max end |