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.
-
#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.
-
.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:.
-
.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.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/dynamic_reports/reports.rb', line 201 def initialize(records, *) = .shift || {} @records = records @views = [] @views << .delete(:views) if [:views] @views += self.class.views @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 |
#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
159 160 161 162 |
# File 'lib/dynamic_reports/reports.rb', line 159 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.
135 136 137 |
# File 'lib/dynamic_reports/reports.rb', line 135 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.
140 141 142 143 144 |
# File 'lib/dynamic_reports/reports.rb', line 140 def charts(object=nil) [:charts] ||= [] [:charts] << object if object [:charts] end |
.class_name(value = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/dynamic_reports/reports.rb', line 82 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]
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/dynamic_reports/reports.rb', line 122 def columns(*array) unless array.empty? if (array.class == Array) [:columns] = array else raise "Report columns must be specified." end else [:columns] end end |
.name(value = nil) ⇒ Object
class level name accessor & setter
Set the name of the report, for example:
name "Orders Report"
41 42 43 44 45 46 47 48 |
# File 'lib/dynamic_reports/reports.rb', line 41 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
177 178 179 |
# File 'lib/dynamic_reports/reports.rb', line 177 def on(records) new(records, @options) end |
.options ⇒ Object
class level options accessor
31 32 33 |
# File 'lib/dynamic_reports/reports.rb', line 31 def @options ||= {} end |
.styles ⇒ Object
78 79 80 |
# File 'lib/dynamic_reports/reports.rb', line 78 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"
74 75 76 |
# File 'lib/dynamic_reports/reports.rb', line 74 def sub_title(value = nil) value ? [:sub_title] = value : [:sub_title] 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
102 103 104 |
# File 'lib/dynamic_reports/reports.rb', line 102 def template(value = nil) value ? [:template] = value : [:template] 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"
60 61 62 |
# File 'lib/dynamic_reports/reports.rb', line 60 def title(value = nil) value ? [:title] = value : [:title] end |
.views(*array) ⇒ Object
Views setter and accessor.
25 26 27 28 |
# File 'lib/dynamic_reports/reports.rb', line 25 def views(*array) @views ||= ["#{File::dirname(File::(__FILE__))}/views/"] array ? @views += array : @views 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
240 241 242 |
# File 'lib/dynamic_reports/reports.rb', line 240 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.
230 231 232 233 234 235 236 237 |
# File 'lib/dynamic_reports/reports.rb', line 230 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 view.__send__("#{engine}", ) end |
#to_pdf ⇒ Object
Not Implemented Yet
245 246 247 |
# File 'lib/dynamic_reports/reports.rb', line 245 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 |