aspector 0.7.1 → 0.8.0
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/.rvmrc +6 -1
- data/Gemfile +1 -4
- data/Gemfile.lock +8 -24
- data/Rakefile +0 -18
- data/VERSION +1 -1
- data/aspector.gemspec +11 -21
- data/examples/cache_aspect.rb +9 -6
- data/lib/aspector/advice.rb +1 -1
- data/lib/aspector/aspect_instances.rb +1 -1
- data/lib/aspector/base.rb +140 -108
- data/lib/aspector/base_class_methods.rb +37 -26
- data/lib/aspector/method_matcher.rb +2 -2
- data/lib/aspector/module_extension.rb +9 -9
- data/lib/aspector/object_extension.rb +3 -3
- data/lib/aspector.rb +0 -1
- data/spec/functional/aspect_on_eigen_class_spec.rb +8 -2
- data/spec/functional/aspector_spec.rb +32 -1
- data/spec/functional/aspects_combined_spec.rb +8 -2
- data/spec/functional/execution_order_spec.rb +8 -2
- data/spec/unit/after_spec.rb +0 -17
- data/spec/unit/base_class_methods_spec.rb +4 -4
- data/spec/unit/base_spec.rb +1 -1
- data/spec/unit/method_matcher_spec.rb +2 -2
- metadata +90 -200
- data/lib/aspector/context.rb +0 -29
|
@@ -3,61 +3,72 @@ module Aspector
|
|
|
3
3
|
module ClassMethods
|
|
4
4
|
::Aspector::Base.extend(self)
|
|
5
5
|
|
|
6
|
-
def
|
|
7
|
-
@
|
|
6
|
+
def aop_advices
|
|
7
|
+
@aop_advices ||= []
|
|
8
8
|
end
|
|
9
|
+
alias :advices :aop_advices
|
|
9
10
|
|
|
10
|
-
def
|
|
11
|
-
@
|
|
11
|
+
def aop_default_options
|
|
12
|
+
@aop_default_options ||= {}
|
|
12
13
|
end
|
|
14
|
+
alias :default_options :aop_default_options
|
|
13
15
|
|
|
14
|
-
def
|
|
15
|
-
@deferred_logics ||= []
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def apply target, options = {}
|
|
16
|
+
def aop_apply target, options = {}
|
|
19
17
|
aspect_instance = new(target, options)
|
|
20
|
-
aspect_instance.
|
|
18
|
+
aspect_instance.send :aop_apply
|
|
19
|
+
aspect_instance.send :aop_add_method_hooks
|
|
21
20
|
aspect_instance
|
|
22
21
|
end
|
|
22
|
+
alias :apply :aop_apply
|
|
23
23
|
|
|
24
|
-
def
|
|
25
|
-
if @
|
|
26
|
-
@
|
|
24
|
+
def aop_default options
|
|
25
|
+
if @aop_default_options
|
|
26
|
+
@aop_default_options.merge! options
|
|
27
27
|
else
|
|
28
|
-
@
|
|
28
|
+
@aop_default_options = options
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
|
+
alias :default :aop_default
|
|
31
32
|
|
|
32
|
-
def
|
|
33
|
-
|
|
33
|
+
def aop_before *methods, &block
|
|
34
|
+
aop_advices << aop_create_advice(Aspector::AdviceMetadata::BEFORE, self, methods, &block)
|
|
34
35
|
end
|
|
36
|
+
alias :before :aop_before
|
|
35
37
|
|
|
36
|
-
def
|
|
37
|
-
|
|
38
|
+
def aop_before_filter *methods, &block
|
|
39
|
+
aop_advices << aop_create_advice(Aspector::AdviceMetadata::BEFORE_FILTER, self, methods, &block)
|
|
38
40
|
end
|
|
41
|
+
alias :before_filter :aop_before_filter
|
|
39
42
|
|
|
40
|
-
def
|
|
41
|
-
|
|
43
|
+
def aop_after *methods, &block
|
|
44
|
+
aop_advices << aop_create_advice(Aspector::AdviceMetadata::AFTER, self, methods, &block)
|
|
42
45
|
end
|
|
46
|
+
alias :after :aop_after
|
|
43
47
|
|
|
44
|
-
def
|
|
45
|
-
|
|
48
|
+
def aop_around *methods, &block
|
|
49
|
+
aop_advices << aop_create_advice(Aspector::AdviceMetadata::AROUND, self, methods, &block)
|
|
46
50
|
end
|
|
51
|
+
alias :around :aop_around
|
|
47
52
|
|
|
48
|
-
def
|
|
53
|
+
def aop_target code = nil, &block
|
|
49
54
|
logic = DeferredLogic.new(code || block)
|
|
50
|
-
|
|
55
|
+
aop_deferred_logics << logic
|
|
51
56
|
logic
|
|
52
57
|
end
|
|
58
|
+
alias :target :aop_target
|
|
53
59
|
|
|
54
|
-
def
|
|
60
|
+
def aop_options
|
|
55
61
|
DeferredOption.new
|
|
56
62
|
end
|
|
63
|
+
alias :options :aop_options
|
|
57
64
|
|
|
58
65
|
private
|
|
59
66
|
|
|
60
|
-
def
|
|
67
|
+
def aop_deferred_logics
|
|
68
|
+
@aop_deferred_logics ||= []
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def aop_create_advice meta_data, klass_or_module, *methods, &block
|
|
61
72
|
methods.flatten!
|
|
62
73
|
|
|
63
74
|
options = meta_data.default_options.clone
|
|
@@ -15,13 +15,13 @@ module Aspector
|
|
|
15
15
|
when Symbol
|
|
16
16
|
item.to_s == method
|
|
17
17
|
when DeferredLogic
|
|
18
|
-
value = aspect.
|
|
18
|
+
value = aspect.aop_deferred_logic_results(item)
|
|
19
19
|
if value
|
|
20
20
|
new_matcher = MethodMatcher.new(value)
|
|
21
21
|
new_matcher.match?(method)
|
|
22
22
|
end
|
|
23
23
|
when DeferredOption
|
|
24
|
-
value = aspect.
|
|
24
|
+
value = aspect.send(:aop_options)[item.key]
|
|
25
25
|
if value
|
|
26
26
|
new_matcher = MethodMatcher.new(value)
|
|
27
27
|
new_matcher.match?(method)
|
|
@@ -4,18 +4,18 @@ module Aspector
|
|
|
4
4
|
|
|
5
5
|
private
|
|
6
6
|
|
|
7
|
-
def
|
|
7
|
+
def aop_method_added method
|
|
8
8
|
return (block_given? and yield) if
|
|
9
|
-
@
|
|
10
|
-
@
|
|
9
|
+
@aop_creating_method or
|
|
10
|
+
@aop_instances.nil? or @aop_instances.empty?
|
|
11
11
|
|
|
12
|
-
aspects_applied_flag = :"@
|
|
12
|
+
aspects_applied_flag = :"@aop_applied_#{method}"
|
|
13
13
|
return (block_given? and yield) if instance_variable_get(aspects_applied_flag)
|
|
14
14
|
|
|
15
15
|
begin
|
|
16
16
|
instance_variable_set(aspects_applied_flag, true)
|
|
17
17
|
|
|
18
|
-
@
|
|
18
|
+
@aop_instances.apply_to_method(method.to_s)
|
|
19
19
|
|
|
20
20
|
yield if block_given?
|
|
21
21
|
ensure
|
|
@@ -23,16 +23,16 @@ module Aspector
|
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
def
|
|
26
|
+
def aop_singleton_method_added method
|
|
27
27
|
# Note: methods involved are on eigen class
|
|
28
28
|
eigen_class = class << self; self; end
|
|
29
29
|
|
|
30
|
-
return (block_given? and yield) if eigen_class.instance_variable_get(:@
|
|
30
|
+
return (block_given? and yield) if eigen_class.instance_variable_get(:@aop_creating_method)
|
|
31
31
|
|
|
32
|
-
aspect_instances = eigen_class.instance_variable_get(:@
|
|
32
|
+
aspect_instances = eigen_class.instance_variable_get(:@aop_instances)
|
|
33
33
|
return (block_given? and yield) if aspect_instances.nil? or aspect_instances.empty?
|
|
34
34
|
|
|
35
|
-
aspects_applied_flag = :"@
|
|
35
|
+
aspects_applied_flag = :"@aop_applied_#{method}"
|
|
36
36
|
return (block_given? and yield) if eigen_class.instance_variable_get(aspects_applied_flag)
|
|
37
37
|
|
|
38
38
|
begin
|
|
@@ -6,15 +6,15 @@ module Aspector
|
|
|
6
6
|
|
|
7
7
|
aspect = Aspector(options, &block)
|
|
8
8
|
|
|
9
|
-
aspect.
|
|
10
|
-
args.each {|target| aspect.
|
|
9
|
+
aspect.aop_apply(self) if self.is_a? Module
|
|
10
|
+
args.each {|target| aspect.aop_apply(target) }
|
|
11
11
|
|
|
12
12
|
aspect
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def Aspector options = {}, &block
|
|
16
16
|
klass = Class.new(Aspector::Base)
|
|
17
|
-
klass.
|
|
17
|
+
klass.aop_default options
|
|
18
18
|
klass.class_eval &block if block_given?
|
|
19
19
|
klass
|
|
20
20
|
end
|
data/lib/aspector.rb
CHANGED
|
@@ -17,7 +17,10 @@ describe "Aspector for eigen class" do
|
|
|
17
17
|
aspector(klass, :eigen_class => true) do
|
|
18
18
|
before :test do value << "do_before" end
|
|
19
19
|
|
|
20
|
-
after :test do
|
|
20
|
+
after :test do |result|
|
|
21
|
+
value << "do_after"
|
|
22
|
+
result
|
|
23
|
+
end
|
|
21
24
|
|
|
22
25
|
around :test do |&block|
|
|
23
26
|
value << "do_around_before"
|
|
@@ -43,7 +46,10 @@ describe "Aspector for eigen class" do
|
|
|
43
46
|
aspector(klass, :eigen_class => true) do
|
|
44
47
|
before :test do value << "do_before" end
|
|
45
48
|
|
|
46
|
-
after :test do
|
|
49
|
+
after :test do |result|
|
|
50
|
+
value << "do_after"
|
|
51
|
+
result
|
|
52
|
+
end
|
|
47
53
|
|
|
48
54
|
around :test do |&block|
|
|
49
55
|
value << "do_around_before"
|
|
@@ -7,7 +7,10 @@ describe "Aspector" do
|
|
|
7
7
|
aspector(klass) do
|
|
8
8
|
before :test do value << "do_before" end
|
|
9
9
|
|
|
10
|
-
after :test do
|
|
10
|
+
after :test do |result|
|
|
11
|
+
value << "do_after"
|
|
12
|
+
result
|
|
13
|
+
end
|
|
11
14
|
|
|
12
15
|
around :test do |&block|
|
|
13
16
|
value << "do_around_before"
|
|
@@ -22,6 +25,34 @@ describe "Aspector" do
|
|
|
22
25
|
obj.value.should == %w"do_before do_around_before test do_around_after do_after"
|
|
23
26
|
end
|
|
24
27
|
|
|
28
|
+
it "advices defined in after_initialization" do
|
|
29
|
+
klass = create_test_class
|
|
30
|
+
|
|
31
|
+
aspector(klass) do
|
|
32
|
+
def after_initialize
|
|
33
|
+
name = 'this'
|
|
34
|
+
|
|
35
|
+
before(:test) { value << "do_before(#{name})" }
|
|
36
|
+
|
|
37
|
+
after(:test) do |result|
|
|
38
|
+
value << "do_after(#{name})"
|
|
39
|
+
result
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
around(:test) do |&block|
|
|
43
|
+
value << "do_around_before(#{name})"
|
|
44
|
+
result = block.call
|
|
45
|
+
value << "do_around_after(#{name})"
|
|
46
|
+
result
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
obj = klass.new
|
|
52
|
+
obj.test
|
|
53
|
+
obj.value.should == %w"do_before(this) do_around_before(this) test do_around_after(this) do_after(this)"
|
|
54
|
+
end
|
|
55
|
+
|
|
25
56
|
it "multiple aspects should work together" do
|
|
26
57
|
klass = create_test_class
|
|
27
58
|
aspector(klass) do
|
|
@@ -7,7 +7,10 @@ describe "Aspects combined" do
|
|
|
7
7
|
aspector(klass) do
|
|
8
8
|
before :test do value << "do_before" end
|
|
9
9
|
|
|
10
|
-
after :test do
|
|
10
|
+
after :test do |result|
|
|
11
|
+
value << "do_after"
|
|
12
|
+
result
|
|
13
|
+
end
|
|
11
14
|
|
|
12
15
|
around :test do |&block|
|
|
13
16
|
value << "do_around_before"
|
|
@@ -20,7 +23,10 @@ describe "Aspects combined" do
|
|
|
20
23
|
aspector(klass) do
|
|
21
24
|
before :test do value << "do_before2" end
|
|
22
25
|
|
|
23
|
-
after :test do
|
|
26
|
+
after :test do |result|
|
|
27
|
+
value << "do_after2"
|
|
28
|
+
result
|
|
29
|
+
end
|
|
24
30
|
|
|
25
31
|
around :test do |&block|
|
|
26
32
|
value << "do_around_before2"
|
|
@@ -7,7 +7,10 @@ describe "Aspect execution order" do
|
|
|
7
7
|
aspector(klass) do
|
|
8
8
|
before :test do value << "do_before" end
|
|
9
9
|
|
|
10
|
-
after :test do
|
|
10
|
+
after :test do |result|
|
|
11
|
+
value << "do_after"
|
|
12
|
+
result
|
|
13
|
+
end
|
|
11
14
|
|
|
12
15
|
around :test do |&block|
|
|
13
16
|
value << "do_around_before"
|
|
@@ -18,7 +21,10 @@ describe "Aspect execution order" do
|
|
|
18
21
|
|
|
19
22
|
before :test do value << "do_before2" end
|
|
20
23
|
|
|
21
|
-
after :test do
|
|
24
|
+
after :test do |result|
|
|
25
|
+
value << "do_after2"
|
|
26
|
+
result
|
|
27
|
+
end
|
|
22
28
|
|
|
23
29
|
around :test do |&block|
|
|
24
30
|
value << "do_around_before2"
|
data/spec/unit/after_spec.rb
CHANGED
|
@@ -18,23 +18,6 @@ describe "After advices" do
|
|
|
18
18
|
obj.value.should == %w"test do_this"
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
it "context_arg" do
|
|
22
|
-
klass = create_test_class do
|
|
23
|
-
def do_this context, result
|
|
24
|
-
value << "do_this(#{context.method_name})"
|
|
25
|
-
result
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
aspector(klass) do
|
|
30
|
-
after :test, :do_this, :context_arg => true
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
obj = klass.new
|
|
34
|
-
obj.test
|
|
35
|
-
obj.value.should == %w"test do_this(test)"
|
|
36
|
-
end
|
|
37
|
-
|
|
38
21
|
it "method_name_arg" do
|
|
39
22
|
klass = create_test_class do
|
|
40
23
|
def do_this method, result
|
|
@@ -6,8 +6,8 @@ describe "Aspector::Base class methods" do
|
|
|
6
6
|
before :test, :do_before
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
klass.
|
|
10
|
-
advice = klass.
|
|
9
|
+
klass.send(:aop_advices).size.should == 1
|
|
10
|
+
advice = klass.send(:aop_advices).first
|
|
11
11
|
advice.before?.should be_true
|
|
12
12
|
advice.options[:skip_if_false].should_not be_true
|
|
13
13
|
advice.with_method.should == 'do_before'
|
|
@@ -18,8 +18,8 @@ describe "Aspector::Base class methods" do
|
|
|
18
18
|
before_filter :test, :do_before
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
klass.
|
|
22
|
-
advice = klass.
|
|
21
|
+
klass.send(:aop_advices).size.should == 1
|
|
22
|
+
advice = klass.send(:aop_advices).first
|
|
23
23
|
advice.before?.should be_true
|
|
24
24
|
advice.options[:skip_if_false].should be_true
|
|
25
25
|
advice.with_method.should == 'do_before'
|
data/spec/unit/base_spec.rb
CHANGED
|
@@ -23,7 +23,7 @@ module Aspector
|
|
|
23
23
|
matcher = MethodMatcher.new(logic)
|
|
24
24
|
|
|
25
25
|
aspect = mock(Aspector::Base)
|
|
26
|
-
aspect.should_receive(:
|
|
26
|
+
aspect.should_receive(:aop_deferred_logic_results).with(logic).once.and_return(/test/)
|
|
27
27
|
|
|
28
28
|
matcher.match?('test', aspect).should_not be_nil
|
|
29
29
|
end
|
|
@@ -33,7 +33,7 @@ module Aspector
|
|
|
33
33
|
matcher = MethodMatcher.new(option)
|
|
34
34
|
|
|
35
35
|
aspect = mock(Aspector::Base)
|
|
36
|
-
aspect.should_receive(:
|
|
36
|
+
aspect.should_receive(:aop_options).once.and_return({:methods => /test/})
|
|
37
37
|
|
|
38
38
|
matcher.match?('test', aspect).should_not be_nil
|
|
39
39
|
end
|