Class: Vernier::Output::Top::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/vernier/output/top.rb

Instance Method Summary collapse

Constructor Details

#initialize(header, row_limit) {|_self| ... } ⇒ Table

Returns a new instance of Table.

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
16
17
# File 'lib/vernier/output/top.rb', line 12

def initialize(header, row_limit)
  @header = header
  @rows = []
  @row_limit = row_limit
  yield self
end

Instance Method Details

#<<(row) ⇒ Object



19
20
21
# File 'lib/vernier/output/top.rb', line 19

def <<(row)
  @rows << row
end

#format_row(row) ⇒ Object



46
47
48
# File 'lib/vernier/output/top.rb', line 46

def format_row(row)
  "|" + row.map.with_index { |str, i| " " + str.ljust(widths[i] + 1) }.join("|") + "|"
end

#row_separatorObject



42
43
44
# File 'lib/vernier/output/top.rb', line 42

def row_separator
  @row_separator = "+" + widths.map { |i| "-" * (i + 2) }.join("+") + "+"
end

#to_sObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vernier/output/top.rb', line 23

def to_s
  (
    [
      row_separator,
      format_row(@header),
      row_separator
    ] + @rows.first(@row_limit).map do |row|
      format_row(row)
    end + [row_separator]
  ).join("\n")
end

#widthsObject



35
36
37
38
39
40
# File 'lib/vernier/output/top.rb', line 35

def widths
  @widths ||=
    (@rows + [@header]).transpose.map do |col|
      col.map(&:size).max
    end
end