hooked 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +5 -0
- data/README.md +3 -1
- data/lib/hooked.rb +6 -1
- data/lib/hooked/aspect.rb +1 -1
- data/lib/hooked/hook.rb +8 -2
- data/lib/hooked/version.rb +1 -1
- data/spec/aspect_spec.rb +9 -19
- data/spec/hook_spec.rb +28 -0
- data/spec/hooked_spec.rb +20 -0
- metadata +28 -41
data/README.md
CHANGED
@@ -3,6 +3,8 @@ Aspect Orientation Made Simple
|
|
3
3
|
|
4
4
|
Hooked lets you transparently aspectify your methods and blocks.
|
5
5
|
|
6
|
+
[![Build status](https://secure.travis-ci.org/lgierth/hooked.png)](http://travis-ci.org/lgierth/hooked)
|
7
|
+
|
6
8
|
Getting Started
|
7
9
|
---------------
|
8
10
|
|
@@ -42,7 +44,7 @@ to methods of this class' objects".
|
|
42
44
|
foo.after :breakfast do
|
43
45
|
puts "Mmmh..."
|
44
46
|
end
|
45
|
-
foo.around :breakfast, better_foo.method(:fuuu), :before => better_foo.method(:shower)
|
47
|
+
foo.around :breakfast, better_foo.method(:fuuu), :before => [better_foo.method(:shower)]
|
46
48
|
|
47
49
|
foo.breakfast
|
48
50
|
|
data/lib/hooked.rb
CHANGED
@@ -13,6 +13,11 @@ module Hooked
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
def instead_of(pointcut, advice, &block)
|
17
|
+
hook! pointcut
|
18
|
+
hooked[pointcut].instead_pointcut = (advice || block)
|
19
|
+
end
|
20
|
+
|
16
21
|
def hook!(pointcut)
|
17
22
|
@hooked ||= {}
|
18
23
|
return if hooked[pointcut]
|
@@ -50,7 +55,7 @@ module Hooked
|
|
50
55
|
module ClassMethods
|
51
56
|
attr_reader :instance_hooked
|
52
57
|
|
53
|
-
[:before, :after, :around].each do |type|
|
58
|
+
[:before, :after, :around, :instead_of].each do |type|
|
54
59
|
define_method :"instance_#{type}" do |pointcut, *args, &block|
|
55
60
|
instance_hooked[pointcut] ||= []
|
56
61
|
instance_hooked[pointcut] << {
|
data/lib/hooked/aspect.rb
CHANGED
data/lib/hooked/hook.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Hooked
|
2
2
|
class Hook
|
3
|
-
attr_reader :pointcut, :graph
|
3
|
+
attr_reader :pointcut, :instead_pointcut, :graph
|
4
4
|
|
5
5
|
def initialize(pointcut = nil, &block)
|
6
6
|
if (pointcut && block) || (!pointcut && !block)
|
@@ -14,14 +14,20 @@ module Hooked
|
|
14
14
|
graph << Aspect.new(*args, &block)
|
15
15
|
end
|
16
16
|
|
17
|
+
def instead_pointcut=(instead)
|
18
|
+
@instead_pointcut = instead
|
19
|
+
@chain = nil
|
20
|
+
end
|
21
|
+
|
17
22
|
def call(*args, &block)
|
18
23
|
@chain = build_chain if graph.changed? || !@chain
|
19
24
|
@chain.call *args, &block
|
20
25
|
end
|
21
26
|
|
22
27
|
def build_chain
|
28
|
+
pc = instead_pointcut || pointcut
|
23
29
|
graph.sort
|
24
|
-
graph.output.reverse.inject(
|
30
|
+
graph.output.reverse.inject(pc) do |pointcut, aspect|
|
25
31
|
aspect.pointcut = pointcut; aspect
|
26
32
|
end
|
27
33
|
end
|
data/lib/hooked/version.rb
CHANGED
data/spec/aspect_spec.rb
CHANGED
@@ -61,15 +61,16 @@ describe Hooked::Aspect do
|
|
61
61
|
order.should == [:advice, :pointcut]
|
62
62
|
end
|
63
63
|
|
64
|
-
it "
|
65
|
-
|
66
|
-
obj = Hooked::Aspect.new :before,
|
67
|
-
obj.pointcut =
|
68
|
-
|
69
|
-
|
70
|
-
|
64
|
+
it "passes arguments as one array" do
|
65
|
+
arg1, arg2 = stub("arg#1"), stub("arg#2")
|
66
|
+
obj = Hooked::Aspect.new :before, advice
|
67
|
+
obj.pointcut = pointcut
|
68
|
+
advice.should_receive(:call) {|args|
|
69
|
+
args[0].should equal(arg1)
|
70
|
+
args[1].should equal(arg2)
|
71
|
+
}
|
71
72
|
|
72
|
-
obj.call
|
73
|
+
obj.call arg1, arg2
|
73
74
|
end
|
74
75
|
end
|
75
76
|
|
@@ -81,17 +82,6 @@ describe Hooked::Aspect do
|
|
81
82
|
|
82
83
|
order.should == [:pointcut, :advice]
|
83
84
|
end
|
84
|
-
|
85
|
-
it "manipulates the return value" do
|
86
|
-
return_before, return_after = :foo, :bar
|
87
|
-
obj = Hooked::Aspect.new :after, mock("advice")
|
88
|
-
obj.pointcut = mock "pointcut"
|
89
|
-
|
90
|
-
obj.pointcut.stub(:call).and_return return_before
|
91
|
-
obj.advice.should_receive(:call).with(return_before).and_return return_after
|
92
|
-
|
93
|
-
obj.call.should == return_after
|
94
|
-
end
|
95
85
|
end
|
96
86
|
|
97
87
|
describe "with type == :around" do
|
data/spec/hook_spec.rb
CHANGED
@@ -41,6 +41,20 @@ describe Hooked::Hook do
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
+
describe "#instead_pointcut=" do
|
45
|
+
it "sets a substitute pointcut and clears the chain" do
|
46
|
+
pointcut = stub("pointcut", :call => nil)
|
47
|
+
instead = stub("substitute pointcut", :call => nil)
|
48
|
+
|
49
|
+
obj = Hooked::Hook.new(pointcut)
|
50
|
+
obj.call
|
51
|
+
|
52
|
+
obj.instead_pointcut = instead
|
53
|
+
obj.should_receive(:build_chain) { stub "chain", :call => nil }
|
54
|
+
obj.call
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
44
58
|
describe "#call" do
|
45
59
|
it "rebuilds the chain if it hasn't been built yet or the graph changed" do
|
46
60
|
obj = Hooked::Hook.new double("pointcut")
|
@@ -82,5 +96,19 @@ describe Hooked::Hook do
|
|
82
96
|
chain.pointcut.pointcut.pos.should == 3
|
83
97
|
chain.pointcut.pointcut.pointcut.should == obj.pointcut
|
84
98
|
end
|
99
|
+
|
100
|
+
it "uses the subsitute pointcut if it's set" do
|
101
|
+
pointcut = stub("pointcut")
|
102
|
+
instead = stub("substitute pointcut")
|
103
|
+
|
104
|
+
obj = Hooked::Hook.new(pointcut)
|
105
|
+
obj.instead_pointcut = instead
|
106
|
+
|
107
|
+
obj.build_chain
|
108
|
+
pointcut.should_not_receive :call
|
109
|
+
instead.should_receive :call
|
110
|
+
|
111
|
+
obj.call
|
112
|
+
end
|
85
113
|
end
|
86
114
|
end
|
data/spec/hooked_spec.rb
CHANGED
@@ -30,6 +30,16 @@ describe Hooked do
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
+
describe "#instead_of" do
|
34
|
+
it "substitutes the pointcut" do
|
35
|
+
pointcut = stub("substitute pointcut")
|
36
|
+
obj = @klass.new
|
37
|
+
|
38
|
+
obj.instead_of :foo, pointcut
|
39
|
+
obj.hooked[:foo].instead_pointcut.should equal(pointcut)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
33
43
|
describe "#hook!" do
|
34
44
|
it "replaces the original method with a hook" do
|
35
45
|
obj = @klass.new
|
@@ -74,4 +84,14 @@ describe Hooked do
|
|
74
84
|
end
|
75
85
|
end
|
76
86
|
end
|
87
|
+
|
88
|
+
describe ".instance_instead_of" do
|
89
|
+
it "substitutes the pointcut on each new object of the class" do
|
90
|
+
pointcut = stub("substitute pointcut")
|
91
|
+
@klass.instance_instead_of :foo, pointcut
|
92
|
+
|
93
|
+
obj = @klass.new
|
94
|
+
obj.hooked[:foo].instead_pointcut.should equal(pointcut)
|
95
|
+
end
|
96
|
+
end
|
77
97
|
end
|
metadata
CHANGED
@@ -1,39 +1,36 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: hooked
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.4
|
4
5
|
prerelease:
|
5
|
-
version: 0.3.3
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Lars Gierth
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-07-20 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
16
15
|
name: rspec
|
17
|
-
requirement: &
|
16
|
+
requirement: &78308770 !ruby/object:Gem::Requirement
|
18
17
|
none: false
|
19
|
-
requirements:
|
20
|
-
- -
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version:
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
23
22
|
type: :development
|
24
23
|
prerelease: false
|
25
|
-
version_requirements: *
|
24
|
+
version_requirements: *78308770
|
26
25
|
description: Hooked lets you transparently aspectify your methods and blocks.
|
27
|
-
email:
|
26
|
+
email:
|
28
27
|
- lars.gierth@gmail.com
|
29
28
|
executables: []
|
30
|
-
|
31
29
|
extensions: []
|
32
|
-
|
33
30
|
extra_rdoc_files: []
|
34
|
-
|
35
|
-
files:
|
31
|
+
files:
|
36
32
|
- .rspec
|
33
|
+
- .travis.yml
|
37
34
|
- Gemfile
|
38
35
|
- LICENSE
|
39
36
|
- README.md
|
@@ -51,36 +48,26 @@ files:
|
|
51
48
|
- spec/spec_helper.rb
|
52
49
|
homepage: http://rubygems.org/gems/hooked
|
53
50
|
licenses: []
|
54
|
-
|
55
51
|
post_install_message:
|
56
52
|
rdoc_options: []
|
57
|
-
|
58
|
-
require_paths:
|
53
|
+
require_paths:
|
59
54
|
- lib
|
60
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
56
|
none: false
|
62
|
-
requirements:
|
63
|
-
- -
|
64
|
-
- !ruby/object:Gem::Version
|
65
|
-
|
66
|
-
|
67
|
-
- 0
|
68
|
-
version: "0"
|
69
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
62
|
none: false
|
71
|
-
requirements:
|
72
|
-
- -
|
73
|
-
- !ruby/object:Gem::Version
|
74
|
-
|
75
|
-
segments:
|
76
|
-
- 0
|
77
|
-
version: "0"
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
78
67
|
requirements: []
|
79
|
-
|
80
68
|
rubyforge_project:
|
81
|
-
rubygems_version: 1.8.
|
69
|
+
rubygems_version: 1.8.5
|
82
70
|
signing_key:
|
83
71
|
specification_version: 3
|
84
72
|
summary: Aspect Orientation Made Simple
|
85
73
|
test_files: []
|
86
|
-
|