Sha256: 199a1088a5658eba8d182610bc0c9e4147d22b88034883d151437b610e1bf73e

Contents?: true

Size: 600 Bytes

Versions: 4

Compression:

Stored size: 600 Bytes

Contents

module Counter::Calculated
  extend ActiveSupport::Concern

  included do
    def calculate!
      new_value = calculate
      update! value: new_value unless new_value.nil?
    end

    def calculate
      counters = counters_for_calculation
      # If any of the counters are missing, we can't calculate
      return if counters.any?(&:nil?)

      definition.calculated_from.call(*counters)
    end

    def counters_for_calculation
      # Fetch the dependant counters
      definition.dependent_counters.map do |counter|
        parent.counters.find_counter(counter)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
counterwise-0.1.5 app/models/concerns/counter/calculated.rb
counterwise-0.1.4 app/models/concerns/counter/calculated.rb
counterwise-0.1.3 app/models/concerns/counter/calculated.rb
counterwise-0.1.2 app/models/concerns/counter/calculated.rb