Sha256: 606a87907eb1d669b20ea4d7a4daae511c5d5c4f7ff6f926fb331a03ad5a4811

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 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
    # Value object that can capture basic calculations:
    # - a_sigma is the sum of data set A's values.
    # - b_sigma is the sum of data set B's values.
    # - delta is the difference: b_sigma - a_sigma.
    class Totals
      include ::Differential::Calculator::Side

      attr_reader :a_sigma,
                  :a_size,
                  :b_sigma,
                  :b_size

      def initialize
        @a_sigma = 0
        @a_size  = 0
        @b_sigma = 0
        @b_size  = 0
      end

      def delta
        b_sigma - a_sigma
      end

      def add(value, side)
        increment_sigma(value, side)
        increment_size(side)
      end

      private

      def increment_sigma(value, side)
        case side
        when A
          @a_sigma += value
        when B
          @b_sigma += value
        else
          raise ArgumentError, "unknown side: #{side}"
        end

        nil
      end

      def increment_size(side)
        case side
        when A
          @a_size += 1.0
        when B
          @b_size += 1.0
        else
          raise ArgumentError, "unknown side: #{side}"
        end

        nil
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
differential-1.0.5 lib/differential/calculator/totals.rb
differential-1.0.4 lib/differential/calculator/totals.rb
differential-1.0.3 lib/differential/calculator/totals.rb