Class: Compendium::Report
- Inherits:
-
Object
- Object
- Compendium::Report
- Extended by:
- DSL
- Defined in:
- lib/compendium/report.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
Returns the value of attribute params.
-
#results ⇒ Object
Returns the value of attribute results.
Class Method Summary collapse
- .inherited(report) ⇒ Object
-
.method_missing(name, *args, &block) ⇒ Object
Define predicate methods for getting the report type ie.
- .report_name ⇒ Object
- .respond_to_missing?(name, include_private = false) ⇒ Boolean
-
.url(params = {}) ⇒ Object
Get a URL for this report (format: :json set by default).
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ Report
constructor
A new instance of Report.
- #metrics ⇒ Object
- #run(context = nil, options = {}) ⇒ Object
Methods included from DSL
extended, filter, metric, option, params_class, params_class=, query
Constructor Details
#initialize(params = {}) ⇒ Report
Returns a new instance of Report.
74 75 76 77 78 79 |
# File 'lib/compendium/report.rb', line 74 def initialize(params = {}) @params = self.class.params_class.new(params, ) # When creating a new report, map each query back to the report queries.each { |q| q.report = self } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
112 113 114 115 116 117 118 119 120 |
# File 'lib/compendium/report.rb', line 112 def method_missing(name, *args, &block) prefix = name.to_s.sub(/(?:_results|\?)\Z/, '').to_sym return queries[name] if queries.keys.include?(name) return results[prefix] if name.to_s.end_with? '_results' and queries.keys.include?(prefix) return params[name] if .keys.include?(name) return !!params[prefix] if name.to_s.end_with? '?' and .keys.include?(prefix) super end |
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
9 10 11 |
# File 'lib/compendium/report.rb', line 9 def params @params end |
#results ⇒ Object
Returns the value of attribute results.
9 10 11 |
# File 'lib/compendium/report.rb', line 9 def results @results end |
Class Method Details
.inherited(report) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/compendium/report.rb', line 19 def inherited(report) Compendium.reports << report # Each Report object has its own Params class so that validations can be added without affecting other # reports. However, validations also need to be inherited, so when inheriting a report, subclass its # params_class report.params_class = Class.new(self.params_class) report.params_class.class_eval %Q{ def self.model_name ActiveModel::Name.new(Compendium::Params, Compendium, "compendium.params.#{report.name.underscore rescue 'report'}") end } end |
.method_missing(name, *args, &block) ⇒ Object
Define predicate methods for getting the report type ie. r.spending? checks that r == SpendingReport
44 45 46 47 48 49 50 51 |
# File 'lib/compendium/report.rb', line 44 def method_missing(name, *args, &block) prefix = name.to_s.gsub(/[?!]\z/, '') report_class = "#{prefix}_report".classify.constantize rescue nil return self == report_class if name.to_s.end_with?('?') and Compendium.reports.include?(report_class) super end |
.report_name ⇒ Object
33 34 35 |
# File 'lib/compendium/report.rb', line 33 def report_name name.underscore.gsub(/_report$/,'').to_sym end |
.respond_to_missing?(name, include_private = false) ⇒ Boolean
53 54 55 56 57 58 59 |
# File 'lib/compendium/report.rb', line 53 def respond_to_missing?(name, include_private = false) prefix = name.to_s.gsub(/[?!]\z/, '') report_class = "#{prefix}_report".classify.constantize rescue nil return true if name.to_s.end_with?('?') and Compendium.reports.include?(report_class) super end |
.url(params = {}) ⇒ Object
Get a URL for this report (format: :json set by default)
38 39 40 |
# File 'lib/compendium/report.rb', line 38 def url(params = {}) path_helper(params) end |
Instance Method Details
#metrics ⇒ Object
104 105 106 |
# File 'lib/compendium/report.rb', line 104 def metrics Collection[Metric, queries.map{ |q| q.metrics.to_a }.flatten] end |
#run(context = nil, options = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/compendium/report.rb', line 81 def run(context = nil, = {}) self.context = context self.results = {} only = [.delete(:only)].flatten.compact except = [.delete(:except)].flatten.compact raise ArgumentError, 'cannot specify only and except options at the same time' if !only.empty? and !except.empty? (only + except).flatten.each { |q| raise ArgumentError, "invalid query #{q}" unless queries.include?(q) } queries_to_run = if !only.empty? queries.slice(*only) elsif !except.empty? queries.except(*except) else queries end queries_to_run.each{ |q| self.results[q.name] = q.run(params, ContextWrapper.wrap(context, self)) } self end |