lib/runby_pace/pace.rb in runby_pace-0.6.114 vs lib/runby_pace/pace.rb in runby_pace-0.6.115

- old
+ new

@@ -27,15 +27,22 @@ when :long then "#{time} per #{distance_s}" end end def as_speed - multiplier = (60 / @time.total_minutes).round(2) + total_minutes = @time.total_minutes + multiplier = total_minutes > 0 ? (60 / total_minutes).round(2) : 0 distance = Runby::Distance.new(@distance.uom, multiplier) Runby::Speed.new distance end + def meters_per_minute + total_minutes = @time.total_minutes + return 0 unless total_minutes > 0 + @distance.meters / total_minutes + end + # @param [String] str is either a long-form pace such as "10:00 per mile" or a short-form pace like "10:00 p/mi" def self.parse(str) str = str.to_s.strip.chomp if str.match(/^(?<time>[:\d]*) ?(?: per |p\/)(?<distance>(?:[\d.]+ ?)?\w+)$/) time = Runby::RunbyTime.new($~[:time]) @@ -56,28 +63,31 @@ { pace: pace, error: error_message, warning: warning_message } end def <=>(other) if other.is_a? Pace - raise 'Comparing paces of different distances is not currently supported' unless @distance == other.distance - @time <=> other.time + (meters_per_minute.round(2)) <=> (other.meters_per_minute.round(2)) elsif other.is_a? RunbyTime @time <=> other.time elsif other.is_a? String return 0 if to_s == other || to_s(format: :long) == other return 0 if @time == other self <=> try_parse(other)[:pace] end end def almost_equals?(other_pace, tolerance_time = '00:01') - return @time.almost_equals?(other_pace, tolerance_time) if other_pace.is_a?(RunbyTime) + if other_pace.is_a?(RunbyTime) + return almost_equals?(Pace.new(other_pace, @distance), tolerance_time) + end if other_pace.is_a?(String) - return @time.almost_equals?(other_pace, tolerance_time) if other_pace =~ /^\d\d:\d\d$/ + return almost_equals?(Pace.new(other_pace, @distance), tolerance_time) if other_pace =~ /^\d?\d:\d\d$/ other_pace = Pace.parse(other_pace) end tolerance = RunbyTime.new(tolerance_time) - (self - tolerance) <= other_pace && (self + tolerance) >= other_pace + fast_end = (self - tolerance) + slow_end = (self + tolerance) + slow_end <= other_pace && other_pace <= fast_end end # @param [Pace, RunbyTime] other def -(other) if other.is_a?(Pace)