rhook 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/rhook.rb +20 -10
- data/rhook.gemspec +5 -3
- data/spec/rhook_minor_spec.rb +91 -0
- data/spec/rhook_spec.rb +1 -0
- metadata +5 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.4
|
data/lib/rhook.rb
CHANGED
@@ -30,13 +30,16 @@ module RHook
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def hack(name, opt = {}, &block)
|
33
|
+
success = false
|
33
34
|
if Class === @obj
|
34
35
|
klass = @obj
|
35
|
-
klass._rhook.on_method(name)
|
36
|
+
success |= klass._rhook.on_method(name, :ifdef => true)
|
36
37
|
end
|
37
38
|
|
38
39
|
klass = @obj.instance_eval("class << self; self; end")
|
39
|
-
klass._rhook.on_method(name)
|
40
|
+
success |= klass._rhook.on_method(name, :ifdef => true)
|
41
|
+
|
42
|
+
success or raise(NameError, "Method #{name} is defined in neither class nor object-specific class.")
|
40
43
|
|
41
44
|
bind(name, opt, &block)
|
42
45
|
end
|
@@ -96,20 +99,27 @@ module RHook
|
|
96
99
|
inv.proceed()
|
97
100
|
end
|
98
101
|
|
99
|
-
def on_method(*
|
102
|
+
def on_method(*names_and_opt)
|
103
|
+
success = true
|
104
|
+
|
105
|
+
names = names_and_opt
|
100
106
|
Class === @obj or raise("Cannot use on_method on non-Class.")
|
101
|
-
|
107
|
+
opt = (Hash === names[-1]) ? names.pop : {}
|
102
108
|
for method_name in names
|
103
|
-
# Skip if method is not defined.
|
104
|
-
@obj.method_defined?(method_name) or next
|
105
|
-
|
106
109
|
real_method_name = "#{method_name}__rhook_real".to_sym
|
107
110
|
@obj.method_defined?(real_method_name) and next
|
108
111
|
|
109
|
-
|
110
|
-
|
112
|
+
begin
|
113
|
+
@obj.module_eval "alias #{real_method_name} #{method_name}", __FILE__, __LINE__
|
114
|
+
rescue NameError
|
115
|
+
# When method_is not defined:
|
116
|
+
opt[:ifdef] and (success = false; next)
|
117
|
+
raise NameError, "[Tried to on_method for undefined method] #{$!}"
|
118
|
+
end
|
119
|
+
@obj.module_eval "def #{method_name}(*args, &block); _rhook.call_method(:#{method_name}, :#{real_method_name}, args, block); end", __FILE__, __LINE__
|
111
120
|
end
|
112
|
-
|
121
|
+
|
122
|
+
success
|
113
123
|
end
|
114
124
|
|
115
125
|
# ================================================================
|
data/rhook.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rhook}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kaoru Kobo"]
|
12
|
-
s.date = %q{2010-11-
|
12
|
+
s.date = %q{2010-11-12}
|
13
13
|
s.description = %q{You can provide hook point in your code, and can customize its behavior from outside. Also you can 'hack' (== injecting hook point from outside) any methods in existing code.}
|
14
14
|
s.email = %q{}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
"VERSION",
|
26
26
|
"lib/rhook.rb",
|
27
27
|
"rhook.gemspec",
|
28
|
+
"spec/rhook_minor_spec.rb",
|
28
29
|
"spec/rhook_spec.rb",
|
29
30
|
"spec/spec.opts",
|
30
31
|
"spec/spec_helper.rb"
|
@@ -35,7 +36,8 @@ Gem::Specification.new do |s|
|
|
35
36
|
s.rubygems_version = %q{1.3.7}
|
36
37
|
s.summary = %q{Easily drive AOP & hacking existing library with Ruby}
|
37
38
|
s.test_files = [
|
38
|
-
"spec/
|
39
|
+
"spec/rhook_minor_spec.rb",
|
40
|
+
"spec/rhook_spec.rb",
|
39
41
|
"spec/spec_helper.rb"
|
40
42
|
]
|
41
43
|
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
# ================================================================
|
4
|
+
describe "rhook (minor specifications / behavior, and bugs)" do
|
5
|
+
class Target
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
before :each do
|
10
|
+
Target._rhook.unbind_all()
|
11
|
+
end
|
12
|
+
|
13
|
+
# ================================================================
|
14
|
+
describe "hack" do
|
15
|
+
# ================================================================
|
16
|
+
describe "Can hack on private method" do
|
17
|
+
class Target
|
18
|
+
def call_private
|
19
|
+
test_private
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def test_private
|
24
|
+
"foo"
|
25
|
+
end
|
26
|
+
end #/Target
|
27
|
+
|
28
|
+
example do
|
29
|
+
Target._rhook.hack(:test_private) do |inv|
|
30
|
+
"bar"
|
31
|
+
end
|
32
|
+
t = Target.new
|
33
|
+
t.call_private.should == "bar"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
# ================================================================
|
37
|
+
|
38
|
+
# ================================================================
|
39
|
+
describe "Raises error on hack to non-existent method" do
|
40
|
+
class Target
|
41
|
+
def hack_err1
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.hack_err2
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
example do
|
49
|
+
Target._rhook.hack(:hack_err1) do |inv|
|
50
|
+
|
51
|
+
end
|
52
|
+
Target._rhook.hack(:hack_err2) do |inv|
|
53
|
+
|
54
|
+
end
|
55
|
+
proc {
|
56
|
+
Target._rhook.hack(:hack_err_nonexistent_method) do |inv|
|
57
|
+
|
58
|
+
end
|
59
|
+
}.should raise_error(/defined in neither/)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# ================================================================
|
65
|
+
describe "on_method" do
|
66
|
+
# ================================================================
|
67
|
+
describe "to non-existent method" do
|
68
|
+
|
69
|
+
class Target
|
70
|
+
def on_method_test_non_existent
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
example "raises error" do
|
76
|
+
proc {
|
77
|
+
Target._rhook.on_method(:nonexistent_method)
|
78
|
+
}.should raise_error(/Tried to on_method/)
|
79
|
+
end
|
80
|
+
|
81
|
+
example "to ignore error, set :ifdef => true" do
|
82
|
+
result = Target._rhook.on_method(:nonexistent_method, :ifdef => true)
|
83
|
+
result.should == false
|
84
|
+
|
85
|
+
# if success, it returns true
|
86
|
+
result = Target._rhook.on_method(:on_method_test_non_existent, :ifdef => true)
|
87
|
+
result.should == true
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/spec/rhook_spec.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 4
|
9
|
+
version: 0.1.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Kaoru Kobo
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-11-
|
17
|
+
date: 2010-11-12 00:00:00 +09:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -50,6 +50,7 @@ files:
|
|
50
50
|
- VERSION
|
51
51
|
- lib/rhook.rb
|
52
52
|
- rhook.gemspec
|
53
|
+
- spec/rhook_minor_spec.rb
|
53
54
|
- spec/rhook_spec.rb
|
54
55
|
- spec/spec.opts
|
55
56
|
- spec/spec_helper.rb
|
@@ -86,5 +87,6 @@ signing_key:
|
|
86
87
|
specification_version: 3
|
87
88
|
summary: Easily drive AOP & hacking existing library with Ruby
|
88
89
|
test_files:
|
90
|
+
- spec/rhook_minor_spec.rb
|
89
91
|
- spec/rhook_spec.rb
|
90
92
|
- spec/spec_helper.rb
|