Sha256: c90519d4c56650c707462e4814e6ff801d0b64b84662c330ac6858b57085809d

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

#
# Copyright (c) 2018-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 Differential
  module Calculator
    # This class is responsible for building an entire report.  Usage:
    # - Instantiate a Reader.
    # - Instantiate a Report.
    # - Feed in dataset(s) into the Reader to generate Record objects.
    # - Feed in Record objects, generated by a Reader, by calling Report#add.
    # The Report object will keep running sums and deltas of all added records.
    class Report
      include HasTotals

      def groups
        groups_by_id.values
      end

      def add(record, side)
        raise ArgumentError, 'record is required' unless record
        raise ArgumentError, 'side is required'   unless side

        totals.add(record.value, side)

        upsert_group(record, side)

        self
      end

      private

      def upsert_group(record, side)
        group_id = record.group_id

        # Create a new group if one does not exist
        groups_by_id[group_id] = Group.new(group_id) unless groups_by_id.key?(group_id)

        groups_by_id[group_id].add(record, side)

        nil
      end

      def groups_by_id
        @groups_by_id ||= {}
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
differential-1.0.2 lib/differential/calculator/report.rb
differential-1.0.1 lib/differential/calculator/report.rb