Class: Runby::Pace
- Inherits:
-
Object
- Object
- Runby::Pace
- Includes:
- Comparable
- Defined in:
- lib/runby_pace/pace.rb
Overview
Represents a pace consisting of a distance and a time in which that distance was covered
Instance Attribute Summary collapse
-
#distance ⇒ Object
readonly
Returns the value of attribute distance.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=>(other) ⇒ Object
- #almost_equals?(other_pace, tolerance_time = '00:01') ⇒ Boolean
-
#initialize(time_or_pace, distance = '1K') ⇒ Pace
constructor
A new instance of Pace.
- #to_s ⇒ Object
Constructor Details
#initialize(time_or_pace, distance = '1K') ⇒ Pace
Returns a new instance of Pace.
8 9 10 11 12 13 14 15 |
# File 'lib/runby_pace/pace.rb', line 8 def initialize(time_or_pace, distance = '1K') if time_or_pace.is_a? Pace init_from_clone time_or_pace else @time = Runby::RunbyTime.new(time_or_pace) @distance = Runby::Distance.new(distance) end end |
Instance Attribute Details
#distance ⇒ Object (readonly)
Returns the value of attribute distance.
6 7 8 |
# File 'lib/runby_pace/pace.rb', line 6 def distance @distance end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
6 7 8 |
# File 'lib/runby_pace/pace.rb', line 6 def time @time end |
Instance Method Details
#+(other) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/runby_pace/pace.rb', line 52 def +(other) if other.is_a?(Pace) raise 'Pace arithmetic with different units is not currently supported' unless @distance == other.distance Pace.new(@time + other.time, @distance) elsif other.is_a?(RunbyTime) Pace.new(@time + other, @distance) end end |
#-(other) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/runby_pace/pace.rb', line 42 def -(other) if other.is_a?(Pace) raise 'Pace arithmetic with different units is not currently supported' unless @distance == other.distance Pace.new(@time - other.time, @distance) elsif other.is_a?(RunbyTime) Pace.new(@time - other, @distance) end end |
#<=>(other) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/runby_pace/pace.rb', line 21 def <=>(other) if other.is_a? Pace return nil unless @distance == other.distance @time <=> other.time elsif other.is_a? RunbyTime @time <=> other.time elsif other.is_a? String # TODO: Parse as Pace when Pace.parse is available @time <=> RunbyTime.parse(other) end end |
#almost_equals?(other_pace, tolerance_time = '00:01') ⇒ Boolean
33 34 35 36 37 38 39 |
# File 'lib/runby_pace/pace.rb', line 33 def almost_equals?(other_pace, tolerance_time = '00:01') if other_pace.is_a?(String) other_pace = Pace.parse(other_pace) end tolerance = RunbyTime.new(tolerance_time) self >= (other_pace - tolerance) && self <= (other_pace + tolerance) end |
#to_s ⇒ Object
17 18 19 |
# File 'lib/runby_pace/pace.rb', line 17 def to_s "#{time} per #{distance.pluralized_uom}" end |