Sha256: 140299cf6276d4044746b45b74efc6d15a6b0b7829f787f587123580b17fe6ca

Contents?: true

Size: 1.32 KB

Versions: 20

Compression:

Stored size: 1.32 KB

Contents

require_dependency "renalware/pd"
require_dependency "renalware/pd/apd/exceptions"

module Renalware
  module PD
    module APD
      class AvailableOvernightVolume
        def initialize(regime:)
          @regime = regime
        end

        def value
          @value ||= available_overnight_volume
        end

        private

        attr_accessor :regime

        def available_overnight_volume
          unique_daily_contents = available_overnight_volumes_grouped_by_day.values.compact.uniq
          if unique_daily_contents.length != 1
            raise NonUniqueOvernightVolumeError, unique_daily_contents
          end
          unique_daily_contents.first
        end

        # Builds a hash in the format:
        # {
        #   monday: 3000,
        #   tuesday: 3000,
        #   ...
        # }
        # where the value is the combined value of all ordinary bags available on that day
        def available_overnight_volumes_grouped_by_day
          Date::DAYNAME_SYMBOLS.each_with_object({}) do |day, hash|
            hash[day] = nil
            regime.bags.select(&:ordinary?).each do |bag|
              next unless bag.volume
              next unless bag.public_send(day) == true
              hash[day] = (hash[day] || 0) + bag.volume
            end
            hash
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
renalware-core-2.0.8 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.7 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.5 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.4 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.3 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.2 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.1 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.0 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.0.pre.rc13 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.0.pre.rc11 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.0.pre.rc10 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.0.pre.rc9 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.0.pre.rc8 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.0.pre.rc7 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.0.pre.rc6 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.0.pre.rc5 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.0.pre.rc4 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.0.pre.rc3 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.0.pre.rc1 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.0.pre.beta12 app/models/renalware/pd/apd/available_overnight_volume.rb