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/rtf.rb,
lib/reportbuilder/builder/html.rb,
lib/reportbuilder/builder/text.rb,
lib/reportbuilder/graph/html_flot.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 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.3.0'
- 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.
- .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_html(file) ⇒ Object
Save an html file.
-
#save_rtf(filename) ⇒ Object
Save a rtf file.
- #save_text(file) ⇒ Object
-
#to_html ⇒ Object
Returns an Html output.
-
#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
104 105 106 107 108 109 110 111 |
# File 'lib/reportbuilder.rb', line 104 def initialize(=Hash.new, &block) [:name]||="Report "+Time.new.to_s @no_title=.delete :no_title @name=.delete :name @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 101 102 |
# File 'lib/reportbuilder.rb', line 77 def self.generate(=Hash.new, &block) [:filename]||=nil [:format]||="text" if [:filename] and [:filename]=~/\.(\w+?)$/ [:format]=$1 end file=.delete(:filename) format=.delete(:format) rb=ReportBuilder.new() rb.add(block) begin builder=builder_for(format).new(rb, ) rescue NameError => e raise 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 |
.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
116 117 118 119 |
# File 'lib/reportbuilder.rb', line 116 def add(element) @elements.push(element) self end |
#save_html(file) ⇒ Object
Save an html file
139 140 141 142 143 144 145 |
# File 'lib/reportbuilder.rb', line 139 def save_html(file) =@options.dup [:directory]=File.dirname(file) gen=Builder::Html.new(self, ) gen.parse gen.save(file) end |
#save_rtf(filename) ⇒ Object
Save a rtf file
133 134 135 136 137 |
# File 'lib/reportbuilder.rb', line 133 def save_rtf(filename) gen = Builder::Rtf.new(self,@options) gen.parse gen.save(filename) end |
#save_text(file) ⇒ Object
152 153 154 155 156 |
# File 'lib/reportbuilder.rb', line 152 def save_text(file) gen=Builder::Text.new(self, @options) gen.parse gen.save(file) end |
#to_html ⇒ Object
Returns an Html output
121 122 123 124 125 |
# File 'lib/reportbuilder.rb', line 121 def to_html() gen = Builder::Html.new(self,@options) gen.parse gen.out end |
#to_rtf ⇒ Object
Returns a RTF output
127 128 129 130 131 |
# File 'lib/reportbuilder.rb', line 127 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
147 148 149 150 151 |
# File 'lib/reportbuilder.rb', line 147 def to_text() gen=Builder::Text.new(self, @options) gen.parse gen.out end |