2016-04-03 18:03:51 -05:00
|
|
|
class YieldParser
|
|
|
|
|
|
|
|
Result = Struct.new(:number, :label)
|
|
|
|
|
|
|
|
def self.parse(yield_string)
|
|
|
|
|
2016-07-05 16:48:59 -05:00
|
|
|
begin
|
|
|
|
vu = UnitConversion::parse(yield_string)
|
|
|
|
rescue UnparseableUnitError
|
|
|
|
vu = nil
|
|
|
|
end
|
2016-04-03 18:03:51 -05:00
|
|
|
|
|
|
|
case
|
2016-07-05 16:48:59 -05:00
|
|
|
when vu.nil?
|
2016-04-03 18:03:51 -05:00
|
|
|
nil
|
2016-07-05 16:48:59 -05:00
|
|
|
when vu.unit.nil?
|
|
|
|
Result.new(vu.raw_value.to_f, 'each')
|
2016-04-03 18:03:51 -05:00
|
|
|
else
|
2016-07-05 16:48:59 -05:00
|
|
|
Result.new(vu.raw_value.to_f, vu.unit.to_s.singularize)
|
2016-04-03 18:03:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|