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.
- .respond_to_missing?(name, include_private = false) ⇒ Boolean
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, metric, option, params_class, params_class=, query
Constructor Details
#initialize(params = {}) ⇒ Report
Returns a new instance of Report.
50 51 52 53 54 55 |
# File 'lib/compendium/report.rb', line 50 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)
88 89 90 91 92 93 94 95 96 |
# File 'lib/compendium/report.rb', line 88 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
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/compendium/report.rb', line 16 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
32 33 34 35 36 37 38 39 |
# File 'lib/compendium/report.rb', line 32 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 |
.respond_to_missing?(name, include_private = false) ⇒ Boolean
41 42 43 44 45 46 47 |
# File 'lib/compendium/report.rb', line 41 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 |
Instance Method Details
#metrics ⇒ Object
80 81 82 |
# File 'lib/compendium/report.rb', line 80 def metrics Collection[Metric, queries.map{ |q| q.metrics.to_a }.flatten] end |
#run(context = nil, options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/compendium/report.rb', line 57 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 |