Sha256: 14118fc81a212d51a1b8867bdadb87ebaf1ba817acc512f3d6b07cb8ee48dd7d

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

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

    # Create a new speed range from an existing pace range.
    def self.from_pace_range(pace_range)
      pace_range.as_speed_range
    end

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

    def to_s(format: :long)
      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

1 entries across 1 versions & 1 rubygems

Version Path
runby_pace-0.6.108 lib/runby_pace/speed_range.rb