-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity.rb
52 lines (42 loc) · 1.04 KB
/
activity.rb
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
46
47
48
49
50
51
52
class Activity
def self.format_seconds(seconds)
hours, mins = seconds.divmod(3600)
mins = mins / 60
"#{hours.to_i} hr. #{mins.round} min."
end
def self.new_time(time)
year, month, day = Time.now.year, Time.now.month, Time.now.day
Time.new(year, month, day, *time.split(':'))
end
attr_accessor :name, :id, :start_time, :history
def initialize(name, id)
@name = name
@id = id
@history = []
end
def start(time)
@start_time = Activity.new_time(time)
end
def running?
@start_time
end
def stop(time)
stop_time = Activity.new_time(time)
seconds = stop_time - @start_time
@history << { start_time: @start_time, stop_time: stop_time, seconds: seconds }
@history.sort_by! { |item| item[:start_time] }
@start_time = nil
end
def status
running? ? 'running' : 'stopped'
end
def current_total
Activity.format_seconds(Time.now - @start_time)
end
def history_total_seconds
@history.map { |item| item[:seconds] }.sum
end
def history?
!@history.empty?
end
end