class Runby::RunbyTime
Represents a human-readable time in the format MM:ss
Attributes
hours_part[R]
minutes_part[R]
seconds_part[R]
time_s[R]
Public Class Methods
check_5k_sanity(time)
click to toggle source
# File lib/runby_pace/runby_time.rb, line 59 def self.check_5k_sanity(time) return unless time.is_a? RunbyTime return '5K times of less than 14:00 are unlikely' if time.minutes_part < 14 return '5K times of greater than 42:00 are not fully supported' if time.total_seconds > (42 * 60) end
from_hours(total_hours)
click to toggle source
@param [numeric] total_hours
# File lib/runby_pace/runby_time.rb, line 40 def self.from_hours(total_hours) from_seconds(total_hours * 60.0 * 60.0) end
from_minutes(total_minutes)
click to toggle source
@param [numeric] total_minutes
# File lib/runby_pace/runby_time.rb, line 35 def self.from_minutes(total_minutes) from_seconds(total_minutes * 60.0) end
from_seconds(total_seconds)
click to toggle source
@param [numeric] total_seconds
# File lib/runby_pace/runby_time.rb, line 23 def self.from_seconds(total_seconds) hours = total_seconds.abs.to_i / 60 / 60 minutes = (total_seconds.abs.to_i / 60) % 60 seconds = total_seconds.abs.to_i % 60 if hours.positive? RunbyTime.new format('%d:%02d:%02d', hours, minutes, seconds) else RunbyTime.new format('%02d:%02d', minutes, seconds) end end
new(time)
click to toggle source
Calls superclass method
# File lib/runby_pace/runby_time.rb, line 10 def self.new(time) return time if time.is_a? RunbyTime return RunbyTime.parse time if time.is_a?(String) || time.is_a?(Symbol) return from_minutes(time) if time.is_a? Numeric super end
new(time)
click to toggle source
# File lib/runby_pace/runby_time.rb, line 17 def initialize(time) init_from_parts time if time.is_a? RunbyTimeParser::TimeParts freeze end
parse(str)
click to toggle source
# File lib/runby_pace/runby_time.rb, line 44 def self.parse(str) RunbyTimeParser.parse str end
try_parse(str, is_five_k = false)
click to toggle source
# File lib/runby_pace/runby_time.rb, line 48 def self.try_parse(str, is_five_k = false) time, error_message, warning_message = nil begin time = parse str rescue StandardError => ex error_message = "#{ex.message} (#{str})" end warning_message = check_5k_sanity(time) if !time.nil? && is_five_k { time: time, error: error_message, warning: warning_message } end
Public Instance Methods
*(other)
click to toggle source
@param [Numeric] other @return [RunbyTime]
# File lib/runby_pace/runby_time.rb, line 95 def *(other) raise "Cannot multiply Runby::RunbyTime with a #{other.class}" unless other.is_a?(Numeric) RunbyTime.from_minutes(total_minutes * other) end
+(other)
click to toggle source
@param [RunbyTime] other
# File lib/runby_pace/runby_time.rb, line 88 def +(other) raise "Cannot add Runby::RunbyTime to a #{other.class}" unless other.is_a?(RunbyTime) RunbyTime.from_seconds(total_seconds + other.total_seconds) end
-(other)
click to toggle source
@param [RunbyTime] other
# File lib/runby_pace/runby_time.rb, line 82 def -(other) raise "Cannot subtract #{other.class} from a Runby::RunbyTime" unless other.is_a?(RunbyTime) RunbyTime.from_seconds(total_seconds - other.total_seconds) end
/(other)
click to toggle source
@param [RunbyTime, Numeric] other @return [Numeric, RunbyTime]
# File lib/runby_pace/runby_time.rb, line 102 def /(other) raise "Cannot divide Runby::RunbyTime by #{other.class}" unless other.is_a?(RunbyTime) || other.is_a?(Numeric) case other when RunbyTime total_seconds / other.total_seconds when Numeric RunbyTime.from_seconds(total_seconds / other) end end
<=>(other)
click to toggle source
# File lib/runby_pace/runby_time.rb, line 112 def <=>(other) raise "Unable to compare Runby::RunbyTime to #{other.class}(#{other})" unless [RunbyTime, String].include? other.class if other.is_a? RunbyTime total_seconds <=> other.total_seconds elsif other.is_a? String return 0 if @time_s == other total_seconds <=> RunbyTime.parse(other).total_seconds end end
almost_equals?(other_time, tolerance_time = '00:01')
click to toggle source
# File lib/runby_pace/runby_time.rb, line 122 def almost_equals?(other_time, tolerance_time = '00:01') other_time = RunbyTime.new(other_time) if other_time.is_a?(String) tolerance = RunbyTime.new(tolerance_time) self >= (other_time - tolerance) && self <= (other_time + tolerance) end
to_s()
click to toggle source
# File lib/runby_pace/runby_time.rb, line 65 def to_s @time_s end
total_hours()
click to toggle source
# File lib/runby_pace/runby_time.rb, line 69 def total_hours @hours_part + (@minutes_part / 60.0) + (@seconds_part / 60.0 / 60.0) end
total_minutes()
click to toggle source
# File lib/runby_pace/runby_time.rb, line 77 def total_minutes @hours_part * 60 + @minutes_part + (@seconds_part / 60.0) end
total_seconds()
click to toggle source
# File lib/runby_pace/runby_time.rb, line 73 def total_seconds @hours_part * 60 * 60 + @minutes_part * 60 + @seconds_part end
Private Instance Methods
init_from_parts(parts)
click to toggle source
@param [RunbyTimeParser::TimeParts] parts
# File lib/runby_pace/runby_time.rb, line 131 def init_from_parts(parts) @time_s = parts.format @hours_part = parts[:hours] @minutes_part = parts[:minutes] @seconds_part = parts[:seconds] end