Class: ReportBuilder
- Inherits:
-
Object
- Object
- ReportBuilder
- Defined in:
- lib/reportbuilder.rb,
lib/reportbuilder/graph.rb,
lib/reportbuilder/image.rb,
lib/reportbuilder/table.rb,
lib/reportbuilder/builder.rb,
lib/reportbuilder/builder/pdf.rb,
lib/reportbuilder/builder/rtf.rb,
lib/reportbuilder/builder/html.rb,
lib/reportbuilder/builder/text.rb,
lib/reportbuilder/graph/html_flot.rb,
lib/reportbuilder/table/pdfbuilder.rb,
lib/reportbuilder/table/rtfbuilder.rb,
lib/reportbuilder/graph/html_jqplot.rb,
lib/reportbuilder/table/htmlbuilder.rb,
lib/reportbuilder/table/textbuilder.rb
Overview
Report Abstract Interface.
Creates text, html,pdf and rtf output, based on a common framework.
Use
1) Using generic ReportBuilder#add, every object will be parsed using methods report_building_FORMAT, #report_building or #to_s
require "reportbuilder"
rb=ReportBuilder.new
rb.add(2) # Int#to_s used
section=ReportBuilder::Section.new(:name=>"Section 1")
table=ReportBuilder::Table.new(:name=>"Table", :header=>%w{id name})
table.row([1,"John"])
table.hr
table.row([2,"Peter"])
section.add(table) # Section is a container for other methods
rb.add(section) # table have a #report_building method
rb.add("Another text") # used directly
rb.name="Text output"
puts rb.to_text
rb.name="Html output"
puts rb.to_html
2) Using a block, you can control directly the builder
require "reportbuilder"
rb=ReportBuilder.new do
text("2")
section(:name=>"Section 1") do
table(:name=>"Table", :header=>%w{id name}) do
row([1,"John"])
hr
row([2,"Peter"])
end
end
preformatted("Another Text")
end
rb.name="Text output"
puts rb.to_text
rb.name="Html output"
puts rb.to_html
Defined Under Namespace
Classes: Builder, ElementBuilder, Graph, Image, ImageBlob, ImageFilename, Section, Table
Constant Summary collapse
- VERSION =
ReportBuilder version
'1.4.2'
- DATA_DIR =
File.dirname(__FILE__)+"/../data"
- FormatNotFound =
Class.new(Exception)
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
-
#name ⇒ Object
Name of report.
-
#no_title ⇒ Object
Doesn’t print a title if set to true.
Class Method Summary collapse
-
.builder_for(format) ⇒ Object
Available formats.
-
.generate(options = Hash.new, &block) ⇒ Object
Generates and optionally save the report on one function.
- .get_format_from_filename(filename) ⇒ Object
- .has_rmagick? ⇒ Boolean
Instance Method Summary collapse
-
#add(element) ⇒ Object
Add an element to the report.
-
#initialize(options = Hash.new, &block) ⇒ ReportBuilder
constructor
Create a new Report.
- #save(filename) ⇒ Object
-
#save_html(file) ⇒ Object
Save an html file.
-
#save_pdf(file) ⇒ Object
Save a pdf file.
-
#save_rtf(filename) ⇒ Object
Save a rtf file.
- #save_text(file) ⇒ Object
-
#to_html ⇒ Object
Returns an Html output.
- #to_pdf ⇒ Object
-
#to_rtf ⇒ Object
Returns a RTF output.
-
#to_text ⇒ Object
(also: #to_s)
Returns a Text output.
Constructor Details
#initialize(options = Hash.new, &block) ⇒ ReportBuilder
Create a new Report
106 107 108 109 110 111 112 113 114 |
# File 'lib/reportbuilder.rb', line 106 def initialize(=Hash.new, &block) [:name]||="Report "+Time.now.to_s @no_title=.delete :no_title @name=.delete :name @name=@name.to_s @options= @elements=Array.new add(block) if block end |
Instance Attribute Details
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
49 50 51 |
# File 'lib/reportbuilder.rb', line 49 def elements @elements end |
#name ⇒ Object
Name of report
51 52 53 |
# File 'lib/reportbuilder.rb', line 51 def name @name end |
#no_title ⇒ Object
Doesn’t print a title if set to true
53 54 55 |
# File 'lib/reportbuilder.rb', line 53 def no_title @no_title end |
Class Method Details
.builder_for(format) ⇒ Object
Available formats
59 60 61 62 |
# File 'lib/reportbuilder.rb', line 59 def self.builder_for(format) format=format.to_s.downcase Builder.inherited_classes.find {|m| m.code.include? format} end |
.generate(options = Hash.new, &block) ⇒ Object
Generates and optionally save the report on one function
-
options= Hash of options
-
:filename => name of file. If not provided, returns output
-
:format => format of output. See Builder subclasses
-
&block: block executed inside builder
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/reportbuilder.rb', line 77 def self.generate(=Hash.new, &block) [:filename]||=nil [:format]||=self.get_format_from_filename([:filename]) if [:filename] [:format]||="text" file=.delete(:filename) format=.delete(:format) rb=ReportBuilder.new() rb.add(block) begin builder=builder_for(format).new(rb, ) rescue NameError => e raise ReportBuilder::FormatNotFound.new(e) end builder.parse out=builder.out unless file.nil? File.open(file,"wb") do |fp| fp.write out end else out end end |
.get_format_from_filename(filename) ⇒ Object
101 102 103 104 |
# File 'lib/reportbuilder.rb', line 101 def self.get_format_from_filename(filename) filename=~/\.(\w+?)$/ $1 end |
.has_rmagick? ⇒ Boolean
63 64 65 66 67 68 69 70 |
# File 'lib/reportbuilder.rb', line 63 def self.has_rmagick? begin require 'RMagick' true rescue LoadError false end end |
Instance Method Details
#add(element) ⇒ Object
Add an element to the report. If parameters is an object which respond to :to_reportbuilder, this method will called. Otherwise, the element itself will be added
119 120 121 122 |
# File 'lib/reportbuilder.rb', line 119 def add(element) @elements.push(element) self end |
#save(filename) ⇒ Object
140 141 142 143 |
# File 'lib/reportbuilder.rb', line 140 def save(filename) format=(self.class).get_format_from_filename(filename) send("save_#{format}", filename) end |
#save_html(file) ⇒ Object
Save an html file
151 152 153 154 155 156 157 |
# File 'lib/reportbuilder.rb', line 151 def save_html(file) =@options.dup [:directory]=File.dirname(file) gen=Builder::Html.new(self, ) gen.parse gen.save(file) end |
#save_pdf(file) ⇒ Object
Save a pdf file
159 160 161 162 163 164 |
# File 'lib/reportbuilder.rb', line 159 def save_pdf(file) =@options.dup gen=Builder::Pdf.new(self, ) gen.parse gen.save(file) end |
#save_rtf(filename) ⇒ Object
Save a rtf file
145 146 147 148 149 |
# File 'lib/reportbuilder.rb', line 145 def save_rtf(filename) gen = Builder::Rtf.new(self,@options) gen.parse gen.save(filename) end |
#save_text(file) ⇒ Object
172 173 174 175 176 |
# File 'lib/reportbuilder.rb', line 172 def save_text(file) gen=Builder::Text.new(self, @options) gen.parse gen.save(file) end |
#to_html ⇒ Object
Returns an Html output
124 125 126 127 128 |
# File 'lib/reportbuilder.rb', line 124 def to_html gen = Builder::Html.new(self,@options) gen.parse gen.out end |
#to_pdf ⇒ Object
135 136 137 138 139 |
# File 'lib/reportbuilder.rb', line 135 def to_pdf gen = Builder::Pdf.new(self, @options) gen.parse gen.out end |
#to_rtf ⇒ Object
Returns a RTF output
130 131 132 133 134 |
# File 'lib/reportbuilder.rb', line 130 def to_rtf gen = Builder::Rtf.new(self, @options) gen.parse gen.out end |
#to_text ⇒ Object Also known as: to_s
Returns a Text output
167 168 169 170 171 |
# File 'lib/reportbuilder.rb', line 167 def to_text() gen=Builder::Text.new(self, @options) gen.parse gen.out end |