Class: Timesplit::Split

Inherits:
Object
  • Object
show all
Defined in:
lib/timesplit/split.rb

Constant Summary collapse

SECS =
60.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*times) ⇒ Split

Returns a new instance of Split.



10
11
12
13
14
15
16
# File 'lib/timesplit/split.rb', line 10

def initialize(*times)
  times = times.first.split(/[^\d]+/).map(&:to_f) if times.first.is_a? String
  multiplier = 1
  @seconds = times.reverse.reduce do |t, n|
    t += n * (multiplier *= SECS)
  end
end

Class Method Details

.[](*values) ⇒ Object



6
7
8
# File 'lib/timesplit/split.rb', line 6

def self.[](*values)
  new(*values)
end

Instance Method Details

#*(other) ⇒ Object



66
67
68
# File 'lib/timesplit/split.rb', line 66

def *(other)
  Split.new(@seconds * other.to_f)
end

#+(other) ⇒ Object



54
55
56
# File 'lib/timesplit/split.rb', line 54

def +(other)
  Split.new(@seconds + other.to_f)
end

#-(other) ⇒ Object



58
59
60
# File 'lib/timesplit/split.rb', line 58

def -(other)
  Split.new(@seconds - other.to_f)
end

#/(other) ⇒ Object



62
63
64
# File 'lib/timesplit/split.rb', line 62

def /(other)
  Split.new(@seconds / other.to_f)
end

#==(other) ⇒ Object



46
47
48
# File 'lib/timesplit/split.rb', line 46

def ==(other)
  self.to_f == other.to_f
end

#coerce(other) ⇒ Object



50
51
52
# File 'lib/timesplit/split.rb', line 50

def coerce(other)
  [Split.new(other.to_f), self]
end

#hoursObject



18
19
20
# File 'lib/timesplit/split.rb', line 18

def hours
  calculate_parts[0]
end

#minutesObject



22
23
24
# File 'lib/timesplit/split.rb', line 22

def minutes
  calculate_parts[1]
end

#secondsObject



26
27
28
# File 'lib/timesplit/split.rb', line 26

def seconds
  calculate_parts[2]
end

#to_fObject



42
43
44
# File 'lib/timesplit/split.rb', line 42

def to_f
  @seconds.to_f
end

#to_iObject



38
39
40
# File 'lib/timesplit/split.rb', line 38

def to_i
  @seconds.to_i
end

#to_sObject



30
31
32
33
34
35
36
# File 'lib/timesplit/split.rb', line 30

def to_s
  [
    hours.to_i.to_s.rjust(2, '0'),
    minutes.to_i.to_s.rjust(2, '0'),
    seconds.to_i.to_s.rjust(2, '0'),
  ].join(':')
end