Class: BenchmarkForRails::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmark_for_rails/report.rb

Constant Summary collapse

PADDING =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReport

Returns a new instance of Report.



5
6
7
# File 'lib/benchmark_for_rails/report.rb', line 5

def initialize
  self.rows = []
end

Instance Attribute Details

#rowsObject

Returns the value of attribute rows.



4
5
6
# File 'lib/benchmark_for_rails/report.rb', line 4

def rows
  @rows
end

Instance Method Details

#render(*headers) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/benchmark_for_rails/report.rb', line 9

def render(*headers)
  headers.each_index do |i|
    print headers[i].to_s.ljust(width_of_column(i, headers[i].length + PADDING), ' ')
  end
  puts ""

  headers.each_index do |i|
    print '-' * (width_of_column(i) - PADDING)
    print ' ' * PADDING
  end
  puts ""

  rows.each do |row|
    row.each_index do |i|
      print row[i].to_s.ljust(width_of_column(i), ' ')
    end
    puts ""
  end
end

#width_of_column(i, min = 8) ⇒ Object



29
30
31
32
33
# File 'lib/benchmark_for_rails/report.rb', line 29

def width_of_column(i, min = 8)
  @column_widths ||= []
  @column_widths[i] = (rows.collect{|r| r[i]}.collect{|c| c.to_s.length + PADDING} << min).max if @column_widths[i].nil?
  @column_widths[i]
end