Sha256: ff5fc3982c0fa9d5f5a1faf48222f014e4b8f627d4edf7ee8edf7496256154ad

Contents?: true

Size: 1.35 KB

Versions: 104

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

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

104 entries across 104 versions & 1 rubygems

Version Path
renalware-core-2.0.159 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.158 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.157 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.156 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.155 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.153 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.152 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.151 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.149 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.148 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.147 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.146 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.145 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.144 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.143 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.142 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.141 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.140 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.139 app/models/renalware/pd/apd/available_overnight_volume.rb
renalware-core-2.0.138 app/models/renalware/pd/apd/available_overnight_volume.rb