Class: Mutiny::Output::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/mutiny/output/table.rb

Instance Method Summary collapse

Constructor Details

#initializeTable

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_sObject



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