Class: Runby::Speed

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/runby_pace/speed.rb

Overview

Represents a speed consisting of a distance and a unit of time in which that distance was covered

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(distance_or_multiplier, units = :km) ⇒ Speed

Returns a new instance of Speed.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/runby_pace/speed.rb', line 19

def initialize(distance_or_multiplier, units = :km)
  case distance_or_multiplier
  when Distance
    init_from_distance distance_or_multiplier
  when String
    # Already tried to parse it as a Speed string. Try parsing it as a Distance string.
    init_from_distance_string distance_or_multiplier
  when Numeric
    init_from_multiplier(distance_or_multiplier, units)
  else
    raise "Unable to initialize Runby::Speed from #{distance_or_multiplier}"
  end
  freeze
end

Instance Attribute Details

#distanceObject (readonly)

Returns the value of attribute distance.



8
9
10
# File 'lib/runby_pace/speed.rb', line 8

def distance
  @distance
end

Class Method Details

.new(distance_or_multiplier, units = :km) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/runby_pace/speed.rb', line 10

def self.new(distance_or_multiplier, units = :km)
  return distance_or_multiplier if distance_or_multiplier.is_a? Speed
  if distance_or_multiplier.is_a? String
    parsed_speed = Speed.try_parse(distance_or_multiplier)
    return parsed_speed[:speed] unless parsed_speed[:error]
  end
  super
end

.parse(str) ⇒ Object

Parameters:

  • str (String)

    is either a long-form speed such as “7.5 miles per hour” or a short-form speed like “7.5mi/ph”



49
50
51
52
53
54
55
# File 'lib/runby_pace/speed.rb', line 49

def self.parse(str)
  str = str.to_s.strip.chomp
  match = str.match(%r{^(?<distance>\d+(?:\.\d+)? ?[A-Za-z]+)(?: per hour|\/ph)$})
  raise "Invalid speed format (#{str})" unless match
  distance = Runby::Distance.new(match[:distance])
  Speed.new distance
end

.try_parse(str) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/runby_pace/speed.rb', line 57

def self.try_parse(str)
  speed = nil
  error_message = nil
  warning_message = nil
  begin
    speed = Speed.parse str
  rescue StandardError => ex
    error_message = "#{ex.message} (#{str})"
  end
  { speed: speed, error: error_message, warning: warning_message }
end

Instance Method Details

#<=>(other) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/runby_pace/speed.rb', line 69

def <=>(other)
  raise "Unable to compare Runby::Speed to #{other.class}(#{other})" unless [Speed, String].include? other.class
  if other.is_a? String
    return 0 if to_s == other || to_s(format: :long) == other
    self <=> try_parse(other)[:speed]
  end
  @distance <=> other.distance
end

#as_paceObject



43
44
45
46
# File 'lib/runby_pace/speed.rb', line 43

def as_pace
  time = Runby::RunbyTime.from_minutes(60.0 / @distance.multiplier)
  Runby::Pace.new(time, @distance.uom)
end

#to_s(format: :short) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/runby_pace/speed.rb', line 34

def to_s(format: :short)
  distance = @distance.to_s(format: format)
  case format
  when :short then "#{distance}/ph"
  when :long then "#{distance} per hour"
  else raise "Invalid string format #{format}"
  end
end