Sha256: e1b5661e36d2befff7b489012130e2dd3ed08c0698fe64125688862fd3b1da34

Contents?: true

Size: 902 Bytes

Versions: 30

Compression:

Stored size: 902 Bytes

Contents

require_relative 'runby_range'

module Runby
  # Represents a range of speeds, from fast to slow.
  class SpeedRange < RunbyRange
    def initialize(fast, slow)
      raise "Invalid fast speed value: #{fast}" unless fast.is_a?(Numeric) || fast.is_a?(Speed)
      raise "Invalid slow speed value: #{slow}" unless slow.is_a?(Numeric) || slow.is_a?(Speed)
      @fast = Runby::Speed.new(fast)
      @slow = Runby::Speed.new(slow)
    end

    def as_pace_range
      Runby::PaceRange.new @fast.as_pace, @slow.as_pace
    end

    def to_s(format: :short)
      if @fast == @slow
        @fast.to_s(format: format)
      else
        fast_multiplier = format('%g', @fast.distance.multiplier.round(2))
        slow_multiplier = format('%g', @slow.distance.multiplier.round(2))
        @fast.to_s(format: format).sub("#{fast_multiplier}", "#{fast_multiplier}-#{slow_multiplier}")
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
runby_pace-0.6.143 lib/runby_pace/speed_range.rb
runby_pace-0.6.142 lib/runby_pace/speed_range.rb
runby_pace-0.6.141 lib/runby_pace/speed_range.rb
runby_pace-0.6.140 lib/runby_pace/speed_range.rb
runby_pace-0.6.139 lib/runby_pace/speed_range.rb
runby_pace-0.6.137 lib/runby_pace/speed_range.rb
runby_pace-0.6.136 lib/runby_pace/speed_range.rb
runby_pace-0.6.133 lib/runby_pace/speed_range.rb
runby_pace-0.6.132 lib/runby_pace/speed_range.rb
runby_pace-0.6.131 lib/runby_pace/speed_range.rb
runby_pace-0.6.130 lib/runby_pace/speed_range.rb
runby_pace-0.6.128 lib/runby_pace/speed_range.rb
runby_pace-0.6.127 lib/runby_pace/speed_range.rb
runby_pace-0.6.126 lib/runby_pace/speed_range.rb
runby_pace-0.6.125 lib/runby_pace/speed_range.rb
runby_pace-0.6.124 lib/runby_pace/speed_range.rb
runby_pace-0.6.123 lib/runby_pace/speed_range.rb
runby_pace-0.6.122 lib/runby_pace/speed_range.rb
runby_pace-0.6.121 lib/runby_pace/speed_range.rb
runby_pace-0.6.120 lib/runby_pace/speed_range.rb