Sha256: 8582cea27245a345ef04d091d1984fe5b6b8740f7b84db463f66b838e40c6914

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

#
# Copyright (c) 2019-present, Blue Marble Payroll, LLC
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#

module Proforma
  module Compiling
    # This class is a group of aggregators that knows how to process records.
    class Aggregation
      attr_reader :aggregators, :evaluator

      def initialize(aggregators, evaluator)
        raise ArgumentError, 'evaluator is required' unless evaluator

        @aggregators  = Array(aggregators)
        @counters     = {}
        @evaluator    = evaluator

        freeze
      end

      def add(records)
        records.each do |record|
          aggregators.each do |aggregator|
            property  = aggregator.property
            value     = property.to_s.empty? ? nil : evaluator.value(record, property)
            name      = aggregator.name

            entry(name).add(value)
          end
        end

        self
      end

      def to_h
        aggregators.map { |aggregator| execute(aggregator) }.to_h
      end

      private

      attr_reader :counters

      def entry(name)
        counters[name.to_s] ||= Counter.new
      end

      def execute(aggregator)
        name      = aggregator.name
        function  = aggregator.function

        raise ArgumentError, "bad func: #{function}" unless entry(name).respond_to?(function)

        value = entry(name).send(function)

        [name, value.to_s('f')]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
proforma-1.0.2 lib/proforma/compiling/aggregation.rb
proforma-1.0.1 lib/proforma/compiling/aggregation.rb