Module: Renalware::Pathology::ObservationSetMethods
- Defined in:
- app/models/renalware/pathology/observations_jsonb_serializer.rb
Overview
We mix this module into any database-returned jsonb hash of observations (e.g. CurrentObservationSet.values and Letter.pathology_snapshot)
Constant Summary collapse
- VALID_SUFFIXES =
%w(_result _observed_at).freeze
Instance Method Summary collapse
-
#method_missing(method_name, *_args, &_block) ⇒ Object
Support these syntaxes values.hgb # => { result: … observed_at: …} values.HGB # => { result: … observed_at: …} values.hgb_result # => 3.3 values.hgb_observed_at # => “2017-17-01” So the values has methods corresponding to the entire set of possible OBX codes, and also methods to reach in and get their result and observed_at date.
-
#method_parts(method_name) ⇒ Object
From eg hgb_result, returns [:HGB, “result”].
- #observation_hash_or_hash_element_for(code, suffix) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *_args, &_block) ⇒ Object
Support these syntaxes
values.hgb # => { result: ... observed_at: ...}
values.HGB # => { result: ... observed_at: ...}
values.hgb_result # => 3.3
values.hgb_observed_at # => "2017-17-01"
So the values has methods corresponding to the entire set of possible OBX codes, and also methods to reach in and get their result and observed_at date.
19 20 21 22 23 24 25 26 |
# File 'app/models/renalware/pathology/observations_jsonb_serializer.rb', line 19 def method_missing(method_name, *_args, &_block) code, suffix = method_parts(method_name) if VALID_SUFFIXES.include?(suffix) || AllObservationCodes.include?(code) observation_hash_or_hash_element_for(code, suffix) else super end end |
Instance Method Details
#method_parts(method_name) ⇒ Object
From eg hgb_result, returns
- :HGB, “result”
30 31 32 33 |
# File 'app/models/renalware/pathology/observations_jsonb_serializer.rb', line 30 def method_parts(method_name) matches = method_name.to_s.match(/([^_]*)(\w*)/) [matches[1]&.upcase&.to_sym, matches[2]] end |
#observation_hash_or_hash_element_for(code, suffix) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'app/models/renalware/pathology/observations_jsonb_serializer.rb', line 35 def observation_hash_or_hash_element_for(code, suffix) obs_hash = self[code] return nil if obs_hash.nil? # the patient may not have this observation in the set return obs_hash[:result] if suffix == "_result" return Date.parse(obs_hash[:observed_at]) if suffix == "_observed_at" obs_hash end |