aspect4r 0.7.1 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/aspect4r.gemspec +14 -4
- data/lib/aspect4r/base.rb +10 -1
- data/lib/aspect4r/extensions/class_extension.rb +1 -6
- data/lib/aspect4r/extensions/module_extension.rb +21 -3
- data/lib/aspect4r/helper.rb +20 -9
- data/lib/aspect4r/model/advice.rb +22 -8
- data/lib/aspect4r/model/advices_for_method.rb +9 -28
- data/lib/aspect4r/model/aspect_data.rb +13 -5
- data/lib/aspect4r.rb +1 -0
- data/spec/aspect4r/advice_group_spec.rb +36 -0
- data/spec/aspect4r/advice_on_class_method_spec.rb +69 -0
- data/spec/aspect4r/advice_spec.rb +62 -0
- data/spec/aspect4r/advice_test_spec.rb +102 -0
- data/spec/aspect4r/advices_for_method_spec.rb +17 -0
- data/spec/aspect4r/classic_spec.rb +1 -1
- metadata +16 -6
data/Rakefile
CHANGED
@@ -6,7 +6,7 @@ begin
|
|
6
6
|
Jeweler::Tasks.new do |gem|
|
7
7
|
gem.name = "aspect4r"
|
8
8
|
gem.summary = %Q{Aspect Oriented Programming for ruby}
|
9
|
-
gem.description = %Q{AOP for ruby - use
|
9
|
+
gem.description = %Q{AOP for ruby - use before, after and around to trim your fat methods and reduce code duplication}
|
10
10
|
gem.email = "gcao99@gmail.com"
|
11
11
|
gem.homepage = "http://github.com/gcao/aspect4r"
|
12
12
|
gem.authors = ["Guoliang Cao"]
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.8.0
|
data/aspect4r.gemspec
CHANGED
@@ -5,12 +5,12 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{aspect4r}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.8.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Guoliang Cao"]
|
12
|
-
s.date = %q{2010-06-
|
13
|
-
s.description = %q{AOP for ruby - use
|
12
|
+
s.date = %q{2010-06-10}
|
13
|
+
s.description = %q{AOP for ruby - use before, after and around to trim your fat methods and reduce code duplication}
|
14
14
|
s.email = %q{gcao99@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
@@ -44,6 +44,11 @@ Gem::Specification.new do |s|
|
|
44
44
|
"lib/aspect4r/model/advices_for_method.rb",
|
45
45
|
"lib/aspect4r/model/aspect_data.rb",
|
46
46
|
"lib/aspect4r/return_this.rb",
|
47
|
+
"spec/aspect4r/advice_group_spec.rb",
|
48
|
+
"spec/aspect4r/advice_on_class_method_spec.rb",
|
49
|
+
"spec/aspect4r/advice_spec.rb",
|
50
|
+
"spec/aspect4r/advice_test_spec.rb",
|
51
|
+
"spec/aspect4r/advices_for_method_spec.rb",
|
47
52
|
"spec/aspect4r/after_spec.rb",
|
48
53
|
"spec/aspect4r/around_spec.rb",
|
49
54
|
"spec/aspect4r/before_spec.rb",
|
@@ -69,7 +74,12 @@ Gem::Specification.new do |s|
|
|
69
74
|
s.rubygems_version = %q{1.3.7}
|
70
75
|
s.summary = %q{Aspect Oriented Programming for ruby}
|
71
76
|
s.test_files = [
|
72
|
-
"spec/aspect4r/
|
77
|
+
"spec/aspect4r/advice_group_spec.rb",
|
78
|
+
"spec/aspect4r/advice_on_class_method_spec.rb",
|
79
|
+
"spec/aspect4r/advice_spec.rb",
|
80
|
+
"spec/aspect4r/advice_test_spec.rb",
|
81
|
+
"spec/aspect4r/advices_for_method_spec.rb",
|
82
|
+
"spec/aspect4r/after_spec.rb",
|
73
83
|
"spec/aspect4r/around_spec.rb",
|
74
84
|
"spec/aspect4r/before_spec.rb",
|
75
85
|
"spec/aspect4r/class_inheritance_spec.rb",
|
data/lib/aspect4r/base.rb
CHANGED
@@ -25,7 +25,16 @@ module Aspect4r
|
|
25
25
|
|
26
26
|
module ClassMethods
|
27
27
|
def a4r_data
|
28
|
-
@a4r_data ||= Aspect4r::Model::AspectData.new
|
28
|
+
@a4r_data ||= Aspect4r::Model::AspectData.new(self)
|
29
|
+
end
|
30
|
+
|
31
|
+
def a4r_group
|
32
|
+
a4r_data.change_group
|
33
|
+
|
34
|
+
if block_given?
|
35
|
+
yield
|
36
|
+
a4r_data.change_group
|
37
|
+
end
|
29
38
|
end
|
30
39
|
end
|
31
40
|
end
|
@@ -4,12 +4,7 @@ class Class
|
|
4
4
|
|
5
5
|
return if @a4r_data.nil? or @a4r_data.empty?
|
6
6
|
|
7
|
-
a4r_data = Aspect4r::Model::AspectData.new
|
8
|
-
|
9
|
-
# @a4r_data.each do |key, value|
|
10
|
-
# a4r_data[key] = (value.clone rescue value)
|
11
|
-
# end
|
12
|
-
|
7
|
+
a4r_data = Aspect4r::Model::AspectData.new(child)
|
13
8
|
a4r_data.methods_with_advices.merge(@a4r_data.methods_with_advices)
|
14
9
|
|
15
10
|
child.instance_variable_set('@a4r_data', a4r_data)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
class Module
|
2
2
|
def included_with_a4r(base)
|
3
|
-
included_without_a4r(
|
3
|
+
included_without_a4r(base)
|
4
4
|
|
5
5
|
return if @a4r_data.nil? or @a4r_data.empty?
|
6
6
|
|
@@ -14,12 +14,12 @@ class Module
|
|
14
14
|
alias included included_with_a4r
|
15
15
|
|
16
16
|
def method_added_with_a4r(method)
|
17
|
-
method_added_without_a4r(
|
17
|
+
method_added_without_a4r(method)
|
18
18
|
|
19
19
|
return if method.to_s =~ /a4r/
|
20
20
|
|
21
21
|
# save unbound method and create new method
|
22
|
-
if
|
22
|
+
if @a4r_data and method_advices = @a4r_data[method] and not Aspect4r::Helper.creating_method?
|
23
23
|
method_advices.wrapped_method = instance_method(method)
|
24
24
|
Aspect4r::Helper.create_method self, method
|
25
25
|
end
|
@@ -27,4 +27,22 @@ class Module
|
|
27
27
|
|
28
28
|
alias method_added_without_a4r method_added
|
29
29
|
alias method_added method_added_with_a4r
|
30
|
+
|
31
|
+
def singleton_method_added_with_a4r(method)
|
32
|
+
singleton_method_added_without_a4r(method)
|
33
|
+
|
34
|
+
return if method.to_s =~ /a4r/
|
35
|
+
|
36
|
+
# save unbound method and create new method
|
37
|
+
eigen_class = Aspect4r::Helper.eigen_class(self)
|
38
|
+
my_advices = eigen_class.instance_variable_get(:@a4r_data)
|
39
|
+
|
40
|
+
if my_advices and method_advices = my_advices[method] and not Aspect4r::Helper.creating_method?
|
41
|
+
method_advices.wrapped_method = eigen_class.instance_method(method)
|
42
|
+
Aspect4r::Helper.create_method eigen_class, method
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
alias singleton_method_added_without_a4r singleton_method_added
|
47
|
+
alias singleton_method_added singleton_method_added_with_a4r
|
30
48
|
end
|
data/lib/aspect4r/helper.rb
CHANGED
@@ -13,10 +13,18 @@ module Aspect4r
|
|
13
13
|
@creating_method
|
14
14
|
end
|
15
15
|
|
16
|
+
def self.eigen_class klass_or_module
|
17
|
+
klass_or_module.module_eval do
|
18
|
+
class << self
|
19
|
+
self
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
16
24
|
# Store original method in aspect data and refer to it whenever recreating method
|
17
25
|
def self.process_advice meta_data, klass_or_module, *methods, &block
|
18
26
|
methods.flatten!
|
19
|
-
|
27
|
+
|
20
28
|
options = meta_data.default_options.clone
|
21
29
|
options.merge!(methods.pop) if methods.last.is_a? Hash
|
22
30
|
options.merge!(meta_data.mandatory_options)
|
@@ -33,8 +41,9 @@ module Aspect4r
|
|
33
41
|
method = method.to_sym
|
34
42
|
klass_or_module.a4r_data.methods_with_advices << method
|
35
43
|
|
36
|
-
|
37
|
-
aspect
|
44
|
+
a4r_data = klass_or_module.a4r_data
|
45
|
+
aspect = a4r_data[method] ||= Aspect4r::Model::AdvicesForMethod.new(method)
|
46
|
+
aspect.add Aspect4r::Model::Advice.new(meta_data.advice_type, with_method, a4r_data.group, options)
|
38
47
|
|
39
48
|
if not aspect.wrapped_method and klass_or_module.instance_methods.include?(method.to_s)
|
40
49
|
aspect.wrapped_method = klass_or_module.instance_method(method)
|
@@ -57,16 +66,16 @@ module Aspect4r
|
|
57
66
|
end
|
58
67
|
|
59
68
|
grouped_advices = []
|
60
|
-
|
69
|
+
group = nil
|
70
|
+
inner_most = true
|
61
71
|
|
62
|
-
aspect.
|
63
|
-
if advice.around? and not grouped_advices.empty?
|
72
|
+
aspect.each do |advice|
|
73
|
+
if ((group and group != advice.group) or advice.around?) and not grouped_advices.empty?
|
64
74
|
# wrap up advices before current advice
|
65
75
|
create_method_for_before_after_advices klass, method, grouped_advices, inner_most
|
66
76
|
|
67
|
-
inner_most = false
|
68
|
-
|
69
77
|
grouped_advices = []
|
78
|
+
inner_most = false
|
70
79
|
end
|
71
80
|
|
72
81
|
# handle current advice
|
@@ -76,6 +85,8 @@ module Aspect4r
|
|
76
85
|
else
|
77
86
|
grouped_advices << advice
|
78
87
|
end
|
88
|
+
|
89
|
+
group = advice.group
|
79
90
|
end
|
80
91
|
|
81
92
|
# create wrap method for before/after advices which are not wrapped inside around advice.
|
@@ -160,7 +171,7 @@ module Aspect4r
|
|
160
171
|
CODE
|
161
172
|
|
162
173
|
def self.create_method_for_before_after_advices klass, method, advices, inner_most
|
163
|
-
before_advices = advices.select {|advice| advice.before? }
|
174
|
+
before_advices = advices.select {|advice| advice.before? or advice.before_filter? }
|
164
175
|
after_advices = advices.select {|advice| advice.after? }
|
165
176
|
|
166
177
|
code = METHOD_TEMPLATE.result(binding)
|
@@ -5,24 +5,38 @@ module Aspect4r
|
|
5
5
|
AFTER = 2
|
6
6
|
AROUND = 3
|
7
7
|
|
8
|
+
attr :group
|
8
9
|
attr_accessor :type, :with_method, :options
|
9
10
|
|
10
|
-
def initialize type, with_method, options = {}
|
11
|
+
def initialize type, with_method, group, options = {}
|
11
12
|
@type = type
|
12
13
|
@with_method = with_method
|
14
|
+
@group = group
|
13
15
|
@options = options
|
14
16
|
end
|
15
17
|
|
16
18
|
def name
|
17
19
|
options[:name] || with_method
|
18
20
|
end
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
|
22
|
+
def before?
|
23
|
+
type == BEFORE and not options[:skip_if_false]
|
24
|
+
end
|
25
|
+
|
26
|
+
def before_filter?
|
27
|
+
type == BEFORE and options[:skip_if_false]
|
28
|
+
end
|
29
|
+
|
30
|
+
def after?
|
31
|
+
type == AFTER
|
32
|
+
end
|
33
|
+
|
34
|
+
def around?
|
35
|
+
type == AROUND
|
36
|
+
end
|
37
|
+
|
38
|
+
def invoke obj, *args
|
39
|
+
obj.send with_method, *args
|
26
40
|
end
|
27
41
|
end
|
28
42
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Aspect4r
|
2
2
|
module Model
|
3
|
-
class AdvicesForMethod
|
3
|
+
class AdvicesForMethod < Array
|
4
4
|
attr_reader :method
|
5
5
|
attr_accessor :wrapped_method
|
6
6
|
|
@@ -8,37 +8,18 @@ module Aspect4r
|
|
8
8
|
@method = method
|
9
9
|
end
|
10
10
|
|
11
|
-
def advices
|
12
|
-
@advices ||= []
|
13
|
-
end
|
14
|
-
|
15
11
|
def add new_advice
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
def empty?
|
20
|
-
@advices.nil? or @advices.empty?
|
12
|
+
self << new_advice unless include?(new_advice)
|
21
13
|
end
|
22
|
-
|
23
|
-
def include? new_advice
|
24
|
-
advices.detect { |advice| advice.name == new_advice.name }
|
25
|
-
end
|
26
|
-
|
27
|
-
def merge! another
|
28
|
-
unless another.nil? or another.empty?
|
29
|
-
another.advices.each do |advice|
|
30
|
-
advices.push advice unless include?(advice)
|
31
|
-
end
|
32
|
-
end
|
33
14
|
|
34
|
-
|
15
|
+
def include? new_advice
|
16
|
+
detect { |advice| advice.name == new_advice.name }
|
35
17
|
end
|
36
|
-
|
37
|
-
def
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
o
|
18
|
+
|
19
|
+
def [] index
|
20
|
+
return super unless index.is_a? String or index.is_a? Symbol
|
21
|
+
|
22
|
+
detect {|advice| advice.name.to_sym == index.to_sym }
|
42
23
|
end
|
43
24
|
end
|
44
25
|
end
|
@@ -3,12 +3,20 @@ require 'set'
|
|
3
3
|
module Aspect4r
|
4
4
|
module Model
|
5
5
|
class AspectData < Hash
|
6
|
-
def initialize
|
7
|
-
|
6
|
+
def initialize klass_or_module
|
7
|
+
@klass_or_module = klass_or_module
|
8
8
|
end
|
9
|
-
|
10
|
-
def
|
11
|
-
@
|
9
|
+
|
10
|
+
def group_index
|
11
|
+
@group_index ||= 0
|
12
|
+
end
|
13
|
+
|
14
|
+
def group
|
15
|
+
"#{@klass_or_module.hash}:#{group_index}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def change_group
|
19
|
+
@group_index = group_index + 1
|
12
20
|
end
|
13
21
|
|
14
22
|
def methods_with_advices
|
data/lib/aspect4r.rb
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe "Group of advices" do
|
4
|
+
it "should work" do
|
5
|
+
klass = Class.new do
|
6
|
+
include Aspect4r
|
7
|
+
|
8
|
+
attr :value
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@value = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def test
|
15
|
+
@value << "test"
|
16
|
+
end
|
17
|
+
|
18
|
+
a4r_group do
|
19
|
+
before :test do
|
20
|
+
@value << "before(group1)"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
a4r_group do
|
25
|
+
before :test do
|
26
|
+
@value << "before(group2)"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
o = klass.new
|
32
|
+
o.test
|
33
|
+
|
34
|
+
o.value.should == %w(before(group2) before(group1) test)
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe "Advices on singleton method (also known as class method)" do
|
4
|
+
it "class method" do
|
5
|
+
class AdvicesOnClassMethod
|
6
|
+
class << self
|
7
|
+
include Aspect4r
|
8
|
+
|
9
|
+
def value
|
10
|
+
@value ||= []
|
11
|
+
end
|
12
|
+
|
13
|
+
around :test do |proxy|
|
14
|
+
value << "around(before)"
|
15
|
+
a4r_invoke proxy
|
16
|
+
value << "around(after)"
|
17
|
+
end
|
18
|
+
|
19
|
+
before :test do
|
20
|
+
value << "before"
|
21
|
+
end
|
22
|
+
|
23
|
+
after :test do
|
24
|
+
value << "after"
|
25
|
+
end
|
26
|
+
|
27
|
+
def test
|
28
|
+
value << "test"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
AdvicesOnClassMethod.test
|
34
|
+
AdvicesOnClassMethod.value.should == %w(before around(before) test around(after) after)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "module singleton method" do
|
38
|
+
module AdvicesOnModuleSingletonMethod
|
39
|
+
class << self
|
40
|
+
include Aspect4r
|
41
|
+
|
42
|
+
def value
|
43
|
+
@value ||= []
|
44
|
+
end
|
45
|
+
|
46
|
+
around :test do |proxy|
|
47
|
+
value << "around(before)"
|
48
|
+
a4r_invoke proxy
|
49
|
+
value << "around(after)"
|
50
|
+
end
|
51
|
+
|
52
|
+
before :test do
|
53
|
+
value << "before"
|
54
|
+
end
|
55
|
+
|
56
|
+
after :test do
|
57
|
+
value << "after"
|
58
|
+
end
|
59
|
+
|
60
|
+
def test
|
61
|
+
value << "test"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
AdvicesOnModuleSingletonMethod.test
|
67
|
+
AdvicesOnModuleSingletonMethod.value.should == %w(before around(before) test around(after) after)
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Aspect4r::Model::Advice do
|
4
|
+
it "before? returns true for before advice" do
|
5
|
+
advice = Aspect4r::Model::Advice.new Aspect4r::Model::Advice::BEFORE, :advice_method, :group
|
6
|
+
advice.before?.should be_true
|
7
|
+
end
|
8
|
+
|
9
|
+
it "before_filter? returns false for before_filter advice" do
|
10
|
+
advice = Aspect4r::Model::Advice.new Aspect4r::Model::Advice::BEFORE, :advice_method, :group
|
11
|
+
advice.before_filter?.should be_false
|
12
|
+
end
|
13
|
+
|
14
|
+
it "before? returns false for before advice" do
|
15
|
+
advice = Aspect4r::Model::Advice.new Aspect4r::Model::Advice::BEFORE, :advice_method, :group, :skip_if_false => true
|
16
|
+
advice.before?.should be_false
|
17
|
+
end
|
18
|
+
|
19
|
+
it "before_filter? returns true for before_filter advice" do
|
20
|
+
advice = Aspect4r::Model::Advice.new Aspect4r::Model::Advice::BEFORE, :advice_method, :group, :skip_if_false => true
|
21
|
+
advice.before_filter?.should be_true
|
22
|
+
end
|
23
|
+
|
24
|
+
it "invoke before advice" do
|
25
|
+
advice = Aspect4r::Model::Advice.new Aspect4r::Model::Advice::BEFORE, :advice_method, :group
|
26
|
+
|
27
|
+
o = Object.new
|
28
|
+
o.class.send :define_method, :advice_method do |*args|
|
29
|
+
do_something *args
|
30
|
+
end
|
31
|
+
|
32
|
+
o.expects(:do_something).with(1)
|
33
|
+
|
34
|
+
advice.invoke(o, 1)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "invoke after advice" do
|
38
|
+
advice = Aspect4r::Model::Advice.new Aspect4r::Model::Advice::AFTER, :advice_method, :group
|
39
|
+
|
40
|
+
o = Object.new
|
41
|
+
o.class.send :define_method, :advice_method do |result, *args|
|
42
|
+
do_something result, *args
|
43
|
+
end
|
44
|
+
|
45
|
+
o.expects(:do_something).with(1)
|
46
|
+
|
47
|
+
advice.invoke(o, 1)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "invoke around advice" do
|
51
|
+
advice = Aspect4r::Model::Advice.new Aspect4r::Model::Advice::AROUND, :advice_method, :group
|
52
|
+
|
53
|
+
o = Object.new
|
54
|
+
o.class.send :define_method, :advice_method do |proxy, *args|
|
55
|
+
a4r_invoke proxy, *args
|
56
|
+
end
|
57
|
+
|
58
|
+
o.expects(:a4r_invoke).with(:proxy, 1)
|
59
|
+
|
60
|
+
advice.invoke(o, :proxy, 1)
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe "Test Advices" do
|
4
|
+
before do
|
5
|
+
@klass = Class.new do
|
6
|
+
include Aspect4r
|
7
|
+
|
8
|
+
attr :value
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@value = []
|
12
|
+
end
|
13
|
+
|
14
|
+
around :test do |proxy, input|
|
15
|
+
@value << "around(before)"
|
16
|
+
a4r_invoke proxy, input
|
17
|
+
@value << "around(after)"
|
18
|
+
end
|
19
|
+
|
20
|
+
before :test, :name => 'before_advice' do |input|
|
21
|
+
@value << 'before'
|
22
|
+
end
|
23
|
+
|
24
|
+
before_filter :test do |input|
|
25
|
+
@value << 'before_filter'
|
26
|
+
input >= 0
|
27
|
+
end
|
28
|
+
|
29
|
+
after :test do |result, input|
|
30
|
+
@value << "after"
|
31
|
+
result * 100
|
32
|
+
end
|
33
|
+
|
34
|
+
def test input
|
35
|
+
@value << "test"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "number of advices" do
|
41
|
+
@klass.a4r_data[:test].size.should == 4
|
42
|
+
end
|
43
|
+
|
44
|
+
it "around advice" do
|
45
|
+
advice = @klass.a4r_data[:test][0]
|
46
|
+
advice.around?.should be_true
|
47
|
+
|
48
|
+
o = @klass.new
|
49
|
+
o.expects(:a4r_invoke).with(:proxy, 1)
|
50
|
+
|
51
|
+
advice.invoke(o, :proxy, 1)
|
52
|
+
|
53
|
+
o.value.should == %w(around(before) around(after))
|
54
|
+
end
|
55
|
+
|
56
|
+
it "before advice" do
|
57
|
+
advice = @klass.a4r_data[:test][1]
|
58
|
+
advice.before?.should be_true
|
59
|
+
|
60
|
+
o = @klass.new
|
61
|
+
advice.invoke(o, 1)
|
62
|
+
|
63
|
+
o.value.should == %w(before)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "before advice retrieved by name" do
|
67
|
+
advice = @klass.a4r_data[:test][:before_advice]
|
68
|
+
advice.before?.should be_true
|
69
|
+
|
70
|
+
o = @klass.new
|
71
|
+
advice.invoke(o, 1)
|
72
|
+
|
73
|
+
o.value.should == %w(before)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "before_filter advice returns true if input is not negative" do
|
77
|
+
advice = @klass.a4r_data[:test][2]
|
78
|
+
advice.before_filter?.should be_true
|
79
|
+
|
80
|
+
o = @klass.new
|
81
|
+
advice.invoke(o, 1).should be_true
|
82
|
+
|
83
|
+
o.value.should == %w(before_filter)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "before_filter advice returns false if input is negative" do
|
87
|
+
advice = @klass.a4r_data[:test][2]
|
88
|
+
|
89
|
+
o = @klass.new
|
90
|
+
advice.invoke(o, -1).should be_false
|
91
|
+
end
|
92
|
+
|
93
|
+
it "after advice" do
|
94
|
+
advice = @klass.a4r_data[:test][3]
|
95
|
+
advice.after?.should be_true
|
96
|
+
|
97
|
+
o = @klass.new
|
98
|
+
advice.invoke(o, 1, 1).should == 100
|
99
|
+
|
100
|
+
o.value.should == %w(after)
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Aspect4r::Model::AdvicesForMethod do
|
4
|
+
it "should be able to get advice by name" do
|
5
|
+
advices = Aspect4r::Model::AdvicesForMethod.new(:test)
|
6
|
+
advice1 = Aspect4r::Model::Advice.new Aspect4r::Model::Advice::BEFORE, :advice_method, :group, :name => :advice1
|
7
|
+
advices.add advice1
|
8
|
+
advices[:advice1].should == advice1
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should be able to get advice by name String" do
|
12
|
+
advices = Aspect4r::Model::AdvicesForMethod.new(:test)
|
13
|
+
advice1 = Aspect4r::Model::Advice.new Aspect4r::Model::Advice::BEFORE, :advice_method, :group, :name => :advice1
|
14
|
+
advices.add advice1
|
15
|
+
advices['advice1'].should == advice1
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aspect4r
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 63
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 8
|
9
|
+
- 0
|
10
|
+
version: 0.8.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Guoliang Cao
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-06-
|
18
|
+
date: 2010-06-10 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -34,7 +34,7 @@ dependencies:
|
|
34
34
|
version: 1.2.9
|
35
35
|
type: :development
|
36
36
|
version_requirements: *id001
|
37
|
-
description: AOP for ruby - use
|
37
|
+
description: AOP for ruby - use before, after and around to trim your fat methods and reduce code duplication
|
38
38
|
email: gcao99@gmail.com
|
39
39
|
executables: []
|
40
40
|
|
@@ -71,6 +71,11 @@ files:
|
|
71
71
|
- lib/aspect4r/model/advices_for_method.rb
|
72
72
|
- lib/aspect4r/model/aspect_data.rb
|
73
73
|
- lib/aspect4r/return_this.rb
|
74
|
+
- spec/aspect4r/advice_group_spec.rb
|
75
|
+
- spec/aspect4r/advice_on_class_method_spec.rb
|
76
|
+
- spec/aspect4r/advice_spec.rb
|
77
|
+
- spec/aspect4r/advice_test_spec.rb
|
78
|
+
- spec/aspect4r/advices_for_method_spec.rb
|
74
79
|
- spec/aspect4r/after_spec.rb
|
75
80
|
- spec/aspect4r/around_spec.rb
|
76
81
|
- spec/aspect4r/before_spec.rb
|
@@ -124,6 +129,11 @@ signing_key:
|
|
124
129
|
specification_version: 3
|
125
130
|
summary: Aspect Oriented Programming for ruby
|
126
131
|
test_files:
|
132
|
+
- spec/aspect4r/advice_group_spec.rb
|
133
|
+
- spec/aspect4r/advice_on_class_method_spec.rb
|
134
|
+
- spec/aspect4r/advice_spec.rb
|
135
|
+
- spec/aspect4r/advice_test_spec.rb
|
136
|
+
- spec/aspect4r/advices_for_method_spec.rb
|
127
137
|
- spec/aspect4r/after_spec.rb
|
128
138
|
- spec/aspect4r/around_spec.rb
|
129
139
|
- spec/aspect4r/before_spec.rb
|