Class: Runby::Distance
- Inherits:
-
Object
- Object
- Runby::Distance
- Defined in:
- lib/runby_pace/distance.rb
Overview
Represents a distance (distance UOM and multiplier)
Instance Attribute Summary collapse
-
#multiplier ⇒ Object
readonly
Returns the value of attribute multiplier.
-
#uom ⇒ Object
readonly
Returns the value of attribute uom.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #convert_to(uom) ⇒ Object
-
#initialize(uom = :km, multiplier = 1) ⇒ Distance
constructor
A new instance of Distance.
- #kilometers ⇒ Object
- #meters ⇒ Object
- #pluralized_uom ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(uom = :km, multiplier = 1) ⇒ Distance
Returns a new instance of Distance.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/runby_pace/distance.rb', line 5 def initialize(uom = :km, multiplier = 1) case uom when Distance return init_from_clone uom when DistanceUnit return init_from_distance_unit uom, multiplier when String return init_from_string uom when Symbol return init_from_symbol uom, multiplier else raise 'Invalid distance unit of measure' end end |
Instance Attribute Details
#multiplier ⇒ Object (readonly)
Returns the value of attribute multiplier.
4 5 6 |
# File 'lib/runby_pace/distance.rb', line 4 def multiplier @multiplier end |
#uom ⇒ Object (readonly)
Returns the value of attribute uom.
4 5 6 |
# File 'lib/runby_pace/distance.rb', line 4 def uom @uom end |
Class Method Details
.parse(str) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/runby_pace/distance.rb', line 34 def self.parse(str) str = str.strip.chomp.downcase multiplier = str.scan(/[\d,.]+/).first.to_f uom = str.scan(/[-_a-z ]+$/).first raise "Unable to find distance unit in '#{str}'" if uom.nil? parsed_uom = Runby::DistanceUnit.parse uom raise "'#{uom.strip}' is not recognized as a distance unit" if parsed_uom.nil? self.new parsed_uom, multiplier end |
.try_parse(str) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/runby_pace/distance.rb', line 46 def self.try_parse(str) distance, = nil begin distance = parse str rescue StandardError => ex = "#{ex.}" end { distance: distance, error: } end |
Instance Method Details
#==(other) ⇒ Object
68 69 70 71 |
# File 'lib/runby_pace/distance.rb', line 68 def ==(other) raise "Cannot compare Runby::Distance to #{other.class}" unless other.is_a? Distance @uom == other.uom && @multiplier == other.multiplier end |
#convert_to(uom) ⇒ Object
20 21 22 23 24 |
# File 'lib/runby_pace/distance.rb', line 20 def convert_to(uom) target_uom = DistanceUnit.new uom target_multiplier = kilometers / (target_uom.conversion_factor * 1.0) Distance.new target_uom, target_multiplier end |
#kilometers ⇒ Object
30 31 32 |
# File 'lib/runby_pace/distance.rb', line 30 def kilometers @multiplier * @uom.conversion_factor end |
#meters ⇒ Object
26 27 28 |
# File 'lib/runby_pace/distance.rb', line 26 def meters kilometers * 1000.0 end |
#pluralized_uom ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/runby_pace/distance.rb', line 60 def pluralized_uom uom_description = @uom.description.downcase if @multiplier > 1 uom_description += 's' end uom_description end |
#to_s ⇒ Object
56 57 58 |
# File 'lib/runby_pace/distance.rb', line 56 def to_s "#{format('%g', @multiplier.round(2))} #{pluralized_uom}" end |