Class: RunbyPace::PaceTime
- Inherits:
-
Object
- Object
- RunbyPace::PaceTime
- Defined in:
- lib/runby_pace/pace_time.rb
Instance Attribute Summary collapse
-
#minutes_part ⇒ Object
readonly
Returns the value of attribute minutes_part.
-
#seconds_part ⇒ Object
readonly
Returns the value of attribute seconds_part.
-
#time_s ⇒ Object
readonly
Returns the value of attribute time_s.
Class Method Summary collapse
- .from_minutes(total_minutes) ⇒ Object
- .from_seconds(total_seconds) ⇒ Object
- .parse(str) ⇒ Object
- .try_parse(str, is_five_k = false) ⇒ Object
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
- #almost_equals?(other_time, tolerance_time = '00:01') ⇒ Boolean
-
#initialize(time) ⇒ PaceTime
constructor
A new instance of PaceTime.
- #to_s ⇒ Object
- #total_minutes ⇒ Object
- #total_seconds ⇒ Object
Constructor Details
#initialize(time) ⇒ PaceTime
Returns a new instance of PaceTime.
5 6 7 8 9 10 11 12 13 |
# File 'lib/runby_pace/pace_time.rb', line 5 def initialize(time) if time.is_a?(String) || time.is_a?(Symbol) init_from_string time elsif time.is_a?(PaceTime) init_from_clone time elsif time.is_a?(Hash) init_from_hash time end end |
Instance Attribute Details
#minutes_part ⇒ Object (readonly)
Returns the value of attribute minutes_part.
3 4 5 |
# File 'lib/runby_pace/pace_time.rb', line 3 def minutes_part @minutes_part end |
#seconds_part ⇒ Object (readonly)
Returns the value of attribute seconds_part.
3 4 5 |
# File 'lib/runby_pace/pace_time.rb', line 3 def seconds_part @seconds_part end |
#time_s ⇒ Object (readonly)
Returns the value of attribute time_s.
3 4 5 |
# File 'lib/runby_pace/pace_time.rb', line 3 def time_s @time_s end |
Class Method Details
.from_minutes(total_minutes) ⇒ Object
23 24 25 |
# File 'lib/runby_pace/pace_time.rb', line 23 def self.from_minutes(total_minutes) from_seconds(total_minutes * 60.0) end |
.from_seconds(total_seconds) ⇒ Object
16 17 18 19 20 |
# File 'lib/runby_pace/pace_time.rb', line 16 def self.from_seconds(total_seconds) minutes = total_seconds.abs.to_i / 60 seconds = total_seconds.abs.to_i % 60 PaceTime.new("#{'%02d' % minutes}:#{'%02d' % seconds}") end |
.parse(str) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/runby_pace/pace_time.rb', line 27 def self.parse(str) time = str.to_s.strip.chomp is_negative = false if time[0] == '-' is_negative = true time = time[1..-1] end if time.match(/^\d?\d:\d\d$/) parts = time.split(':') minutes_part = parts[0].to_i seconds_part = parts[1].to_i elsif time.match(/^\d+$/) minutes_part = time.to_i seconds_part = 0 elsif time.match(/^\d+[,\. ]\d+$/) parts = time.split(/[,\. ]/) minutes_part = parts[0].to_i seconds_part = (parts[1].to_i / 10.0 * 60).to_i else raise 'Invalid time format' end raise 'Minutes must be less than 100' if minutes_part > 99 raise 'Seconds must be less than 60' if seconds_part > 59 if is_negative minutes_part *= -1 seconds_part *= -1 end time_formatted = "#{minutes_part.to_s.rjust(2, '0')}:#{seconds_part.to_s.rjust(2, '0')}" PaceTime.new(time_s: time_formatted, minutes_part: minutes_part, seconds_part: seconds_part) end |
.try_parse(str, is_five_k = false) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/runby_pace/pace_time.rb', line 62 def self.try_parse(str, is_five_k = false) time, , = nil begin time = parse str rescue Exception => ex = "#{ex.} (#{str})" end # Break out these sanity checks into their own class if we add any more. if !time.nil? && is_five_k if time.minutes_part < 14 then = '5K times of less than 14:00 are unlikely' end if time.total_seconds > (42 * 60) then = '5K times of greater than 42:00 are not fully supported' end end { time: time, error: , warning: } end |
Instance Method Details
#+(other) ⇒ Object
99 100 101 102 103 |
# File 'lib/runby_pace/pace_time.rb', line 99 def +(other) if other.is_a?(PaceTime) PaceTime.from_seconds(total_seconds + other.total_seconds) end end |
#-(other) ⇒ Object
92 93 94 95 96 |
# File 'lib/runby_pace/pace_time.rb', line 92 def -(other) if other.is_a?(PaceTime) PaceTime.from_seconds(total_seconds - other.total_seconds) end end |
#<(other) ⇒ Object
133 134 135 136 137 |
# File 'lib/runby_pace/pace_time.rb', line 133 def <(other) if other.is_a?(PaceTime) total_seconds < other.total_seconds end end |
#<=(other) ⇒ Object
139 140 141 142 143 |
# File 'lib/runby_pace/pace_time.rb', line 139 def <=(other) if other.is_a?(PaceTime) total_seconds <= other.total_seconds end end |
#==(other) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/runby_pace/pace_time.rb', line 105 def ==(other) if other.is_a?(PaceTime) total_seconds == other.total_seconds elsif other.is_a?(String) @time_s == other end end |
#>(other) ⇒ Object
121 122 123 124 125 |
# File 'lib/runby_pace/pace_time.rb', line 121 def >(other) if other.is_a?(PaceTime) total_seconds > other.total_seconds end end |
#>=(other) ⇒ Object
127 128 129 130 131 |
# File 'lib/runby_pace/pace_time.rb', line 127 def >=(other) if other.is_a?(PaceTime) total_seconds >= other.total_seconds end end |
#almost_equals?(other_time, tolerance_time = '00:01') ⇒ Boolean
113 114 115 116 117 118 119 |
# File 'lib/runby_pace/pace_time.rb', line 113 def almost_equals?(other_time, tolerance_time = '00:01') if other_time.is_a?(String) other_time = PaceTime.new(other_time) end tolerance = PaceTime.new(tolerance_time) self >= (other_time - tolerance) && self <= (other_time + tolerance) end |
#to_s ⇒ Object
79 80 81 |
# File 'lib/runby_pace/pace_time.rb', line 79 def to_s @time_s end |
#total_minutes ⇒ Object
87 88 89 |
# File 'lib/runby_pace/pace_time.rb', line 87 def total_minutes @minutes_part + (@seconds_part / 60.0) end |
#total_seconds ⇒ Object
83 84 85 |
# File 'lib/runby_pace/pace_time.rb', line 83 def total_seconds @minutes_part * 60 + @seconds_part end |