Class: ReportBuilder::Table::RtfBuilder

Inherits:
ElementBuilder show all
Includes:
RTF
Defined in:
lib/reportbuilder/table/rtfbuilder.rb

Instance Method Summary collapse

Methods inherited from ElementBuilder

#initialize

Constructor Details

This class inherits a constructor from ReportBuilder::ElementBuilder

Instance Method Details

#add_hr_on_bottom(row_i) ⇒ Object



49
50
51
52
53
# File 'lib/reportbuilder/table/rtfbuilder.rb', line 49

def add_hr_on_bottom(row_i)
  (0...@t.n_columns).each {|i|
    @table[row_i][i].bottom_border_width=@builder.options[:table_hr_width]
  }
end

#add_hr_on_top(row_i) ⇒ Object



44
45
46
47
48
# File 'lib/reportbuilder/table/rtfbuilder.rb', line 44

def add_hr_on_top(row_i)
  (0...@t.n_columns).each {|i|
    @table[row_i][i].top_border_width=@builder.options[:table_hr_width]
  }
end

#generateObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/reportbuilder/table/rtfbuilder.rb', line 5

def generate()
  @t=@element
  @rtf=@builder.rtf

  # Title

  @builder.header(6,@t.name)

  max_cols=@t.calculate_widths
  n_rows=@t.n_rows_no_hr+(@t.header.size>0 ? 1: 0)
  args=[n_rows, @t.n_columns]+max_cols.map{|m| m*@builder.options[:font_size]*10}
  @table=@rtf.table(*args)
  @table.border_width=@builder.options[:table_border_width]
  @rowspans=[]
  row_i=0
  if @t.header.size>0
    row_i=1
    @t.header.each_with_index do |th,i|
      @table[0][i] << th
    end
    add_hr_on_bottom(0)
  end
  next_with_hr=false

  @t.rows.each do |row|
    if row==:hr
      next_with_hr=true
      # Nothing
    else
      parse_row(row,row_i)
      if next_with_hr
        add_hr_on_top(row_i)
        next_with_hr=false
      end
      row_i+=1
    end
  end

end

#parse_row(row, row_i) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/reportbuilder/table/rtfbuilder.rb', line 55

def parse_row(row,row_i)
  t=@element
  row_ary=[]
  real_i=0
  colspan_i=0
  row.each_index do |i|
    extra=1
    while !@rowspans[real_i].nil? and @rowspans[real_i]>0
      @rowspans[real_i]-=1
      real_i+=1
    end
    if row[i].is_a? Table::Colspan
      @table[row_i][real_i] << row[i].data
      colspan_i=row[i].cols-1
      extra=row[i].cols
    elsif row[i].nil?
      @table[row_i][i] << ""
    elsif row[i].is_a? Table::Rowspan
      @table[row_i][real_i] << row[i].data
      @rowspans[real_i]=row[i].rows-1
    else
      @table[row_i][real_i] << row[i].to_s
    end
    real_i+=extra
  end
end