Class: RunbyPace::PaceTime

Inherits:
Object
  • Object
show all
Defined in:
lib/runby_pace/pace_time.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time) ⇒ PaceTime

Returns a new instance of PaceTime.



6
7
8
9
10
11
12
# File 'lib/runby_pace/pace_time.rb', line 6

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)
  end
end

Instance Attribute Details

#minutes_partObject (readonly)

Returns the value of attribute minutes_part.



4
5
6
# File 'lib/runby_pace/pace_time.rb', line 4

def minutes_part
  @minutes_part
end

#seconds_partObject (readonly)

Returns the value of attribute seconds_part.



4
5
6
# File 'lib/runby_pace/pace_time.rb', line 4

def seconds_part
  @seconds_part
end

#time_sObject (readonly)

Returns the value of attribute time_s.



4
5
6
# File 'lib/runby_pace/pace_time.rb', line 4

def time_s
  @time_s
end

Class Method Details

.from_minutes(total_minutes) ⇒ Object

Parameters:

  • total_minutes (numeric)


22
23
24
# File 'lib/runby_pace/pace_time.rb', line 22

def self.from_minutes(total_minutes)
  self.from_seconds(total_minutes * 60.0)
end

.from_seconds(total_seconds) ⇒ Object

Parameters:

  • total_seconds (numeric)


15
16
17
18
19
# File 'lib/runby_pace/pace_time.rb', line 15

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

Instance Method Details

#+(value) ⇒ Object

Parameters:



46
47
48
49
50
# File 'lib/runby_pace/pace_time.rb', line 46

def +(value)
  if value.is_a?(PaceTime)
    PaceTime.from_seconds(total_seconds + value.total_seconds)
  end
end

#-(value) ⇒ Object

Parameters:



39
40
41
42
43
# File 'lib/runby_pace/pace_time.rb', line 39

def -(value)
  if value.is_a?(PaceTime)
    PaceTime.from_seconds(total_seconds - value.total_seconds)
  end
end

#<(value) ⇒ Object



80
81
82
83
84
# File 'lib/runby_pace/pace_time.rb', line 80

def <(value)
  if value.is_a?(PaceTime)
    total_seconds < value.total_seconds
  end
end

#<=(value) ⇒ Object



86
87
88
89
90
# File 'lib/runby_pace/pace_time.rb', line 86

def <=(value)
  if value.is_a?(PaceTime)
    total_seconds <= value.total_seconds
  end
end

#==(value) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/runby_pace/pace_time.rb', line 52

def ==(value)
  if value.is_a?(PaceTime)
    total_seconds == value.total_seconds
  elsif value.is_a?(String)
    @time_s == value
  end
end

#>(value) ⇒ Object



68
69
70
71
72
# File 'lib/runby_pace/pace_time.rb', line 68

def >(value)
  if value.is_a?(PaceTime)
    total_seconds > value.total_seconds
  end
end

#>=(value) ⇒ Object



74
75
76
77
78
# File 'lib/runby_pace/pace_time.rb', line 74

def >=(value)
  if value.is_a?(PaceTime)
    total_seconds >= value.total_seconds
  end
end

#almost_equals?(other_time, tolerance_time = '00:01') ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/runby_pace/pace_time.rb', line 60

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_sObject



26
27
28
# File 'lib/runby_pace/pace_time.rb', line 26

def to_s
  @time_s
end

#total_minutesObject



34
35
36
# File 'lib/runby_pace/pace_time.rb', line 34

def total_minutes
  @minutes_part + (@seconds_part / 60.0)
end

#total_secondsObject



30
31
32
# File 'lib/runby_pace/pace_time.rb', line 30

def total_seconds
  @minutes_part * 60 + @seconds_part
end