Sha256: 82528da37165b1ac72a3e38b9b906b3dab4b143c301ad879f2443fd50ad28295

Contents?: true

Size: 946 Bytes

Versions: 20

Compression:

Stored size: 946 Bytes

Contents

# frozen_string_literal: true

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)
      freeze
    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.to_s, "#{fast_multiplier}-#{slow_multiplier}")
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
runby_pace-0.62.7 lib/runby_pace/speed_range.rb
runby_pace-0.62.5 lib/runby_pace/speed_range.rb
runby_pace-0.62.4 lib/runby_pace/speed_range.rb
runby_pace-0.62.3 lib/runby_pace/speed_range.rb
runby_pace-0.62.2 lib/runby_pace/speed_range.rb
runby_pace-0.2.50.111 lib/runby_pace/speed_range.rb
runby_pace-0.61.160 lib/runby_pace/speed_range.rb
runby_pace-0.61.159 lib/runby_pace/speed_range.rb
runby_pace-0.61.158 lib/runby_pace/speed_range.rb
runby_pace-0.61.157 lib/runby_pace/speed_range.rb
runby_pace-0.61.156 lib/runby_pace/speed_range.rb
runby_pace-0.61.155 lib/runby_pace/speed_range.rb
runby_pace-0.61.154 lib/runby_pace/speed_range.rb
runby_pace-0.61.153 lib/runby_pace/speed_range.rb
runby_pace-0.6.152 lib/runby_pace/speed_range.rb
runby_pace-0.6.151 lib/runby_pace/speed_range.rb
runby_pace-0.6.150 lib/runby_pace/speed_range.rb
runby_pace-0.6.149 lib/runby_pace/speed_range.rb
runby_pace-0.6.148 lib/runby_pace/speed_range.rb
runby_pace-0.6.147 lib/runby_pace/speed_range.rb