class Runby::Speed

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

Attributes

distance[R]

Public Class Methods

new(distance_or_multiplier, units = :km) click to toggle source
Calls superclass method
# 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
new(distance_or_multiplier, units = :km) click to toggle source
# 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
parse(str) click to toggle source

@param [String] str is either a long-form speed such as “7.5 miles per hour” or a short-form speed like “7.5mi/ph”

# 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) click to toggle source
# 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

Public Instance Methods

<=>(other) click to toggle source
# 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_pace() click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

init_from_distance(distance) click to toggle source
# File lib/runby_pace/speed.rb, line 80
def init_from_distance(distance)
  @distance = distance
end
init_from_distance_string(str) click to toggle source
# File lib/runby_pace/speed.rb, line 88
def init_from_distance_string(str)
  results = Distance.try_parse(str)
  unless results[:error]
    @distance = results[:distance]
    return
  end
  raise "'#{str}' is not recognized as a Speed or a Distance"
end
init_from_multiplier(multiplier, uom) click to toggle source
# File lib/runby_pace/speed.rb, line 84
def init_from_multiplier(multiplier, uom)
  @distance = Distance.new(uom, multiplier)
end