Class: Compendium::Report

Inherits:
Object
  • Object
show all
Extended by:
DSL
Defined in:
lib/compendium/report.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

exports, extended, filter, metric, option, params_class, params_class=, query, table

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, options)

  # 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)



116
117
118
119
120
121
122
123
124
# File 'lib/compendium/report.rb', line 116

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') && queries.keys.include?(prefix)
  return params[name] if options.keys.include?(name)
  return !!params[prefix] if name.to_s.end_with?('?') && options.keys.include?(prefix)
  super
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



9
10
11
# File 'lib/compendium/report.rb', line 9

def params
  @params
end

#resultsObject

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?('?') && Compendium.reports.include?(report_class)

  super
end

.report_nameObject



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

Returns:

  • (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?('?') && 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

#exports?(type) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/compendium/report.rb', line 108

def exports?(type)
  return exporters[type.to_sym]
end

#metricsObject



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

Raises:

  • (ArgumentError)


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, options = {})
  self.context = context
  self.results = {}

  only = Array.wrap(options.delete(:only)).compact
  except = Array.wrap(options.delete(:except)).compact

  raise ArgumentError, 'cannot specify only and except options at the same time' if !only.empty? && !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