Sha256: 10fd1b0f01427fd2049fae315aa0a572f30acc1cde2c631894efd0a6d8be2044

Contents?: true

Size: 732 Bytes

Versions: 3

Compression:

Stored size: 732 Bytes

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
    # A counter is the actual underlying data structure for aggregation.
    class Counter
      attr_reader :count, :sum

      def initialize
        @count = 0
        @sum   = BigDecimal(0)
      end

      def add(value)
        parsed_value = value.to_s.empty? ? 0 : BigDecimal(value.to_s)

        @count += 1
        @sum   += parsed_value

        self
      end

      def ave
        return BigDecimal(0) if count.zero?

        sum / count
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
proforma-1.0.2 lib/proforma/compiling/counter.rb
proforma-1.0.1 lib/proforma/compiling/counter.rb
proforma-1.0.0 lib/proforma/compiling/counter.rb