Class: DDMetrics::Table

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

Instance Method Summary collapse

Constructor Details

#initialize(rows, num_headers: 1) ⇒ Table

Returns a new instance of Table.



5
6
7
8
# File 'lib/ddmetrics/table.rb', line 5

def initialize(rows, num_headers: 1)
  @rows = rows
  @num_headers = num_headers
end

Instance Method Details

#to_sObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ddmetrics/table.rb', line 10

def to_s
  columns = @rows.transpose
  column_lengths = columns.map { |c| c.map(&:size).max }

  [].tap do |lines|
    # header
    lines << row_to_s(@rows[0], column_lengths)

    # separator
    lines << separator(column_lengths)

    # body
    rows = sort_rows(@rows.drop(1))
    lines.concat(rows.map { |r| row_to_s(r, column_lengths) })
  end.join("\n")
end