Class: ActsAsCalculator::AggregateResults

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_calculator/aggregate_results.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records:, formula:, as_of: nil, group_by: nil) ⇒ AggregateResults

Returns a new instance of AggregateResults.



11
12
13
14
15
16
# File 'lib/acts_as_calculator/aggregate_results.rb', line 11

def initialize(records:, formula:, as_of: nil, group_by: nil)
  @records = records
  @formula = formula
  @as_of = as_of
  @group_by = group_by
end

Class Method Details

.callObject



7
8
9
# File 'lib/acts_as_calculator/aggregate_results.rb', line 7

def self.call(...)
  new(...).call
end

Instance Method Details

#callObject



18
19
20
21
22
# File 'lib/acts_as_calculator/aggregate_results.rb', line 18

def call
  return total(rows) if group_by.nil?

  rows.group_by { |record| group_key(record) }.transform_values { |group| total(group) }
end