Class: Renalware::Pathology::AdjustObservation

Inherits:
Object
  • Object
show all
Defined in:
app/models/renalware/pathology/adjust_observation.rb

Overview

A utility class used for example by the host app to provide a generic way of adjusting an observation, usually in a Wisper listener on receipt of a broadcast pathology message probably ‘after_observation_request_persisted’.

Example usage:

# Subscribe our class to pathology messages. Renalware.configure do |config|

map = config.broadcast_subscription_map
map["Renalware::Pathology::CreateObservationRequests"] << "MyListener"

end

class MyListener

def after_observation_request_persisted(observation_request)
  service = Renalware::Pathology::AdjustObservation.new(
    observation_request: observation_request,
    code: "EGFR",
    policy: ->(patient, observation){
      .. some logic returning true or false to indicate to adjust or not
    }
  )
  service.call do |egfr_observation_requiring_adjustment|
    egfr_observation_requiring_adjustment.result += 1 or whatever
    egfr_observation_requiring_adjustment.comment = "adjusted (original = 123)"
    egfr_observation_requiring_adjustment.save!
  end
end

end

Instance Method Summary collapse

Instance Method Details

#adjustment_required?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/models/renalware/pathology/adjust_observation.rb', line 58

def adjustment_required?
  policy.call(patient, observation)
end

#call {|observation| ... } ⇒ Object

Will yield the requested observation if one found - the caller is responsible for making and saving the adjustment.

Yields:



42
43
44
45
46
47
48
# File 'app/models/renalware/pathology/adjust_observation.rb', line 42

def call
  return unless request_has_the_relevant_observation?
  return if observation_result_is_zero?
  return unless adjustment_required?

  yield observation
end

#observationObject



62
63
64
65
66
67
68
69
70
71
# File 'app/models/renalware/pathology/adjust_observation.rb', line 62

def observation
  @observation ||= begin
    observation_request
      .observations
      .joins(:description)
      .find_by(
        pathology_observation_descriptions: { code: code }
      ) || Renalware::NullObject.instance
  end
end

#observation_result_is_zero?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/models/renalware/pathology/adjust_observation.rb', line 54

def observation_result_is_zero?
  observation.result.to_f.zero?
end

#request_has_the_relevant_observation?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/renalware/pathology/adjust_observation.rb', line 50

def request_has_the_relevant_observation?
  observation.present?
end