Class: ReportBuilder::Table::TextBuilder

Inherits:
ElementBuilder show all
Defined in:
lib/reportbuilder/table/textbuilder.rb

Overview

Text Builder for ReportBuilder::Table objects.

Uses Aaron Tinio’s text-table gem

Instance Method Summary collapse

Methods inherited from ElementBuilder

#initialize

Constructor Details

This class inherits a constructor from ReportBuilder::ElementBuilder

Instance Method Details

#generateObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/reportbuilder/table/textbuilder.rb', line 9

def generate()
  t=@element
  @rowspans=[]
  @builder.text(t.name)
  return if t.header.size+t.rows.size==0
  table = Text::Table.new do |tt|
    tt.head=t.header if t.header.size>0
    tt.rows=t.rows.map {|row| parse_row(row)}
  end
  #pp table.rows
  @builder.preformatted(table.to_s)
end

#parse_row(row) ⇒ Object

Parse a row



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/reportbuilder/table/textbuilder.rb', line 22

def parse_row(row)
  return :separator if row==:hr
  t=@element
  row_ary=[]
  real_i=0
  row.each_index do |i|
    extra=1
    
    while !@rowspans[real_i].nil? and @rowspans[real_i]>0
      @rowspans[real_i]-=1
      row_ary << ""
      real_i+=1
    end
    
    if row[i].is_a? ReportBuilder::Table::Rowspan
      @rowspans[real_i]=row[i].rows-1
      row_ary << row[i].to_s
    elsif row[i].is_a? ReportBuilder::Table::Colspan
      row_ary.push({:value=>row[i].to_s, :colspan=>row[i].cols})
      extra=row[i].cols
    elsif row[i].nil?
      row_ary.push("")
    else
      #size=row[i].to_s.size
      #puts sprintf("%i : %s (%d-%d)",i,row[i].to_s,@max_cols[i], size)
      row_ary.push(row[i].to_s)
    end
    real_i+=extra
  end
  row_ary
end