Class: Runby::RunbyTimeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/runby_pace/runby_time_parser.rb

Overview

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

Defined Under Namespace

Classes: TimeParts

Class Method Summary collapse

Class Method Details

.decimal?(str) ⇒ Boolean

Returns:

  • (Boolean)

20
21
22
# 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) ⇒ Object


32
33
34
35
36
37
# 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) ⇒ Object


39
40
41
# 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) ⇒ Boolean

Returns:

  • (Boolean)

24
25
26
# File 'lib/runby_pace/runby_time_parser.rb', line 24

def self.integer?(str)
  str.match?(/^\d+$/)
end

.parse(str) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
# 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) ⇒ Boolean

Returns:

  • (Boolean)

28
29
30
# File 'lib/runby_pace/runby_time_parser.rb', line 28

def self.time_string?(str)
  str.match?(/^\d?\d(:\d\d)+$/)
end