hooked 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -77,7 +77,7 @@ Dependencies
77
77
  * stdlib's [TSort](http://rubydoc.info/stdlib/tsort/1.9.2/frames)
78
78
  * [RSpec](http://relishapp.com/rspec) for development
79
79
 
80
- Specs pass on MRI 1.9.2, others have not been tested yet.
80
+ Specs pass on MRI 1.9.2, Rubinius 1.2.3, JRuby 1.6.1 and MRI 1.8.7.
81
81
 
82
82
  To do & Ideas
83
83
  -------------
data/lib/hooked.rb CHANGED
@@ -18,7 +18,7 @@ module Hooked
18
18
  return if hooked[pointcut]
19
19
 
20
20
  hooked[pointcut] = Hooked::Hook.new method(pointcut)
21
- singleton_class.class_eval do
21
+ (class << self; self; end).class_eval do
22
22
  undef_method pointcut
23
23
  define_method pointcut do |*args, &block|
24
24
  hooked[pointcut].call *args, &block
@@ -30,10 +30,45 @@ module Hooked
30
30
  return unless hooked && hooked[pointcut]
31
31
 
32
32
  p = hooked[pointcut].pointcut
33
- singleton_class.class_eval do
33
+ (class << self; self; end).class_eval do
34
34
  remove_method pointcut
35
35
  define_method pointcut, &p
36
36
  end
37
37
  hooked.delete pointcut
38
38
  end
39
+
40
+ def self.included(klass)
41
+ klass.instance_variable_set :@instance_hooked, {}
42
+ klass.extend ClassMethods
43
+
44
+ class << klass
45
+ alias_method :new_without_hooked, :new
46
+ alias_method :new, :new_with_hooked
47
+ end
48
+ end
49
+
50
+ module ClassMethods
51
+ attr_reader :instance_hooked
52
+
53
+ [:before, :after, :around].each do |type|
54
+ define_method :"instance_#{type}" do |pointcut, *args, &block|
55
+ instance_hooked[pointcut] ||= []
56
+ instance_hooked[pointcut] << {
57
+ :type => type,
58
+ :args => args,
59
+ :block => block
60
+ }
61
+ end
62
+ end
63
+
64
+ def new_with_hooked(*args, &block)
65
+ obj = new_without_hooked *args, &block
66
+ instance_hooked.each do |pointcut, aspects|
67
+ aspects.each do |aspect|
68
+ obj.send aspect[:type], pointcut, *aspect[:args], &aspect[:block]
69
+ end
70
+ end
71
+ obj
72
+ end
73
+ end
39
74
  end
@@ -1,3 +1,3 @@
1
1
  module Hooked
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
data/spec/graph_spec.rb CHANGED
@@ -43,7 +43,11 @@ describe Hooked::Graph do
43
43
  aspects.each {|a| graph << a }
44
44
  graph.sort
45
45
 
46
- graph.output.should == [aspects[2], aspects[0], aspects[3], aspects[1]]
46
+ g = graph.output
47
+ g.index(aspects[0]).should > g.index(aspects[2])
48
+ g.index(aspects[1]).should > g.index(aspects[0])
49
+ g.index(aspects[2]).should < g.index(aspects[3])
50
+ g.index(aspects[3]).should < g.index(aspects[1])
47
51
  end
48
52
 
49
53
  it "detects circular dependencies" do
data/spec/hooked_spec.rb CHANGED
@@ -1,15 +1,17 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Hooked do
4
- klass = Class.new do
5
- include Hooked
6
- def foo; end
4
+ before do
5
+ @klass = Class.new do
6
+ include Hooked
7
+ def foo; end
8
+ end
7
9
  end
8
10
 
9
11
  [:before, :after, :around].each do |type|
10
12
  describe "##{type}" do
11
13
  it "hooks the method" do
12
- obj = klass.new
14
+ obj = @klass.new
13
15
  obj.stub :hooked => {:foo => double("hooked", :add_aspect => nil)}
14
16
 
15
17
  obj.should_receive(:hook!).with(:foo)
@@ -17,7 +19,7 @@ describe Hooked do
17
19
  end
18
20
 
19
21
  it "adds an aspect to the hook" do
20
- obj = klass.new
22
+ obj = @klass.new
21
23
  advice, deps = stub("advice"), stub("deps")
22
24
 
23
25
  obj.hook! :foo
@@ -30,7 +32,7 @@ describe Hooked do
30
32
 
31
33
  describe "#hook!" do
32
34
  it "replaces the original method with a hook" do
33
- obj = klass.new
35
+ obj = @klass.new
34
36
  args, result = ["bar", 123], stub("result")
35
37
 
36
38
  obj.hook! :foo
@@ -39,7 +41,7 @@ describe Hooked do
39
41
  end
40
42
 
41
43
  it "doesn't hook a method twice" do
42
- obj = klass.new
44
+ obj = @klass.new
43
45
  obj.hook! :foo
44
46
 
45
47
  Hooked::Hook.should_not_receive :new
@@ -49,7 +51,7 @@ describe Hooked do
49
51
 
50
52
  describe "#unhook!" do
51
53
  it "brings the original back in place" do
52
- obj = klass.new
54
+ obj = @klass.new
53
55
 
54
56
  obj.hook! :foo
55
57
  hook = obj.hooked[:foo]
@@ -61,4 +63,15 @@ describe Hooked do
61
63
  obj.hooked[:foo].should be_nil
62
64
  end
63
65
  end
66
+
67
+ [:before, :after, :around].each do |type|
68
+ describe ".instance_#{type}" do
69
+ it "hooks this pointcut on each new object of the class" do
70
+ @klass.send :"instance_#{type}", :foo, stub("advice")
71
+
72
+ obj = @klass.new
73
+ obj.hooked[:foo].graph.changed?.should be_true
74
+ end
75
+ end
76
+ end
64
77
  end
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hooked
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 23
4
5
  prerelease:
5
- version: 0.3.1
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 2
10
+ version: 0.3.2
6
11
  platform: ruby
7
12
  authors:
8
13
  - Lars Gierth
@@ -10,19 +15,22 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2011-04-20 00:00:00 +02:00
18
+ date: 2011-05-16 00:00:00 +02:00
14
19
  default_executable:
15
20
  dependencies:
16
21
  - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ type: :development
17
24
  name: rspec
18
25
  requirement: &id001 !ruby/object:Gem::Requirement
19
26
  none: false
20
27
  requirements:
21
28
  - - ">="
22
29
  - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
23
33
  version: "0"
24
- type: :development
25
- prerelease: false
26
34
  version_requirements: *id001
27
35
  description: Hooked lets you transparently aspectify your methods and blocks.
28
36
  email:
@@ -64,7 +72,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
64
72
  requirements:
65
73
  - - ">="
66
74
  - !ruby/object:Gem::Version
67
- hash: -167606191
75
+ hash: 3
68
76
  segments:
69
77
  - 0
70
78
  version: "0"
@@ -73,14 +81,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
81
  requirements:
74
82
  - - ">="
75
83
  - !ruby/object:Gem::Version
76
- hash: -167606191
84
+ hash: 3
77
85
  segments:
78
86
  - 0
79
87
  version: "0"
80
88
  requirements: []
81
89
 
82
90
  rubyforge_project:
83
- rubygems_version: 1.6.2
91
+ rubygems_version: 1.5.2
84
92
  signing_key:
85
93
  specification_version: 3
86
94
  summary: Aspect Orientation Made Simple