Skip to content

Commit 18b143e

Browse files
committed
Extract Height as a value class
1 parent df24344 commit 18b143e

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

app/models/height.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Height
2+
def initialize(observation)
3+
return unless observation.present?
4+
@height = observation.value
5+
@imperial = (observation.units == 'in')
6+
end
7+
8+
def feet
9+
if @imperial
10+
(@height / 12).floor
11+
else
12+
(@height / 30).floor
13+
end
14+
end
15+
16+
def inches
17+
if @imperial
18+
@height % 12
19+
else
20+
(@height / 30 / 2.54).round
21+
end
22+
end
23+
24+
def to_s
25+
return 'N/A' unless @height.present?
26+
"#{feet} ft. #{inches} in."
27+
end
28+
end

app/models/patient.rb

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,8 @@ def age
4646
end
4747

4848
def height
49-
if observations.map(&:code_display).include?("Body Height")
50-
height_observation = [observations.select{|observation| observation.code_display == "Body Height"}].flatten.sort_by(&:date).last
51-
height_number = height_observation.value
52-
if height_observation.units == "in"
53-
height_feet = (height_number / 12).floor
54-
height_inches = height_number % 12
55-
else
56-
height_feet = (height_number / 30).floor
57-
height_inches = (height_number / 30 / 2.54).round
58-
end
59-
height = "#{height_feet} ft. #{height_inches} in."
60-
else
61-
height = "N/A"
62-
end
63-
height
49+
height_observation = observations.select { |observation| observation.code_display == "Body Height" }
50+
Height.new(height_observation).to_s
6451
end
6552

6653
def observations

0 commit comments

Comments
 (0)