Class: BBLib::TaskTimer
Instance Method Summary
collapse
Methods inherited from LazyClass
#initialize, #serialize
Methods included from Hooks
#add_after_hook, #add_before_hook, #after, #after_hooked_methods, #after_hooks, #after_hooks_for, #before, #before_hooked_methods, #before_hooks, #before_hooks_for, #method_added
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, **named) ⇒ Object
74
75
76
77
78
79
80
81
|
# File 'lib/time/task_timer.rb', line 74
def method_missing *args, **named
temp = args.first.to_sym
pretty = named.delete :pretty
type, task = TIMER_TYPES.keys.find{ |k| k == temp || TIMER_TYPES[k].include?(temp) }, args[1] ||= :default
return super unless type
t = time task, type
pretty && type != :count && t ? (t.is_a?(Array) ? t.map{|m| m.to_duration} : t.to_duration) : t
end
|
Instance Method Details
#active?(task) ⇒ Boolean
59
60
61
62
|
# File 'lib/time/task_timer.rb', line 59
def active? task
return false unless @tasks.keys.include? task
!@tasks[task][:current].nil?
end
|
#clear(task) ⇒ Object
33
34
35
36
37
|
# File 'lib/time/task_timer.rb', line 33
def clear task
return nil unless @tasks.keys.include?(task)
stop task
@tasks[task][:history].clear
end
|
#restart(task = :default) ⇒ Object
55
56
57
|
# File 'lib/time/task_timer.rb', line 55
def restart task = :default
start(task) unless stop(task).nil?
end
|
#start(task = :default) ⇒ Object
39
40
41
42
43
44
|
# File 'lib/time/task_timer.rb', line 39
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
|
#stats(task, pretty: false) ⇒ Object
64
65
66
67
68
69
70
71
72
|
# File 'lib/time/task_timer.rb', line 64
def stats task, pretty: false
return nil unless @tasks.include?(task)
stats = "#{task}" + "\n" + '-'*30 + "\n"
TIMER_TYPES.each do |k,v|
next if STATS_IGNORE.include?(k)
stats+= k.to_s.capitalize.ljust(10) + "#{self.send(k, task, pretty:pretty)}\n"
end
stats
end
|
#stop(task = :default) ⇒ Object
46
47
48
49
50
51
52
53
|
# File 'lib/time/task_timer.rb', line 46
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
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
|
# File 'lib/time/task_timer.rb', line 7
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
|