Permalink
Browse files

Extract Height as a value class

  • Loading branch information...
1 parent df24344 commit 18b143e25ccfdf090b8a2faaed5209f36f31dfd7 @nilbus committed Jul 10, 2015
Showing with 30 additions and 15 deletions.
  1. +28 −0 app/models/height.rb
  2. +2 −15 app/models/patient.rb
View
@@ -0,0 +1,28 @@
+class Height
+ def initialize(observation)
+ return unless observation.present?
+ @height = observation.value
+ @imperial = (observation.units == 'in')
+ end
+
+ def feet
+ if @imperial
+ (@height / 12).floor
+ else
+ (@height / 30).floor
+ end
+ end
+
+ def inches
+ if @imperial
+ @height % 12
+ else
+ (@height / 30 / 2.54).round
+ end
+ end
+
+ def to_s
+ return 'N/A' unless @height.present?
+ "#{feet} ft. #{inches} in."
+ end
+end
View
@@ -46,21 +46,8 @@ def age
end
def height
- if observations.map(&:code_display).include?("Body Height")
- height_observation = [observations.select{|observation| observation.code_display == "Body Height"}].flatten.sort_by(&:date).last
- height_number = height_observation.value
- if height_observation.units == "in"
- height_feet = (height_number / 12).floor
- height_inches = height_number % 12
- else
- height_feet = (height_number / 30).floor
- height_inches = (height_number / 30 / 2.54).round
- end
- height = "#{height_feet} ft. #{height_inches} in."
- else
- height = "N/A"
- end
- height
+ height_observation = observations.select { |observation| observation.code_display == "Body Height" }
+ Height.new(height_observation).to_s
end
def observations

0 comments on commit 18b143e

Please sign in to comment.