overrider 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -0
- data/lib/overrider.rb +57 -16
- data/lib/overrider/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c417392909ef2a0f683cc5e2b13f217383eb6219d82b98f2e4dcbb02960b40ac
|
4
|
+
data.tar.gz: 855689641cc3992f9da0e65abd3efa8c4353ce1fb9f7d9d39a41484f85704339
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1b7030c31d7c823f2bfcbe02781271bb882f2cf9b631e3e9ae50560c00e981f155a003e9188bb09142f4f3d87ec29b2c61a53c352768cffb6ce09742e22b911
|
7
|
+
data.tar.gz: 95e5d9d96127c4160cec6c8d55aa3968da975a6051ebdae93a12a36ec08c4078abd99cfaca324721fe77943ac0999d2b869499764ac1129015ce5f1c48d59b12
|
data/README.md
CHANGED
@@ -52,6 +52,35 @@ class B2 < B1
|
|
52
52
|
end # => raise
|
53
53
|
```
|
54
54
|
|
55
|
+
### for Production
|
56
|
+
If you want to disable Abstriker, write `Abstriker.disable = true` at first line.
|
57
|
+
If Abstriker is disabled, TracePoint never runs, and so there is no overhead of VM instruction.
|
58
|
+
|
59
|
+
### Caution
|
60
|
+
|
61
|
+
Must not call `override` outer class definition.
|
62
|
+
|
63
|
+
ex.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class A1
|
67
|
+
def foo
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class A2 < A1
|
72
|
+
extend Overrider
|
73
|
+
|
74
|
+
def foo
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
A2.send(:override, :foo)
|
79
|
+
```
|
80
|
+
|
81
|
+
This case leaves enabled TracePoint.
|
82
|
+
It is very high overhead.
|
83
|
+
|
55
84
|
### Examples
|
56
85
|
|
57
86
|
#### include module method after override method
|
data/lib/overrider.rb
CHANGED
@@ -12,19 +12,53 @@ module Overrider
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
+
@disable = false
|
16
|
+
|
17
|
+
def self.disable=(v)
|
18
|
+
@disable = v
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.disabled?
|
22
|
+
@disable
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.enabled?
|
26
|
+
!disabled?
|
27
|
+
end
|
28
|
+
|
15
29
|
private
|
16
30
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
31
|
+
using Module.new {
|
32
|
+
refine Module do
|
33
|
+
def detect_event_type
|
34
|
+
caller_info = caller_locations(2, 1)[0]
|
35
|
+
if caller_info.label.match?(/block/)
|
36
|
+
[:b_call, :b_return, :raise]
|
37
|
+
else
|
38
|
+
[:end, :raise]
|
39
|
+
end
|
40
|
+
end
|
23
41
|
end
|
42
|
+
}
|
43
|
+
|
44
|
+
def override(symbol)
|
45
|
+
return if Overrider.disabled?
|
46
|
+
|
47
|
+
event_type = detect_event_type
|
48
|
+
|
49
|
+
block_count = 1
|
50
|
+
@__overrider_trace_point ||= TracePoint.trace(*event_type) do |t|
|
51
|
+
if t.event == :raise
|
52
|
+
@__overrider_trace_point.disable
|
53
|
+
@__overrider_trace_point = nil
|
54
|
+
next
|
55
|
+
end
|
56
|
+
|
57
|
+
block_count += 1 if t.event == :b_call
|
58
|
+
block_count -= 1 if t.event == :b_return
|
24
59
|
|
25
|
-
@__overrider_trace_point ||= TracePoint.trace(event_type) do |t|
|
26
60
|
klass = t.self
|
27
|
-
if klass == self
|
61
|
+
if klass == self && (t.event == :end || t.event == :b_return && block_count.zero?)
|
28
62
|
@ensure_overrides.each do |n|
|
29
63
|
meth = klass.instance_method(n)
|
30
64
|
unless meth.super_method
|
@@ -43,16 +77,23 @@ module Overrider
|
|
43
77
|
end
|
44
78
|
|
45
79
|
def override_singleton_method(symbol)
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
80
|
+
return if Overrider.disabled?
|
81
|
+
|
82
|
+
event_type = detect_event_type
|
83
|
+
|
84
|
+
block_count = 1
|
85
|
+
@__overrider_singleton_trace_point ||= TracePoint.trace(*event_type) do |t|
|
86
|
+
if t.event == :raise
|
87
|
+
@__overrider_singleton_trace_point.disable
|
88
|
+
@__overrider_singleton_trace_point = nil
|
89
|
+
next
|
90
|
+
end
|
91
|
+
|
92
|
+
block_count += 1 if t.event == :b_call
|
93
|
+
block_count -= 1 if t.event == :b_return
|
52
94
|
|
53
|
-
@__overrider_singleton_trace_point ||= TracePoint.trace(event_type) do |t|
|
54
95
|
klass = t.self
|
55
|
-
if klass == self
|
96
|
+
if klass == self && (t.event == :end || t.event == :b_return && block_count.zero?)
|
56
97
|
@ensure_overrides.each do |n|
|
57
98
|
meth = klass.singleton_class.instance_method(n)
|
58
99
|
unless meth.super_method
|
data/lib/overrider/version.rb
CHANGED