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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cfc96db5c2b0821331d448087dbb5737a480a94dfec4512b4713a3ab44375deb
4
- data.tar.gz: 190c64c0e997937c2442864d134ab8ceaf6558e1b41acb8429aca47719cbde83
3
+ metadata.gz: c417392909ef2a0f683cc5e2b13f217383eb6219d82b98f2e4dcbb02960b40ac
4
+ data.tar.gz: 855689641cc3992f9da0e65abd3efa8c4353ce1fb9f7d9d39a41484f85704339
5
5
  SHA512:
6
- metadata.gz: 00fcfb5131bcc6f7160f15a447ccfd98fb988b9aa91d1238550bbe0dae580ad53defb338bddfa095a93f3ad388ab0116311d68549fb279b478f9e78e92db8ff4
7
- data.tar.gz: 4d643fd6a432df8006670dd588205df282867501b715f9707f1835e0522c2f175e26e6e16f9500b7a9074de65aa73e31adc69dc63eea638c0ee5ff14813fedeb
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
@@ -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
- def override(symbol)
18
- caller_info = caller_locations(1, 1)[0]
19
- event_type = if caller_info.label.match?(/block/)
20
- :b_return
21
- else
22
- :end
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
- caller_info = caller_locations(1, 1)[0]
47
- event_type = if caller_info.label.match?(/block/)
48
- :b_return
49
- else
50
- :end
51
- end
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
@@ -1,3 +1,3 @@
1
1
  module Overrider
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: overrider
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - joker1007