Class: Runby::Speed
- Inherits:
-
Object
- Object
- Runby::Speed
- 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
-
#distance ⇒ Object
readonly
Returns the value of attribute distance.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #as_pace ⇒ Object
-
#initialize(distance_or_multiplier, units = :km) ⇒ Speed
constructor
A new instance of Speed.
- #to_s(format: :short) ⇒ Object
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
#distance ⇒ Object (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
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 |
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_pace ⇒ Object
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 |