Code Coverage
 
Lines
Covered
97.78% covered (success)
97.78%
44 / 45
1
module TheFox
1
module Timr
1
class Table
# Holds all rows.
1
attr_reader :rows
1
def initialize(options = Hash.new)
1
@headings = options.fetch(:headings, Array.new)
1
@rows = Array.new
end
# Append a row.
1
def <<(row)
1
col_n = 0
1
row.each do |col|
2
header = @headings[col_n]
2
if header
2
header[:format] ||= '%s'
2
header[:label] ||= ''
2
unless header.has_key?(:empty)
2
header[:empty] = true
end
2
header[:max_length] ||= 0
2
header[:padding_left] ||= ''
2
header[:padding_right] ||= ''
else
0
header = {
:format => '%s',
:label => '',
:empty => true,
:max_length => 0,
:padding_left => '',
:padding_right => '',
}
end
2
unless col.nil?
2
if header[:empty]
2
header[:empty] = false
end
2
col_s = col.to_s
2
if col_s.length > header[:max_length]
2
header[:max_length] = (header[:format] % [col_s]).length + header[:padding_left].length + header[:padding_right].length
end
end
2
col_n += 1
end
1
@rows << row
end
# Render Table to String.
1
def to_s
1
s = ''
s << @headings.map{ |header|
2
unless header[:empty]
2
"%s#{header[:format]}%s" % [header[:padding_left], header[:label], header[:padding_right]]
end
3
}.select{ |ts| !ts.nil? }.join(' ')
1
s << "\n"
1
@rows.each do |row|
1
col_n = 0
1
columns = []
1
row.each do |col|
2
header = @headings[col_n]
2
unless header[:empty]
2
col_s = "%s#{header[:format]}%s" % [header[:padding_left], col, header[:padding_right]]
2
columns << col_s
end
2
col_n += 1
end
1
s << columns.join(' ') << "\n"
end
1
s
end
end # class Task
end # module Timr
end #module TheFox