class Runby::RunbyTimeParser

Helper class which parses strings and returns new RunbyTime(s)

Public Class Methods

decimal?(str) click to toggle source
# File lib/runby_pace/runby_time_parser.rb, line 20
def self.decimal?(str)
  str.match?(/^\d+[,. ]\d+$/)
end
extract_minutes_from_decimal(decimal_str) click to toggle source
# File lib/runby_pace/runby_time_parser.rb, line 32
def self.extract_minutes_from_decimal(decimal_str)
  decimal_parts = decimal_str.split(/[,. ]/)
  minutes = decimal_parts[0].to_i
  seconds = (decimal_parts[1].to_i / 10.0 * 60).to_i
  TimeParts.new([seconds, minutes])
end
extract_minutes_from_integer(integer_str) click to toggle source
# File lib/runby_pace/runby_time_parser.rb, line 39
def self.extract_minutes_from_integer(integer_str)
  TimeParts.new([0, integer_str.to_i])
end
integer?(str) click to toggle source
# File lib/runby_pace/runby_time_parser.rb, line 24
def self.integer?(str)
  str.match?(/^\d+$/)
end
parse(str) click to toggle source
# File lib/runby_pace/runby_time_parser.rb, line 6
def self.parse(str)
  time = str.to_s.strip.chomp
  if time_string?(time)
    parts = TimeParts.new(time.split(':').reverse)
  elsif integer?(time)
    parts = extract_minutes_from_integer time
  elsif decimal?(time)
    parts = extract_minutes_from_decimal time
  else
    raise 'Invalid time format'
  end
  RunbyTime.new(parts)
end
time_string?(str) click to toggle source
# File lib/runby_pace/runby_time_parser.rb, line 28
def self.time_string?(str)
  str.match?(/^\d?\d(:\d\d)+$/)
end