easy-hook 0.1.4 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/easy-hook.rb +57 -1
- metadata +1 -1
data/lib/easy-hook.rb
CHANGED
@@ -1,8 +1,64 @@
|
|
1
1
|
# Contains methods to hook method calls
|
2
2
|
module EasyHook
|
3
|
+
|
4
|
+
module HookTimer
|
5
|
+
require 'set'
|
6
|
+
class Timer
|
7
|
+
class << self
|
8
|
+
attr_accessor :ids
|
9
|
+
|
10
|
+
def finalize(id)
|
11
|
+
@ids.delete(id)
|
12
|
+
end
|
13
|
+
|
14
|
+
def register(obj)
|
15
|
+
@ids ||= Set.new
|
16
|
+
@ids << obj.object_id
|
17
|
+
ObjectSpace.define_finalizer(obj, method(:finalize))
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_accessor :time, :start
|
24
|
+
|
25
|
+
def initialize
|
26
|
+
@start = Time.now
|
27
|
+
@time = nil
|
28
|
+
Timer.register(self)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.start
|
33
|
+
Timer.new.object_id
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.restart(id)
|
37
|
+
timer = ObjectSpace._id2ref(id)
|
38
|
+
return false unless timer
|
39
|
+
timer.start = Time.now
|
40
|
+
timer.time = nil
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.stop(id)
|
44
|
+
stop = Time.now
|
45
|
+
timer = ObjectSpace._id2ref(id)
|
46
|
+
return false unless timer
|
47
|
+
timer.time ||= stop - timer.start
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.time(id)
|
51
|
+
stop = Time.now
|
52
|
+
timer = ObjectSpace._id2ref(id)
|
53
|
+
return false unless timer
|
54
|
+
stop - timer.start
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
end
|
3
59
|
|
4
60
|
module ClassMethods
|
5
|
-
|
61
|
+
include HookTimer
|
6
62
|
private
|
7
63
|
|
8
64
|
# Hook the provided instance methods so that the block
|