Class: ActiveReporter::Report

Inherits:
Object
  • Object
show all
Includes:
Aggregation, Definition, Metrics, Validation
Defined in:
lib/active_reporter/report.rb,
lib/active_reporter/report/metrics.rb,
lib/active_reporter/report/definition.rb,
lib/active_reporter/report/validation.rb,
lib/active_reporter/report/aggregation.rb

Defined Under Namespace

Modules: Aggregation, Definition, Metrics, Validation

Constant Summary

Constants included from Definition

Definition::METRICS

Instance Attribute Summary collapse

Attributes included from Validation

#errors

Instance Method Summary collapse

Methods included from Aggregation

#flat_data, #hashed_data, #nested_data, #raw_data, #source_data, #total_data

Methods included from Metrics

#aggregators, #all_aggregators, #base_relation, #calculators, #dimensions, #evaluators, #fields, #filters, #grouper_names, #groupers, #groups, #records, #relation, #relators, #total_report, #trackers

Methods included from Validation

#validate_aggregators!, #validate_calculators!, #validate_configuration!, #validate_groupers!, #validate_params!, #validate_parent_report!, #validate_total_report!, #validate_trackers!

Constructor Details

#initialize(params = {}) ⇒ Report

Returns a new instance of Report.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/active_reporter/report.rb', line 12

def initialize(params = {})
  @params = params

  # prepare the params for processing
  clean_params

  # When using a Calculator you may need the parent report data. Pass in a ActiveReporter::Report object when
  # instantiating a new ActiveReporter::Report instance as :parent_report. This will allow you to calculate a data
  # based on the #total_report of this passed :parent_report. For example, if the parent report includes a sum
  # aggregated 'views' column, the child report can use Report::Calculator::Ratio to caluclate the ratio of 'views'
  # on a given row versus the total 'views' from the parent report.
  @parent_report = @params.delete(:parent_report)
  @parent_groupers = @params.delete(:parent_groupers) || ( grouper_names & Array(parent_report&.grouper_names) )

  # Supplements -> supplemental reports and data
  #
  # we need 2 items:
  # 1- the #supplements, a hash of reports and data, we can refrence by name
  # => this is passed into the report initializer, the key is the name the value is the enrire report object
  # 2- a configuration class, this will allow you to specify a special aggregator in the report class that
  # => take a block. The block defines { |key, row| return_value }, the block has access to the data in
  # #supplements available to use when calculating return the value.
  @supplements = @params.delete(:supplements)

  # You may pass in pre-compiled :row_data if you don't want ActiveReporter to compile this data for you. All
  # :calculators and :trackers will still be processed when :raw_data is passed in.
  @raw_data = @params.delete(:raw_data)

  # You may pass in pre-aggregated :total_report object as an instance of ActiveReporter::Report if you don't want
  # ActiveReporter to total this data for you no additional processing is completed on #total_report when a
  # :total_report value is passed.
  @total_report = @params.delete(:total_report)

  # Instead or in addition to passing a :total_report you may pass :total_data, which is used when report data is
  # built. In the case that both :total_report and :total_data are passed, the :total_report object will be used
  # for all :calculators. If only :total_data is passed, the :total_report object will not be populated and no
  # :calculators will be processed. Data in :total_data is never altered or appended.
  @total_data = @params.delete(:total_data) || @total_report&.data

  validate_params!

  # After params are parsed and validated you can call #data (or any derivitive of: #raw_data, #flat_data,
  # #hashed_data, #nested_data, etc.) on the ActiveReporter::Report object to #aggregate the data. This will
  # aggregate all the raw data by the configured dimensions, process any calculators, and then process any
  # trackers.
  
  # Caclulators calculate values using the current row data and the #parent_report.
  
  # Trackers calculate values using the current row data and prior row data.


  # If pre-compiled raw data was passed in, process all :calculators and :trackers now.
  aggregate if  @raw_data.present? && ( @params.include?(:calculators) || @params.include?(:trackers) )
  total if @total_data.present?
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/active_reporter/report.rb', line 10

def params
  @params
end

#parent_groupersObject (readonly)

Returns the value of attribute parent_groupers.



10
11
12
# File 'lib/active_reporter/report.rb', line 10

def parent_groupers
  @parent_groupers
end

#parent_reportObject (readonly)

Returns the value of attribute parent_report.



10
11
12
# File 'lib/active_reporter/report.rb', line 10

def parent_report
  @parent_report
end

#supplementsObject (readonly)

Returns the value of attribute supplements.



10
11
12
# File 'lib/active_reporter/report.rb', line 10

def supplements
  @supplements
end