Class: BBLib::TaskTimer
- Inherits:
-
Object
show all
- Defined in:
- lib/time/task_timer.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(task: nil, retention: 100) ⇒ TaskTimer
Returns a new instance of TaskTimer.
6
7
8
9
10
|
# File 'lib/time/task_timer.rb', line 6
def initialize task:nil, retention:100
@tasks = {}
self.retention = retention
if task then start task end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
77
78
79
80
81
82
83
|
# File 'lib/time/task_timer.rb', line 77
def method_missing *args
temp = args.first.to_s.sub('p_','').to_sym
type, task = TIMER_TYPES.keys.find{ |k| k == temp || TIMER_TYPES[k].include?(temp) }, args[1] ||= :default
raise NoMethodError unless type
t = time task, type
args.first.to_s.start_with?('p_') && type != :count ? t.to_duration : t
end
|
Instance Attribute Details
#retention ⇒ Object
Returns the value of attribute retention.
4
5
6
|
# File 'lib/time/task_timer.rb', line 4
def retention
@retention
end
|
#save ⇒ Object
Returns the value of attribute save.
4
5
6
|
# File 'lib/time/task_timer.rb', line 4
def save
@save
end
|
#tasks ⇒ Object
Returns the value of attribute tasks.
4
5
6
|
# File 'lib/time/task_timer.rb', line 4
def tasks
@tasks
end
|
Instance Method Details
#active?(task) ⇒ Boolean
72
73
74
75
|
# File 'lib/time/task_timer.rb', line 72
def active? task
return false unless @tasks.keys.include? task
!@tasks[task][:current].nil?
end
|
#clear(task) ⇒ Object
42
43
44
45
46
|
# File 'lib/time/task_timer.rb', line 42
def clear task
return nil unless @tasks.keys.include?(task)
stop task
@tasks[task][:history].clear
end
|
#restart(task = :default) ⇒ Object
64
65
66
|
# File 'lib/time/task_timer.rb', line 64
def restart task = :default
start(task) unless stop(task).nil?
end
|
#start(task = :default) ⇒ Object
48
49
50
51
52
53
|
# File 'lib/time/task_timer.rb', line 48
def start task = :default
if !@tasks.keys.include?(task) then @tasks[task] = {history: [], current: nil} end
if @tasks[task][:current] then stop task end
@tasks[task][:current] = Time.now.to_f
return 0
end
|
#stop(task = :default) ⇒ Object
55
56
57
58
59
60
61
62
|
# File 'lib/time/task_timer.rb', line 55
def stop task = :default
return nil unless @tasks.keys.include?(task) && active?(task)
time_taken = Time.now.to_f - @tasks[task][:current].to_f
@tasks[task][:history] << {start: @tasks[task][:current], stop: Time.now.to_f, time: time_taken}
@tasks[task][:current] = nil
if @retention && @tasks[task][:history].size > @retention then @tasks[task][:history].shift end
time_taken
end
|
#time(task = :default, type = :current) ⇒ Object
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
|
# File 'lib/time/task_timer.rb', line 16
def time task = :default, type = :current
return nil unless @tasks.keys.include? task
numbers = @tasks[task][:history].map{ |v| v[:time] }
case type
when :current
return nil unless @tasks[task][:current]
return Time.now.to_f - @tasks[task][:current]
when :min
return numbers.min
when :max
return numbers.max
when :avg
return numbers.inject{ |sum, n| sum + n }.to_f / numbers.size
when :sum
return numbers.inject{ |sum, n| sum + n }
when :all
return numbers
when :first
return numbers.first
when :last
return numbers.last
when :count
return numbers.size
end
end
|