# frozen_string_literal: true
module AceOfBase
# Aggregates records together.
class AggregatedRecord
def initialize(records, aggregations)
@records = records
@aggregations = aggregations
end
AceOfBase::Record::ATTRIBUTES.each do |field|
define_method field do
aggregate_field(field)
private
attr_reader :records, :aggregations
def aggregate_field(field)
return records.first.public_send(field) unless aggregations.keys.include?(field)
send("aggregate_field_with_#{aggregations[field]}", field)
def aggregate_field_with_min(field)
records
.map { |record| record.public_send(field) }
.min
def aggregate_field_with_max(field)
def aggregate_field_with_sum(field)
.reduce(:+)
def aggregate_field_with_count(field)
.uniq
.size
def aggregate_field_with_collect(field)