parsley/lib/yield_parser.rb
2016-04-03 18:03:51 -05:00

19 lines
353 B
Ruby

class YieldParser
Result = Struct.new(:number, :label)
def self.parse(yield_string)
match = /(\d+(?:\.\d*)?)\s*(\w[\w\s]*)?/.match(yield_string)
case
when match.nil?
nil
when match[2]
Result.new(match[1].to_f, match[2].strip.singularize)
else
Result.new(match[1].to_f, 'each')
end
end
end