7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/simpletable.rb', line 7
def create( objects, titles, methods, options = {} )
raise "Mismatched number of methods and column titles" if titles.length != methods.length
divider = options[:divider] || DEFAULT_DIVIDER
padding = options[:padding] || DEFAULT_PADDING
widths = []
titles.zip(methods).each do |title,method|
widths << objects.collect { |o| o.send(method).to_s }.push(title).group_by(&:size).max.first + padding
end
print_row(titles,widths)
puts divider * (widths.inject(:+) - padding)
objects.each do |o|
data = methods.collect{ |m| o.send(m) } print_row(data,widths)
end
end
|