Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'natural_language/log_entry_parsers/standard_parser'
- class NumberDistanceParser < StandardParser
- def regex
- # matching "3 miles", "5k", etc
- /
- ( \d+ (?:\.\d+)? ) # match integer or float
- \s? # optional whitespace
- #{DISTANCE_POSSIBLE}
- /ix
- end
- def handle_match(match_data, curr_log_entry)
- le = curr_log_entry
- unless le
- le = LogEntry.new
- end
- le.user_distance = match_data[1]
- if match_data[2].upcase.starts_with?("M")
- le.user_unit = UserUnit.find_by_ref("MILES")
- elsif match_data[2].upcase.starts_with?("K")
- le.user_unit = UserUnit.find_by_ref("KILO")
- end
- le
- end
- end
Add Comment
Please, Sign In to add comment