Class: ReportBuilder::Table
- Inherits:
-
Object
- Object
- ReportBuilder::Table
- Defined in:
- lib/reportbuilder/table.rb,
lib/reportbuilder/table/pdfbuilder.rb,
lib/reportbuilder/table/rtfbuilder.rb,
lib/reportbuilder/table/htmlbuilder.rb,
lib/reportbuilder/table/textbuilder.rb
Overview
Creates a table. Use:
table=ReportBuilder::Table.new(:header =>["id","city","name","code1","code2"])
table.row([1, Table.rowspan("New York",3), "Ringo", Table.colspan("no code",2),nil])
table.row([2, nil,"John", "ab-1","ab-2"])
table.row([3, nil,"Paul", "ab-3"])
table.hr
table.row([4, "London","George", Table.colspan("ab-4",2),nil])
puts table
==>
-----------------------------------------
| id | city | name | code1 | code2 |
-----------------------------------------
| 1 | New York | Ringo | no code |
| 2 | | John | ab-1 | ab-2 |
| 3 | | Paul | ab-3 | |
-----------------------------------------
| 4 | London |George | ab-4 |
-----------------------------------------
Defined Under Namespace
Classes: Colspan, HtmlBuilder, PdfBuilder, Rowspan, RtfBuilder, TextBuilder
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :header => [], :name => nil }
- @@n =
:nodoc:
1
Instance Attribute Summary collapse
-
#header ⇒ Object
Array of headers.
-
#max_cols ⇒ Object
readonly
Size for each column.
-
#name ⇒ Object
Array of headers.
-
#options ⇒ Object
Returns the value of attribute options.
-
#rows ⇒ Object
readonly
Array of rows.
Instance Method Summary collapse
-
#calculate_widths ⇒ Object
:nodoc:.
-
#colspan(data, n) ⇒ Object
Adds a colspan on a cell table.add_row().
-
#hr ⇒ Object
(also: #horizontal_line)
Adds a horizontal rule table.add_hr.
-
#initialize(opts = Hash.new, &block) ⇒ Table
constructor
Create a new table.
- #n_columns ⇒ Object
- #n_rows ⇒ Object
-
#n_rows_no_hr ⇒ Object
Count the numbers of rows without :hr.
- #report_building_html(builder) ⇒ Object
- #report_building_pdf(builder) ⇒ Object
- #report_building_rtf(builder) ⇒ Object
- #report_building_text(builder) ⇒ Object
-
#row(row) ⇒ Object
Adds a row Usage: table.add_row(%w2).
-
#rowspan(data, n) ⇒ Object
Adds a rowspan on a cell table.add_row().
-
#total_width ⇒ Object
:nodoc:.
Constructor Details
#initialize(opts = Hash.new, &block) ⇒ Table
Create a new table. Options: :name, :header Use:
table=ReportBuilder::Table.new(:header =>["var1","var2"])
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/reportbuilder/table.rb', line 39 def initialize(opts=Hash.new, &block) raise ArgumentError,"opts should be a Hash" if !opts.is_a? Hash @options=DEFAULT_OPTIONS.merge opts if @options[:name].nil? @name= "Table #{@@n}" @@n+=1 else @name=@options.delete :name end @header=@options.delete :header @rows=[] @max_cols=[] if block block.arity<1 ? self.instance_eval(&block) : block.call(self) end end |
Instance Attribute Details
#header ⇒ Object
Array of headers
29 30 31 |
# File 'lib/reportbuilder/table.rb', line 29 def header @header end |
#max_cols ⇒ Object (readonly)
Size for each column
31 32 33 |
# File 'lib/reportbuilder/table.rb', line 31 def max_cols @max_cols end |
#name ⇒ Object
Array of headers
29 30 31 |
# File 'lib/reportbuilder/table.rb', line 29 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
34 35 36 |
# File 'lib/reportbuilder/table.rb', line 34 def @options end |
#rows ⇒ Object (readonly)
Array of rows
33 34 35 |
# File 'lib/reportbuilder/table.rb', line 33 def rows @rows end |
Instance Method Details
#calculate_widths ⇒ Object
:nodoc:
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/reportbuilder/table.rb', line 87 def calculate_widths # :nodoc: @max_cols=[] rows_cal=[header]+@rows rows_cal.each{|row| next if row==:hr row.each_index{|i| if row[i].nil? next elsif row[i].is_a? Colspan size_total=row[i].data.to_s.size size_per_column=(size_total / row[i].cols)+1 for mi in i...i+row[i].cols @max_cols[mi] = size_per_column if @max_cols[mi].nil? or @max_cols[mi]<size_per_column end elsif row[i].is_a? Rowspan size=row[i].data.to_s.size @max_cols[i]= size if @max_cols[i].nil? or @max_cols[i] < size else size=row[i].to_s.size @max_cols[i]= size if @max_cols[i].nil? or @max_cols[i] < size end } } @max_cols end |
#colspan(data, n) ⇒ Object
Adds a colspan on a cell
table.add_row(["a",table.colspan("b",2)])
84 85 86 |
# File 'lib/reportbuilder/table.rb', line 84 def colspan(data,n) Colspan.new(data,n) end |
#hr ⇒ Object Also known as: horizontal_line
Adds a horizontal rule
table.add_hr
66 67 68 |
# File 'lib/reportbuilder/table.rb', line 66 def hr @rows.push(:hr) end |
#n_columns ⇒ Object
61 62 63 |
# File 'lib/reportbuilder/table.rb', line 61 def n_columns @n_columns||=calculate_widths.size end |
#n_rows ⇒ Object
79 80 81 |
# File 'lib/reportbuilder/table.rb', line 79 def n_rows @rows.size end |
#n_rows_no_hr ⇒ Object
Count the numbers of rows without :hr
76 77 78 |
# File 'lib/reportbuilder/table.rb', line 76 def n_rows_no_hr @rows.inject(0) {|ac,v| ac+(v==:hr ? 0 : 1)} end |
#report_building_html(builder) ⇒ Object
118 119 120 121 122 |
# File 'lib/reportbuilder/table.rb', line 118 def report_building_html(builder) require 'reportbuilder/table/htmlbuilder' table_builder=ReportBuilder::Table::HtmlBuilder.new(builder, self) table_builder.generate end |
#report_building_pdf(builder) ⇒ Object
128 129 130 131 132 |
# File 'lib/reportbuilder/table.rb', line 128 def report_building_pdf(builder) require 'reportbuilder/table/pdfbuilder' table_builder=ReportBuilder::Table::PdfBuilder.new(builder, self) table_builder.generate end |
#report_building_rtf(builder) ⇒ Object
123 124 125 126 127 |
# File 'lib/reportbuilder/table.rb', line 123 def report_building_rtf(builder) require 'reportbuilder/table/rtfbuilder' table_builder=ReportBuilder::Table::RtfBuilder.new(builder, self) table_builder.generate end |
#report_building_text(builder) ⇒ Object
113 114 115 116 117 |
# File 'lib/reportbuilder/table.rb', line 113 def report_building_text(builder) require 'reportbuilder/table/textbuilder' table_builder=ReportBuilder::Table::TextBuilder.new( builder, self) table_builder.generate end |
#row(row) ⇒ Object
Adds a row Usage:
table.add_row(%w{1 2})
58 59 60 |
# File 'lib/reportbuilder/table.rb', line 58 def row(row) @rows.push(row) end |
#rowspan(data, n) ⇒ Object
Adds a rowspan on a cell
table.add_row(["a",table.rowspan("b",2)])
72 73 74 |
# File 'lib/reportbuilder/table.rb', line 72 def rowspan(data,n) Rowspan.new(data,n) end |
#total_width ⇒ Object
:nodoc:
134 135 136 137 138 139 140 |
# File 'lib/reportbuilder/table.rb', line 134 def total_width # :nodoc: if @max_cols.size>0 @max_cols.inject(0){|a,v| a+(v+3)}+1 else 0 end end |