Class: Ruport::Report
- Inherits:
-
Object
- Object
- Ruport::Report
- Extended by:
- Forwardable
- Includes:
- Controller::Hooks
- Defined in:
- lib/ruport/util/report.rb,
lib/ruport/util/mailer.rb,
lib/ruport/util/report_manager.rb
Overview
Overview
The Ruport::Report class provides a high level interface to much of Ruport’s functionality. It is designed to allow you to build and run reports easily. If your needs are complicated, you will probably need to take a look at the individual classes of the library, but if they are fairly simple, you may be able to get away using this class alone.
Ruport::Report is primarily meant to be used with Ruport’s code generator, rope, and is less useful when integrated within another system, such as Rails or Camping.
Below is a simple example of loading a report in from a CSV, performing a grouping operation, and then rendering the resulting PDF to file.
require "rubygems"
require "ruport"
class MyReport < Ruport::Report
renders_as_grouping(:style => :inline)
def renderable_data(format)
table = Table("foo.csv")
Grouping(table, :by => "username")
end
end
report = MyReport.new
report.save_as("bar.pdf")
Defined Under Namespace
Modules: Invoice
Class Method Summary collapse
Instance Method Summary collapse
- #add_source(*args) ⇒ Object
- #as(*args, &block) ⇒ Object
- #method_missing(id, *args, &block) ⇒ Object
- #old_as ⇒ Object
-
#query(sql, options = {}) ⇒ Object
This is a simplified interface to Ruport::Query.
- #renderable_data(format) ⇒ Object
- #save_as(filename, options = {}, &block) ⇒ Object
-
#send_to(adds) {|m| ... } ⇒ Object
Creates a new Mailer and sets the
to
attribute to the addresses specified. -
#use_mailer(label) ⇒ Object
Sets the active mailer to the Ruport::Mailer source requested by
label
.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *args, &block) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/ruport/util/report.rb', line 113 def method_missing(id,*args,&block) if id.to_s =~ /^to_(.*)/ as($1.to_sym,*args,&block) else super end end |
Class Method Details
.acts_as_managed_report ⇒ Object
41 42 43 |
# File 'lib/ruport/util/report_manager.rb', line 41 def self.acts_as_managed_report Ruport::ReportManager.add_report(self) end |
.generate {|new| ... } ⇒ Object
125 126 127 |
# File 'lib/ruport/util/report.rb', line 125 def self.generate yield(new) end |
Instance Method Details
#add_source(*args) ⇒ Object
121 122 123 |
# File 'lib/ruport/util/report.rb', line 121 def add_source(*args) Ruport::Query.add_source(*args) end |
#as(*args, &block) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/ruport/util/report.rb', line 93 def as(*args,&block) prepare if respond_to?(:prepare) output = old_as(*args,&block) cleanup if respond_to?(:cleanup) return output end |
#old_as ⇒ Object
91 |
# File 'lib/ruport/util/report.rb', line 91 alias_method :old_as, :as |
#query(sql, options = {}) ⇒ Object
This is a simplified interface to Ruport::Query.
You can use it to read SQL statements from file or string:
#from string
result = query "select * from foo"
You can use multistatement SQL:
# will return the value of the last statement, "select * from foo"
result = query "insert into foo values(1,2); select * from foo"
You can iterate by row:
query("select * from foo") { |r|
#do something with the rows here
}
query() can return raw DBI:Row objects or Ruport’s data structures:
# will return an Array of DBI::Row objects
result = query "select * from foo", :raw_data => true
See Ruport::Query for details.
75 76 77 78 79 80 81 82 83 |
# File 'lib/ruport/util/report.rb', line 75 def query(sql, ={}) [:source] ||= :default q = Ruport::Query.new(sql, ) if block_given? q.each { |r| yield(r) } else q.result end end |
#renderable_data(format) ⇒ Object
85 86 87 88 89 |
# File 'lib/ruport/util/report.rb', line 85 def renderable_data(format) raise NotImplementedError, "You must implement renderable_data(format) if you wish to use as() or save_as()" end |
#save_as(filename, options = {}, &block) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ruport/util/report.rb', line 100 def save_as(filename,={},&block) formats = { "csv" => ["w",:csv], "txt" => ["w",:text], "html" => ["w", :html], "pdf" => ["wb", :pdf ] } fo = filename =~ /.*\.(.*)/ && formats[$1] flags = .delete(:flags) if fo File.open(filename,flags || fo[0]) { |f| f << as(fo[1],,&block) } else File.open(filename,flags || "w") { |f| f << as($1.to_sym,,&block) } end end |
#send_to(adds) {|m| ... } ⇒ Object
Creates a new Mailer and sets the to
attribute to the addresses specified. Yields a Mailer object, which can be modified before delivery.
137 138 139 140 141 142 143 |
# File 'lib/ruport/util/mailer.rb', line 137 def send_to(adds) use_mailer(:default) unless @mailer m = Mailer.new yield(m) m.send(:select_mailer,@mailer) m.deliver :from => m.from, :to => adds end |
#use_mailer(label) ⇒ Object
Sets the active mailer to the Ruport::Mailer source requested by label
.
146 147 148 |
# File 'lib/ruport/util/mailer.rb', line 146 def use_mailer(label) @mailer = label end |