-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.rb
More file actions
45 lines (37 loc) · 814 Bytes
/
timer.rb
File metadata and controls
45 lines (37 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# attributes: { id: int, latest_start: datetime,
# time: int, status: # string, enum, int your choice }
class Timer
attr_reader :status, :time, :latest_start
PAUSED = 'paused'
ACTIVE = 'active'
STOPPED = 'stopped'
def initialize
@status = STOPPED
@time = 0
@latest_start = nil
end
def start
return if status == ACTIVE
@latest_start = Time.now
@status = ACTIVE
end
def pause
return if status == PAUSED
@time = current_time
@status = PAUSED
end
def reset
return if status == STOPPED
@time = 0
@latest_start = nil
@status = STOPPED
end
def current_time
@time + time_since_latest_start
end
private
def time_since_latest_start
return 0 if @status == PAUSED || @latest_start.nil?
Time.now - @latest_start
end
end