Class: DynamicReports::Report
- Inherits:
-
Object
- Object
- DynamicReports::Report
- Defined in:
- lib/dynamic_reports/reports.rb
Overview
DynamicReports::Report
Constant Summary collapse
- @@default_engine =
"erb"
Instance Attribute Summary collapse
-
#charts ⇒ Object
Returns the value of attribute charts.
-
#class_name ⇒ Object
Returns the value of attribute class_name.
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#links ⇒ Object
Returns the value of attribute links.
-
#name ⇒ Object
Returns the value of attribute name.
-
#records ⇒ Object
Returns the value of attribute records.
-
#styles ⇒ Object
Returns the value of attribute styles.
-
#sub_title ⇒ Object
Returns the value of attribute sub_title.
-
#template ⇒ Object
Returns the value of attribute template.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
-
.chart(name, *chart_options, &block) ⇒ Object
Define a chart for the report.
-
.chart_with_name(name) ⇒ Object
Return the chart with the specified name, if it exists.
-
.charts(object = nil) ⇒ Object
Return an array of charts defined for the report.
- .class_name(value = nil) ⇒ Object
-
.columns(*array) ⇒ Object
Accessor for columns.
-
.link(column, url, link_options = nil) ⇒ Object
Define a link for the report.
-
.links(object = nil) ⇒ Object
Return an array of links defined for the report.
-
.name(value = nil) ⇒ Object
class level name accessor & setter.
-
.on(records) ⇒ Object
Method for instanciating a report instance on a set of given records.
-
.options ⇒ Object
class level options accessor.
- .styles ⇒ Object
-
.sub_title(value = nil) ⇒ Object
Accessor used to set the sub title:.
-
.subreport(column, url, link_options = nil) ⇒ Object
Define an inline subreport for the report.
-
.template(value = nil) ⇒ Object
Accessor for the template to use for the report.
-
.title(value = nil) ⇒ Object
Accessor used to set the title:.
-
.views(*array) ⇒ Object
Views setter and accessor.
Instance Method Summary collapse
-
#initialize(records, *new_options) ⇒ Report
constructor
Instantiate the report on a set of records.
-
#options ⇒ Object
options accessor, all report options and configuration is stored in this.
-
#to_csv ⇒ Object
Not Implemented Yet.
-
#to_html(*options) ⇒ Object
Convert an instance of a report bound to a records set to an html view.
-
#to_pdf ⇒ Object
Not Implemented Yet.
-
#views ⇒ Object
views accessor, array of view paths.
Constructor Details
#initialize(records, *new_options) ⇒ Report
Instantiate the report on a set of records.
Example:
OrdersReport.new(@records)
options is a set of optional overrides for
-
views - Used to override the class defined views.
-
template - Used to override the class defined template.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/dynamic_reports/reports.rb', line 247 def initialize(records, *) = .shift || {} @records = records @views = self.class.views @views.unshift(.delete(:views)) if [:views] @views.flatten! @views.uniq! @template = self.class.template @template = .delete(:template) if [:template] @options = self.class..merge!() @options.each_pair do |key,value| if key == "chart" # TODO: erh? self.chart(value[:name],{},value) else instance_variable_set("@#{key}".to_sym, value) end end end |
Instance Attribute Details
#charts ⇒ Object
Returns the value of attribute charts.
11 12 13 |
# File 'lib/dynamic_reports/reports.rb', line 11 def charts @charts end |
#class_name ⇒ Object
Returns the value of attribute class_name.
11 12 13 |
# File 'lib/dynamic_reports/reports.rb', line 11 def class_name @class_name end |
#columns ⇒ Object
Returns the value of attribute columns.
11 12 13 |
# File 'lib/dynamic_reports/reports.rb', line 11 def columns @columns end |
#links ⇒ Object
Returns the value of attribute links.
11 12 13 |
# File 'lib/dynamic_reports/reports.rb', line 11 def links @links end |
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/dynamic_reports/reports.rb', line 11 def name @name end |
#records ⇒ Object
Returns the value of attribute records.
11 12 13 |
# File 'lib/dynamic_reports/reports.rb', line 11 def records @records end |
#styles ⇒ Object
Returns the value of attribute styles.
11 12 13 |
# File 'lib/dynamic_reports/reports.rb', line 11 def styles @styles end |
#sub_title ⇒ Object
Returns the value of attribute sub_title.
11 12 13 |
# File 'lib/dynamic_reports/reports.rb', line 11 def sub_title @sub_title end |
#template ⇒ Object
Returns the value of attribute template.
11 12 13 |
# File 'lib/dynamic_reports/reports.rb', line 11 def template @template end |
#title ⇒ Object
Returns the value of attribute title.
11 12 13 |
# File 'lib/dynamic_reports/reports.rb', line 11 def title @title end |
Class Method Details
.chart(name, *chart_options, &block) ⇒ Object
Define a chart for the report
Example:
chart :PV_Visits, {:grxl => 'xxxxx'} do
title "Pageviews and Visits"
columns [:pageviews, :visits]
no_labels false
label_column "recorded_at"
width "400"
height "350"
type "line"
end
169 170 171 172 |
# File 'lib/dynamic_reports/reports.rb', line 169 def chart(name, *, &block) = .shift || {} charts(Chart.configure(name, , &block)) end |
.chart_with_name(name) ⇒ Object
Return the chart with the specified name, if it exists. nil otherwise.
145 146 147 |
# File 'lib/dynamic_reports/reports.rb', line 145 def chart_with_name(name) [:charts].to_a.detect{ |c| c.name == name.to_sym } end |
.charts(object = nil) ⇒ Object
Return an array of charts defined for the report.
150 151 152 153 154 |
# File 'lib/dynamic_reports/reports.rb', line 150 def charts(object=nil) [:charts] ||= [] [:charts] << object if object [:charts] end |
.class_name(value = nil) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/dynamic_reports/reports.rb', line 88 def class_name(value = nil) if value [:class_name] = value elsif [:class_name].empty? [:class_name] = self.to_s else [:class_name] end end |
.columns(*array) ⇒ Object
Accessor for columns
Pass an array of symbols to define columns on the report
Example:
columns :total, :created_at
Calling the class method with no arguments will return an array with the defined columns.
Example:
OrdersReport.columns
# => [:total, :created_at]
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/dynamic_reports/reports.rb', line 132 def columns(*array) unless array.empty? if (array.class == Array) [:columns] = array else raise "Report columns must be specified." end else [:columns] end end |
.link(column, url, link_options = nil) ⇒ Object
Define a link for the report
Pass parameters within {}. Parameters are replaced with the row values from passed records. You do NOT need to include a parameter value as a report column for it to be used in a link. For example, you might want to generate a link with an ID field in it but not display that id in the actual report. Just include id to do this.
Example:
link :visits, ‘/reports/visit/details?date=recorded_at’
193 194 195 |
# File 'lib/dynamic_reports/reports.rb', line 193 def link(column, url, =nil) links({:column => column, :url => url, :link_options => }) end |
.links(object = nil) ⇒ Object
Return an array of links defined for the report.
175 176 177 178 179 |
# File 'lib/dynamic_reports/reports.rb', line 175 def links(object=nil) [:links] ||= [] [:links] << object if object [:links] end |
.name(value = nil) ⇒ Object
class level name accessor & setter
Set the name of the report, for example:
name "Orders Report"
47 48 49 50 51 52 53 54 |
# File 'lib/dynamic_reports/reports.rb', line 47 def name(value = nil) if value [:name] = value else # TODO: snake_case_the_name [:name] || self.class.name end end |
.on(records) ⇒ Object
Method for instanciating a report instance on a set of given records.
Example:
OrdersReport.on(@records)
Where @records is an array of
-
Objects that respond to methods for all columns defined on the report
-
Hashes that have keys corresponding to all columns defined on the report
This will return an instance of the OrdersReport bound to @records
231 232 233 |
# File 'lib/dynamic_reports/reports.rb', line 231 def on(records) new(records, @options) end |
.options ⇒ Object
class level options accessor
37 38 39 |
# File 'lib/dynamic_reports/reports.rb', line 37 def @options ||= {} end |
.styles ⇒ Object
84 85 86 |
# File 'lib/dynamic_reports/reports.rb', line 84 def styles [:styles] ||= false end |
.sub_title(value = nil) ⇒ Object
Accessor used to set the sub title:
sub_title "All orders for the account"
Or to access the already set sub title:
OrdersReport.sub_title
#=> "All orders for the account"
80 81 82 |
# File 'lib/dynamic_reports/reports.rb', line 80 def sub_title(value = nil) value ? [:sub_title] = value : [:sub_title] end |
.subreport(column, url, link_options = nil) ⇒ Object
Define an inline subreport for the report
Pass parameters within {}. Parameters are replaced with the row values from passed records. You do NOT need to include a parameter value as a report column for it to be used in a link. For example, you might want to generate a subreport with an ID field in it but not display that id in the actual report. Just include id to do this.
Example:
subreport :visits, ‘/reports/visit/details?date=recorded_at’
The subreport should be created using the same report definition style that you use for any other report.
212 213 214 215 216 |
# File 'lib/dynamic_reports/reports.rb', line 212 def subreport(column, url, =nil) ||= {} .merge!({:class => 'sub_report_link'}) links({:column => column, :url => url, :link_options => }) end |
.template(value = nil) ⇒ Object
Accessor for the template to use for the report.
Example:
template :orders # => renders orders.html.erb (erb is the default engine)
Used without argument returns the template set for the report.
OrdersReport.template # => :orders
108 109 110 111 112 113 114 |
# File 'lib/dynamic_reports/reports.rb', line 108 def template(value = nil) if value @template = value [:template] = @template end @template ||= nil end |
.title(value = nil) ⇒ Object
Accessor used to set the title:
title "All orders for the account"
Or to access the already set title:
OrdersReport.title
#=> "All orders for the account"
66 67 68 |
# File 'lib/dynamic_reports/reports.rb', line 66 def title(value = nil) value ? [:title] = value : [:title] end |
.views(*array) ⇒ Object
Views setter and accessor.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/dynamic_reports/reports.rb', line 25 def views(*array) @views ||= ["#{File::dirname(File::(__FILE__))}/views/"] unless array.empty? @views.unshift(array) @views.flatten! @views.uniq! else @views end end |
Instance Method Details
#options ⇒ Object
options accessor, all report options and configuration is stored in this.
19 20 21 |
# File 'lib/dynamic_reports/reports.rb', line 19 def @options end |
#to_csv ⇒ Object
Not Implemented Yet
292 293 294 |
# File 'lib/dynamic_reports/reports.rb', line 292 def to_csv # TODO: Write csv hook end |
#to_html(*options) ⇒ Object
Convert an instance of a report bound to a records set to an html view
Example
OrdersReport.on(@records).to_html
- options
-
:engine - one of :erb, :haml, :csv, :pdf
Note: CSV & PDF forthcoming.
281 282 283 284 285 286 287 288 289 |
# File 'lib/dynamic_reports/reports.rb', line 281 def to_html(*) view = View.new(self) # todo: this is not clean... = (.shift || {}).merge!(@options || {}) # todo: if rails is loaded set the default engine: dynamicreports::report.default_engine engine = .delete(:engine) || @@default_engine [:template] = self.class.template view.__send__("#{engine}", ) end |
#to_pdf ⇒ Object
Not Implemented Yet
297 298 299 |
# File 'lib/dynamic_reports/reports.rb', line 297 def to_pdf # TODO: Write pdf hook end |
#views ⇒ Object
views accessor, array of view paths.
14 15 16 |
# File 'lib/dynamic_reports/reports.rb', line 14 def views @views end |