Sha256: 276bf0eabe89758c3d7d91a609564e42315111d0e570309f26cba8d6fedba6bd

Contents?: true

Size: 1.41 KB

Versions: 87

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/pathology"

module Renalware
  module Pathology
    class ObservationsForCodesPresenter
      # Example usage
      #   ObservationsForCodesPresenter.new(patient: patient, codes: %w(HBA FER PHT))
      def initialize(patient:, codes:)
        @patient = Pathology.cast_patient(patient)
        @codes = Array(codes).map(&:upcase)
      end

      # Returns an array of observations reformatted to be easy to consume.
      def observations
        codes.map do |code|
          observation = observation_for(code) || NullObject.instance
          OpenStruct.new(code: code,
                         description: description_for(code).name,
                         result: observation.result,
                         observed_at: observation.observed_at)
        end
      end

      private

      attr_reader :patient, :codes

      def descriptions
        @descriptions ||= Renalware::Pathology::ObservationDescription.where(code: codes).to_a
      end

      def matching_observations
        @matching_observations ||= patient.current_observations.where(description_code: codes).to_a
      end

      def description_for(code)
        descriptions.find { |desc| desc.code.upcase == code } || NullObject.instance
      end

      def observation_for(code)
        matching_observations.find { |observation| observation.description_code.upcase == code }
      end
    end
  end
end

Version data entries

87 entries across 87 versions & 1 rubygems

Version Path
renalware-core-2.0.147 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.146 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.145 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.144 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.143 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.142 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.141 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.140 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.139 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.138 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.137 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.136 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.135 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.134 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.133 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.132 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.131 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.130 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.129 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.128 app/presenters/renalware/pathology/observations_for_codes_presenter.rb