class Runby::DistanceUnit
Represents the distance units (e.g. kilometers, miles) used in paces
including the human-readable description of each unit and the factor used to convert it to kilometers.
Constants
- UOM_DEFINITIONS
Attributes
conversion_factor[R]
description[R]
symbol[R]
Public Class Methods
known_uom?(symbol)
click to toggle source
# File lib/runby_pace/distance_unit.rb, line 60 def self.known_uom?(symbol) UOM_DEFINITIONS.key?(symbol) end
new(unit_of_measure)
click to toggle source
Calls superclass method
# File lib/runby_pace/distance_unit.rb, line 10 def self.new(unit_of_measure) return unit_of_measure if unit_of_measure.is_a? DistanceUnit return DistanceUnit.parse(unit_of_measure) if unit_of_measure.is_a? String super end
new(unit_of_measure)
click to toggle source
# File lib/runby_pace/distance_unit.rb, line 16 def initialize(unit_of_measure) raise "':#{unit_of_measure}' is an unknown unit of measure" unless DistanceUnit.known_uom? unit_of_measure @symbol = unit_of_measure @conversion_factor = UOM_DEFINITIONS[@symbol][:conversion_factor] @description = UOM_DEFINITIONS[@symbol][:description] freeze end
parse(description)
click to toggle source
# File lib/runby_pace/distance_unit.rb, line 36 def self.parse(description) return new description if description.is_a? Symbol description = description.strip.chomp.downcase found_uom = nil UOM_DEFINITIONS.each do |uom, details| if details[:synonyms].include? description found_uom = uom break end end raise "Error parsing distance unit '#{description}'" unless found_uom DistanceUnit.new found_uom end
try_parse(str)
click to toggle source
# File lib/runby_pace/distance_unit.rb, line 50 def self.try_parse(str) uom, error_message = nil begin uom = parse str rescue StandardError => ex error_message = ex.message end { uom: uom, error: error_message } end
Public Instance Methods
==(other)
click to toggle source
# File lib/runby_pace/distance_unit.rb, line 64 def ==(other) if other.is_a? DistanceUnit @symbol == other.symbol elsif other.is_a? String self == DistanceUnit.parse(other) else raise "Unable to compare DistanceUnit to #{other.class}(#{other})" end end
description_plural()
click to toggle source
# File lib/runby_pace/distance_unit.rb, line 32 def description_plural UOM_DEFINITIONS[@symbol][:description_plural] end
to_s(format: :long, pluralize: false)
click to toggle source
# File lib/runby_pace/distance_unit.rb, line 24 def to_s(format: :long, pluralize: false) case format when :short then @symbol.to_s when :long then pluralize ? description_plural : @description else raise "Invalid string format #{format}" end end