Class: Repor::Aggregators::BaseAggregator

Inherits:
Object
  • Object
show all
Defined in:
lib/repor/aggregators/base_aggregator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, report, opts = {}) ⇒ BaseAggregator

Returns a new instance of BaseAggregator.



6
7
8
9
10
# File 'lib/repor/aggregators/base_aggregator.rb', line 6

def initialize(name, report, opts={})
  @name = name
  @report = report
  @opts = opts
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/repor/aggregators/base_aggregator.rb', line 4

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



4
5
6
# File 'lib/repor/aggregators/base_aggregator.rb', line 4

def opts
  @opts
end

#reportObject (readonly)

Returns the value of attribute report.



4
5
6
# File 'lib/repor/aggregators/base_aggregator.rb', line 4

def report
  @report
end

Instance Method Details

#aggregate(groups) ⇒ Object

This is the method called by Repor::Report. It should return a hash of array keys (of grouper values) mapped to aggregation values.



14
15
16
17
18
19
20
21
# 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