aspect4r 0.9.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,31 +7,29 @@ Aspect Oriented Programming support for Ruby
7
7
  * extract common logic from multiple methods into before, after, around advices
8
8
  * before_filter as method filters (work like before_filters in web application)
9
9
  * advices can be customized (e.g. to take an optional method name argument)
10
- * work as a supporting piece for more complex AOP tasks
10
+ * well-tested and optimized
11
11
 
12
12
  == Usage
13
13
 
14
- API documentation: http://gcao.posterous.com/aspect4r-usage-and-public-api
14
+ API documentation: https://github.com/gcao/aspect4r/wiki/Api
15
15
 
16
16
  class A
17
17
  include Aspect4r
18
18
 
19
- around :test do |proxy_method, value|
20
- a4r_invoke proxy_method, value
21
- end
22
-
23
- before :test do |value|
24
- puts "entering test(#{value})"
19
+ around /test/ do |*args, &block|
20
+ ...
21
+ block.call *args
22
+ ...
25
23
  end
26
24
 
27
25
  before :test, :do_something
28
26
 
29
- before_filter :test do |value|
30
- value >= 0
27
+ before_filter :test do |*args|
28
+ authorized?
31
29
  end
32
30
 
33
- after :test do |result, value|
34
- puts "test(#{value}) returned #{result}"
31
+ after :test do |result, *args|
32
+ puts "test(#{*args}) returned #{result}"
35
33
  result
36
34
  end
37
35
 
@@ -59,28 +57,6 @@ Execution model: http://gcao.posterous.com/aspect4r-documentation-advice-executi
59
57
 
60
58
  See github issues http://github.com/gcao/aspect4r/issues
61
59
 
62
- Support regular expression on methods to be advised. When such an advice is defined, methods defined before it
63
- will not be processed, only new methods are.
64
-
65
- Advices are stored in an array. Whenever a method is created, the whole array is checked to find the advices that
66
- are applicable to it. Those applicable advices are used to construct the new method. This provides a lot
67
- flexibilities but can be a performance burden.
68
-
69
- One way to improve performance is to use array to store advices that use regular expression. Advices specific to
70
- methods are stored in hash. (Do this only when performance is proved to be a problem in real world scenarios)
71
-
72
- Advice name: each advice can take a :name option. If no name is given, the advice method name is used as the advice
73
- name. If advice logic is in a block and :name is not set, the advice will not have a name. Multiple advices can
74
- have same name.
75
- What use can this bring? skip advice for specific methods. debugging purpose. testing purpose.
76
-
77
- Inherit advices: add option :inherit to all advices. Advices whose :inherit options are set to true will be copied to
78
- child modules/classes. By default :inherit is set to false.
79
- This adds a lot complexity and might not be a good idea.
80
- This can be achieved by putting advices in ModuleX.included or ClassY.inherited
81
-
82
- a4r_remove_advices: will remove advices with given names. If no name is given, all advices are removed.
83
-
84
60
  == Note on Patches/Pull Requests
85
61
 
