Last active
August 29, 2015 14:22
-
-
Save ZempTime/6c9701935b23d5c8cba1 to your computer and use it in GitHub Desktop.
Weight Formatter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WeightFormatter | |
PROCESSABLE_WEIGHT_UNITS = ["grams", "ounces"] | |
def self.call(weight_unit, weight) | |
unless PROCESSABLE_WEIGHT_TYPES.include?(weight_unit) | |
return "#{weight} #{weight_unit}" | |
end | |
send("process_#{weight_unit.downcase}", weight) | |
end | |
private | |
def self.process_grams(weight) | |
oz = (weight.to_i * 0.035274).round | |
lbs = oz / 16 | |
ounces = oz % 16 | |
"#{lbs} lbs. #{ounces} oz." | |
end | |
def self.process_ounces(weight) | |
lbs = weight.to_i.round / 16 | |
ounces = weight.to_i.round % 16 | |
"#{lbs} lbs. #{ounces} oz." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment