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
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
-
#constant(unit) ⇒ Float
Retrieves the conversion constant for a given unit.
-
#convert(value, unit) ⇒ Float
Converts a value from one unit to another.
-
#convert_to_clocktime(seconds, compact: false) ⇒ String
Converts seconds to a clocktime string.
-
#convert_to_seconds(time) ⇒ Integer
Converts a time string to total seconds.
- #list_all ⇒ Object
- #list_distance ⇒ Object
- #list_speed ⇒ Object
Instance Method Details
#constant(unit) ⇒ Float
Retrieves the conversion constant for a given unit
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
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.
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
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_all ⇒ Object
159 160 161 |
# File 'lib/calcpace/converter.rb', line 159 def list_all format_list(Distance.constants + Speed.constants) end |
#list_distance ⇒ Object
167 168 169 |
# File 'lib/calcpace/converter.rb', line 167 def list_distance format_list(Distance.constants) end |
#list_speed ⇒ Object
163 164 165 |
# File 'lib/calcpace/converter.rb', line 163 def list_speed format_list(Speed.constants) end |