Sha256: 1d5bcc347a1a59aa650ec7878e8684578b9f35b7bc2e52c599214a42e10a6393

Contents?: true

Size: 1.41 KB

Versions: 71

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

71 entries across 71 versions & 1 rubygems

Version Path
renalware-core-2.0.54 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.53 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.52 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.51 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.50 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.48 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.47 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.46 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.45 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.44 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.43 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.42 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.41 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.40 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.39 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.38 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.37 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.36 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.35 app/presenters/renalware/pathology/observations_for_codes_presenter.rb
renalware-core-2.0.34 app/presenters/renalware/pathology/observations_for_codes_presenter.rb