Skip to content

Commit 67b31e9

Browse files
committed
Extract Weight as a value object
1 parent 18b143e commit 67b31e9

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

app/models/patient.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,8 @@ def full_name
2424
end
2525

2626
def weight
27-
if observations.map(&:code_display).include?("Body Weight")
28-
body_weight_observation = [observations.select{|observation| observation.code_display == "Body Weight"}].flatten.sort_by(&:date).last
29-
weight = "#{body_weight_observation.value} #{body_weight_observation.units}"
30-
else
31-
weight = "N/A"
32-
end
33-
weight
27+
body_weight_observation = observations.select { |observation| observation.code_display == "Body Weight"}
28+
Weight.new(body_weight_observation).to_s
3429
end
3530

3631
def birthday

app/models/weight.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Weight
2+
def initialize(observation)
3+
return unless observation.present?
4+
@weight = observation.value
5+
@unit = observation.units
6+
end
7+
8+
def to_s
9+
return 'N/A' unless @weight.present?
10+
"#{@weight} #{@unit}"
11+
end
12+
end

0 commit comments

Comments
 (0)