86
62
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.1
1
+ 0.10.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{aspect4r}
8
- s.version = "0.9.1"
8
+ s.version = "0.10.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{2011-05-02}
12
+ s.date = %q{2011-09-01}
13
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 = [
@@ -75,41 +75,6 @@ Gem::Specification.new do |s|
75
75
  s.require_paths = ["lib"]
76
76
  s.rubygems_version = %q{1.4.2}
77
77
  s.summary = %q{Aspect Oriented Programming for ruby}
78
- s.test_files = [
79
- "examples/advices_on_class_method_example.rb",
80
- "examples/after_example.rb",
81
- "examples/around_example.rb",
82
- "examples/before_example.rb",
83
- "examples/combined_example.rb",
84
- "examples/test_advices_example.rb",
85
- "spec/aspect4r/advice_on_class_method_spec.rb",
86
- "spec/aspect4r/advice_scope_spec.rb",
87
- "spec/aspect4r/advice_test_spec.rb",
88
- "spec/aspect4r/after_spec.rb",
89
- "spec/aspect4r/around_spec.rb",
90
- "spec/aspect4r/before_spec.rb",
91
- "spec/aspect4r/class_inheritance_spec.rb",
92
- "spec/aspect4r/classic_spec.rb",
93
- "spec/aspect4r/helper_spec.rb",
94
- "spec/aspect4r/include_advices_from_module_spec.rb",
95
- "spec/aspect4r/inheritance_inclusion_combined_spec.rb",
96
- "spec/aspect4r/method_added_spec.rb",
97
- "spec/aspect4r/model/advice_group_spec.rb",
98
- "spec/aspect4r/model/advice_spec.rb",
99
- "spec/aspect4r/model/method_matcher_spec.rb",
100
- "spec/aspect4r/module_inclusion_spec.rb",
101
- "spec/aspect4r/regexp_methods_spec.rb",
102
- "spec/aspect4r/singleton_method_added_spec.rb",
103
- "spec/aspect4r/super_in_method_spec.rb",
104
- "spec/aspect4r_spec.rb",
105
- "spec/spec_helper.rb",
106
- "test/after_test.rb",
107
- "test/around_test.rb",
108
- "test/before_test.rb",
109
- "test/combined_test.rb",
110
- "test/method_invocation_test.rb",
111
- "test/test_helper.rb"
112
- ]
113
78
 
114
79
  if s.respond_to? :specification_version then
115
80
  s.specification_version = 3
@@ -3,9 +3,9 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'aspect4r'
4
4
 
5
5
  class A
6
- class << self
7
- include Aspect4r
8
-
6
+ include Aspect4r
7
+
8
+ class << self
9
9
  around :test do |input, &block|
10
10
  puts 'around test (before)'
11
11
  result = block.call input
@@ -5,17 +5,17 @@ require 'aspect4r'
5
5
  class A
6
6
  include Aspect4r
7
7
 
8
- def test value
9
- puts 'test'
10
- value
11
- end
12
-
13
8
  around :test do |value, &block|
14
9
  puts 'before test'
15
10
  result = block.call value
16
11
  puts 'after test'
17
12
  result
18
13
  end
14
+
15
+ def test value
16
+ puts 'test'
17
+ value
18
+ end
19
19
  end
20
20
 
21
21
  puts "Example 1:"
@@ -5,10 +5,6 @@ require 'aspect4r'
5
5
  class A
6
6
  include Aspect4r
7
7
 
8
- def test value
9
- puts 'test'
10
- end
11
-
12
8
  before :test do |value|
13
9
  puts 'before test'
14
10
  end
@@ -17,6 +13,10 @@ class A
17
13
  puts 'check before test'
18
14
  value >= 0
19
15
  end
16
+
17
+ def test value
18
+ puts 'test'
19
+ end
20
20
  end
21
21
 
22
22
  puts "Example 1:"
@@ -5,11 +5,6 @@ require 'aspect4r'
5
5
  class A
6
6
  include Aspect4r
7
7
 
8
- def test value
9
- puts 'test'
10
- value
11
- end
12
-
13
8
  around :test do |value, &block|
14
9
  puts 'around test 1'
15
10
  result = block.call value
@@ -25,6 +20,11 @@ class A
25
20
  puts 'after test'
26
21
  result
27
22
  end
23
+
24
+ def test value
25
+ puts 'test'
26
+ value
27
+ end
28
28
  end
29
29
 
30
30
  puts "Example 1:"
@@ -1,6 +1,7 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
 
3
3
  require 'rubygems'
4
+ require 'mocha'
4
5
  require 'spec'
5
6
  require 'spec/autorun'
6
7
 
@@ -48,22 +49,22 @@ end
48
49
  # =============================== Tests start here ===========================
49
50
  describe Klass do
50
51
  it "number of advices" do
51
- Klass.a4r_data[:test].size.should == 4
52
+ Klass.a4r_data.advices_for_method(:test).size.should == 4
52
53
  end
53
54
 
54
55
  it "around advice" do
55
- advice = Klass.a4r_data[:test][0]
56
+ advice = Klass.a4r_data.advices_for_method(:test)[0]
56
57
  advice.around?.should be_true
57
58
 
58
59
  o = Klass.new
59
60
 
60
- advice.invoke(o, :proxy, 1)
61
+ advice.invoke(o, 1) {}
61
62
 
62
63
  o.value.should == %w(around(before) around(after))
63
64
  end
64
65
 
65
66
  it "before advice" do
66
- advice = Klass.a4r_data[:test][1]
67
+ advice = Klass.a4r_data.advices_for_method(:test)[1]
67
68
  advice.before?.should be_true
68
69
 
69
70
  o = Klass.new
@@ -71,19 +72,9 @@ describe Klass do
71
72
 
72
73
  o.value.should == %w(before)
73
74
  end
74
-
75
- it "before advice retrieved by name" do
76
- advice = Klass.a4r_data[:test][:before_advice]
77
- advice.before?.should be_true
78
-
79
- o = Klass.new
80
- advice.invoke(o, 1)
81
75
 
82
- o.value.should == %w(before)
83
- end
84
-
85
76
  it "before_filter advice returns true if input is not negative" do
86
- advice = Klass.a4r_data[:test][2]
77
+ advice = Klass.a4r_data.advices_for_method(:test)[2]
87
78
  advice.before_filter?.should be_true
88
79
 
89
80
  o = Klass.new
@@ -93,14 +84,14 @@ describe Klass do
93
84
  end
94
85
 
95
86
  it "before_filter advice returns false if input is negative" do
96
- advice = Klass.a4r_data[:test][2]
87
+ advice = Klass.a4r_data.advices_for_method(:test)[2]
97
88
 
98
89
  o = Klass.new
99
90
  advice.invoke(o, -1).should be_false
100
91
  end
101
92
 
102
93
  it "after advice" do
103
- advice = Klass.a4r_data[:test][3]
94
+ advice = Klass.a4r_data.advices_for_method(:test)[3]
104
95
  advice.after?.should be_true
105
96
 
106
97
  o = Klass.new
@@ -78,7 +78,7 @@ module Aspect4r
78
78
  ensure
79
79
  methods.each do |method|
80
80
  method_with_advices = "#{method}_with_advices"
81
- next unless instance_methods.include?(method_with_advices)
81
+ next unless instance_methods.detect {|method| method.to_s == method_with_advices }
82
82
 
83
83
  send :alias_method, method, method_with_advices
84
84
  self.send :remove_method, method_with_advices
@@ -4,8 +4,8 @@ module Aspect4r
4
4
  module Helper
5
5
  def self.find_available_method_name klass, method_name_prefix
6
6
  0.upto(10000) do |i|
7
- m = "#{method_name_prefix}#{i}_#{klass.hash}"
8
- return m unless klass.private_instance_methods(false).include?(m)
7
+ m = "#{method_name_prefix}#{i}_#{klass.hash.abs}"
8
+ return m unless klass.private_instance_methods(false).detect {|method| method.to_s == m }
9
9
  end
10
10
  end
11
11
 
@@ -54,11 +54,11 @@ module Aspect4r
54
54
 
55
55
  wrapped_method = a4r_data.wrapped_methods[method]
56
56
 
57
- if not wrapped_method and klass_or_module.instance_methods.include?(method)
57
+ if not wrapped_method and klass_or_module.instance_methods.detect {|m| m.to_s == method }
58
58
  wrapped_method = klass_or_module.instance_method(method)
59
59
  a4r_data.wrapped_methods[method] = wrapped_method
60
60
  end
61
-
61
+
62
62
  create_method klass_or_module, method if wrapped_method
63
63
  end
64
64
  end
@@ -21,7 +21,7 @@ describe "Advices on singleton method (also known as class method)" do
21
21
  value << "before"
22
22
  end
23
23
 
24
- after :test do
24
+ after :test do |result|
25
25
  value << "after"
26
26
  end
27
27
 
@@ -38,9 +38,9 @@ describe "Advices on singleton method (also known as class method)" do
38
38
  it "module singleton method" do
39
39
  module AdvicesOnModuleSingletonMethod
40
40
  include Aspect4r
41
-
41
+
42
42
  class << self
43
-
43
+
44
44
  def value
45
45
  @value ||= []
46
46
  end
@@ -55,7 +55,7 @@ describe "Advices on singleton method (also known as class method)" do
55
55
  value << "before"
56
56
  end
57
57
 
58
- after :test do
58
+ after :test do |result|
59
59
  value << "after"
60
60
  end
61
61
 
@@ -86,7 +86,7 @@ describe Aspect4r::Before do
86
86
 
87
87
  o = @klass.new
88
88
  o.test('something')
89
-
89
+
90
90
  o.value.should == 'something'
91
91
  end
92
92
 
@@ -101,7 +101,7 @@ describe Aspect4r::Before do
101
101
 
102
102
  o = @klass.new
103
103
  o.test('something')
104
-
104
+
105
105
  s.should == 'test'
106
106
  end
107
107
 
@@ -110,7 +110,7 @@ describe Aspect4r::Before do
110
110
  def do_something value
111
111
  Aspect4r::ReturnThis.new('do_something')
112
112
  end
113
-
113
+
114
114
  before :test, :do_something
115
115
  end
116
116
 
@@ -122,7 +122,7 @@ describe Aspect4r::Before do
122
122
 
123
123
  it "should skip original method if before_filter advice returned false" do
124
124
  @klass.class_eval do
125
- before_filter :test do
125
+ before_filter :test do |value|
126
126
  false
127
127
  end
128
128
  end
@@ -135,7 +135,7 @@ describe Aspect4r::Before do
135
135
 
136
136
  it "should not skip original method if before_filter did not return false or nil" do
137
137
  @klass.class_eval do
138
- before_filter :test do
138
+ before_filter :test do |value|
139
139
  true
140
140
  end
141
141
  end
@@ -126,7 +126,7 @@ describe "Aspect4r execution order" do
126
126
  @value << "after1"
127
127
  end
128
128
 
129
- after :test do
129
+ after :test do |result|
130
130
  @value << "after2"
131
131
  end
132
132
  end
@@ -232,7 +232,7 @@ describe "Aspect4r chaining (add advice to advice method)" do
232
232
  @value << "before(do_something)"
233
233
  end
234
234
 
235
- after :do_something do
235
+ after :do_something do |result|
236
236
  @value << "after(do_something)"
237
237
  end
238
238
 
@@ -242,11 +242,11 @@ describe "Aspect4r chaining (add advice to advice method)" do
242
242
 
243
243
  after :test, :process_result
244
244
 
245
- before :process_result do
245
+ before :process_result do |result|
246
246
  @value << "before(process_result)"
247
247
  end
248
248
 
249
- after :process_result do
249
+ after :process_result do |result, *args|
250
250
  @value << "after(process_result)"
251
251
  end
252
252
  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: 57
4
+ hash: 55
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 9
9
- - 1
10
- version: 0.9.1
8
+ - 10
9
+ - 0
10
+ version: 0.10.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: 2011-05-02 00:00:00 -04:00
18
+ date: 2011-09-01 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -131,37 +131,5 @@ rubygems_version: 1.4.2
131
131
  signing_key:
132
132
  specification_version: 3
133
133
  summary: Aspect Oriented Programming for ruby
134
- test_files:
135
- - examples/advices_on_class_method_example.rb
136
- - examples/after_example.rb
137
- - examples/around_example.rb
138
- - examples/before_example.rb
139
- - examples/combined_example.rb
140
- - examples/test_advices_example.rb
141
- - spec/aspect4r/advice_on_class_method_spec.rb
142
- - spec/aspect4r/advice_scope_spec.rb
143
- - spec/aspect4r/advice_test_spec.rb
144
- - spec/aspect4r/after_spec.rb
145
- - spec/aspect4r/around_spec.rb
146
- - spec/aspect4r/before_spec.rb
147
- - spec/aspect4r/class_inheritance_spec.rb
148
- - spec/aspect4r/classic_spec.rb
149
- - spec/aspect4r/helper_spec.rb
150
- - spec/aspect4r/include_advices_from_module_spec.rb
151
- - spec/aspect4r/inheritance_inclusion_combined_spec.rb
152
- - spec/aspect4r/method_added_spec.rb
153
- - spec/aspect4r/model/advice_group_spec.rb
154
- - spec/aspect4r/model/advice_spec.rb
155
- - spec/aspect4r/model/method_matcher_spec.rb
156
- - spec/aspect4r/module_inclusion_spec.rb
157
- - spec/aspect4r/regexp_methods_spec.rb
158
- - spec/aspect4r/singleton_method_added_spec.rb
159
- - spec/aspect4r/super_in_method_spec.rb
160
- - spec/aspect4r_spec.rb
161
- - spec/spec_helper.rb
162
- - test/after_test.rb
163
- - test/around_test.rb
164
- - test/before_test.rb
165
- - test/combined_test.rb
166
- - test/method_invocation_test.rb
167
- - test/test_helper.rb
134
+ test_files: []
135
+