Class: Cardigan::TextReportFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/cardigan/text_report_formatter.rb

Constant Summary collapse

INDEX_HEADING =
'index'

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ TextReportFormatter

Returns a new instance of TextReportFormatter.



5
6
7
8
# File 'lib/cardigan/text_report_formatter.rb', line 5

def initialize io
  @io = io
  @columns = []
end

Instance Method Details

#add_column(name, length) ⇒ Object



10
11
12
13
# File 'lib/cardigan/text_report_formatter.rb', line 10

def add_column name, length
  longer = [name.length, length].max
  @columns << [name, longer]
end

#output(hashes, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cardigan/text_report_formatter.rb', line 15

def output hashes, options={}
  return if @columns.empty?
  width = calculate_width(options[:suppress_index])
  hline width
  row INDEX_HEADING, @columns.map {|tuple| tuple.first }, options[:suppress_index]
  hline width
  hashes.each_with_index do |h,i|
    row (i+1).to_s, @columns.map {|tuple| h[tuple.first]}, options[:suppress_index]
  end
  hline width
end