Module: Converter

Included in:
Calcpace
Defined in:
lib/calcpace/converter.rb

Overview

Module to convert between different units of distance and speed

This module provides conversion methods for 42 different unit pairs, including distance units (kilometers, miles, meters, feet, etc.) and speed units (m/s, km/h, mi/h, knots, etc.).

Defined Under Namespace

Modules: Distance, Speed

Constant Summary collapse

DISTANCE_UNIT_TO_KM =

Multipliers from a supported distance-input unit to kilometres (used by methods that accept a distance_unit: keyword)

{ km: 1.0, mi: Distance::MI_TO_KM }.freeze

Instance Method Summary collapse

Instance Method Details

#constant(unit) ⇒ Float

Retrieves the conversion constant for a given unit

Examples:

constant(:km_to_mi)    #=> 0.621371
constant('km to mi')   #=> 0.621371

Parameters:

  • unit (Symbol, String)

    the unit conversion (e.g., :km_to_mi or 'km to mi')

Returns:

  • (Float)

    the conversion factor

Raises:



148
149
150
151
152
153
154
155
156
157
# File 'lib/calcpace/converter.rb', line 148

def constant(unit)
  unit = format_unit(unit) if unit.is_a?(String)
  Distance.const_get(unit.to_s.upcase)
rescue NameError
  begin
    Speed.const_get(unit.to_s.upcase)
  rescue NameError
    raise Calcpace::UnsupportedUnitError, unit
  end
end

#convert(value, unit) ⇒ Float

Converts a value from one unit to another

Examples:

convert(10, :km_to_mi)    #=> 6.21371 (10 km = 6.21 miles)
convert(5, 'mi to km')    #=> 8.0467 (5 miles = 8.05 km)

Parameters:

  • value (Numeric)

    the value to convert

  • unit (Symbol, String)

    the conversion unit (e.g., :km_to_mi or 'km to mi')

Returns:

  • (Float)

    the converted value

Raises:



72
73
74
75
76
# File 'lib/calcpace/converter.rb', line 72

def convert(value, unit)
  check_positive(value, 'Value')
  unit_constant = constant(unit)
  value * unit_constant
end

#convert_to_clocktime(seconds, compact: false) ⇒ String

Converts seconds to a clocktime string

The default (padded) format is the machine-readable one: always HH:MM:SS, with a day prefix past 24 hours. The compact format is the one a runner reads on a screen — it drops a zero hour and the leading zero of the most significant component, keeping two digits on everything after it.

Fractional seconds are truncated, not rounded, in both formats, so a predictor returning 292.9 s prints the same 4:52 either way. Past 24 hours the compact format keeps counting hours ('27:46:40') instead of adding the padded format's day prefix: a day count reintroduces the very padding and the extra unit the compact format exists to strip, and ultra finish times are read as a running hour count.

Examples:

padded (default)

convert_to_clocktime(3600)    #=> '01:00:00' (1 hour)
convert_to_clocktime(292)     #=> '00:04:52'
convert_to_clocktime(100000)  #=> '1 03:46:40' (1 day, 3 hours, 46 minutes, 40 seconds)

compact

convert_to_clocktime(45, compact: true)      #=> '0:45'
convert_to_clocktime(292, compact: true)     #=> '4:52'
convert_to_clocktime(5025, compact: true)    #=> '1:23:45'
convert_to_clocktime(100000, compact: true)  #=> '27:46:40'

Parameters:

  • seconds (Numeric)

    total seconds, zero or more

  • compact (Boolean) (defaults to: false)

    when true, return the compact display format

Returns:

  • (String)

    time in HH:MM:SS format, or "D HH:MM:SS" for durations over 24 hours; with compact: true, "M:SS" or "H:MM:SS"

Raises:



130
131
132
133
134
135
136
137
# File 'lib/calcpace/converter.rb', line 130

def convert_to_clocktime(seconds, compact: false)
  check_not_negative(seconds)
  return compact_clocktime(seconds) if compact

  days = (seconds / 86_400).to_i
  format = days.positive? ? "#{days} %H:%M:%S" : '%H:%M:%S'
  Time.at(seconds).utc.strftime(format)
end

#convert_to_seconds(time) ⇒ Integer

Converts a time string to total seconds

Examples:

convert_to_seconds('01:30:00') #=> 5400 (1 hour 30 minutes)
convert_to_seconds('05:30')    #=> 330 (5 minutes 30 seconds)

Parameters:

  • time (String)

    time string in HH:MM:SS or MM:SS format

Returns:

  • (Integer)

    total seconds



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/calcpace/converter.rb', line 86

def convert_to_seconds(time)
  parts = time.split(':').map(&:to_i)
  case parts.length
  when 2
    minute, seconds = parts
    (minute * 60) + seconds
  when 3
    hour, minute, seconds = parts
    (hour * 3600) + (minute * 60) + seconds
  else
    0
  end
end

#list_allObject



159
160
161
# File 'lib/calcpace/converter.rb', line 159

def list_all
  format_list(Distance.constants + Speed.constants)
end

#list_distanceObject



167
168
169
# File 'lib/calcpace/converter.rb', line 167

def list_distance
  format_list(Distance.constants)
end

#list_speedObject



163
164
165
# File 'lib/calcpace/converter.rb', line 163

def list_speed
  format_list(Speed.constants)
end