class Repor::Aggregators::BaseAggregator
Attributes
name[R]
opts[R]
report[R]
Public Class Methods
new(name, report, opts={})
click to toggle source
# File lib/repor/aggregators/base_aggregator.rb, line 6 def initialize(name, report, opts={}) @name = name @report = report @opts = opts end
Public Instance Methods
aggregate(groups)
click to toggle source
This is the method called by Repor::Report
. It should return a hash of array keys (of grouper values) mapped to aggregation values.
# File lib/repor/aggregators/base_aggregator.rb, line 14 def aggregate(groups) query = aggregation(relate(groups)) result = ActiveRecord::Base.connection.select_all(query) result.cast_values.each_with_object(Hash.new(default_y_value)) do |values, h| row = result.columns.zip(values).to_h h[x_value_of(row)] = y_value_of(row) end end
Private Instance Methods
aggregation(groups)
click to toggle source
This is the method any aggregator must implement. It should return a relation with the aggregator value SELECTed as the ‘sql_value_name`.
# File lib/repor/aggregators/base_aggregator.rb, line 27 def aggregation(groups) raise NotImplementedError end
default_y_value()
click to toggle source
What value should be returned if there are no results for a certain key? For count, that’s clearly 0; for min/max, that may be less clear.
# File lib/repor/aggregators/base_aggregator.rb, line 57 def default_y_value opts.fetch(:default_value, 0) end
expression()
click to toggle source
# File lib/repor/aggregators/base_aggregator.rb, line 51 def expression opts.fetch(:expression, "#{report.table_name}.#{name}") end
relate(groups)
click to toggle source
# File lib/repor/aggregators/base_aggregator.rb, line 43 def relate(groups) relation.call(groups) end
relation()
click to toggle source
# File lib/repor/aggregators/base_aggregator.rb, line 47 def relation opts.fetch(:relation, ->(r) { r }) end
sql_value_name()
click to toggle source
# File lib/repor/aggregators/base_aggregator.rb, line 31 def sql_value_name "_report_aggregator_#{name}" end
x_value_of(row)
click to toggle source
# File lib/repor/aggregators/base_aggregator.rb, line 35 def x_value_of(row) report.groupers.map { |g| g.extract_sql_value(row) } end
y_value_of(row)
click to toggle source
# File lib/repor/aggregators/base_aggregator.rb, line 39 def y_value_of(row) row[sql_value_name] end