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 CHANGED
@@ -1,3 +1,8 @@
1
1
  rvm_gemset_create_on_use_flag=1
2
2
 
3
- rvm use 1.8.7@aspector
3
+ if [ "$RUBY_VERSION" == "1.9" ]; then
4
+ rvm use 1.9.2@aspector
5
+ else
6
+ rvm use 1.8.7@aspector
7
+ fi
8
+
data/Gemfile CHANGED
@@ -1,11 +1,8 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  group :development do
4
- gem "rspec", "~> 2.3.0"
4
+ gem "rspec"
5
5
  gem "jeweler", "~> 1.6.4"
6
- gem "rcov", ">= 0"
7
- gem "reek", "~> 1.2.8"
8
- gem "roodi", "~> 2.1.0"
9
6
 
10
7
  gem 'ruby-prof'
11
8
 
data/Gemfile.lock CHANGED
@@ -20,28 +20,15 @@ GEM
20
20
  rake
21
21
  rake (0.9.2.2)
22
22
  rb-fsevent (0.4.3.1)
23
- rcov (1.0.0)
24
- reek (1.2.8)
25
- ruby2ruby (~> 1.2)
26
- ruby_parser (~> 2.0)
27
- sexp_processor (~> 3.0)
28
- roodi (2.1.0)
29
- ruby_parser
30
- rspec (2.3.0)
31
- rspec-core (~> 2.3.0)
32
- rspec-expectations (~> 2.3.0)
33
- rspec-mocks (~> 2.3.0)
34
- rspec-core (2.3.1)
35
- rspec-expectations (2.3.0)
23
+ rspec (2.8.0)
24
+ rspec-core (~> 2.8.0)
25
+ rspec-expectations (~> 2.8.0)
26
+ rspec-mocks (~> 2.8.0)
27
+ rspec-core (2.8.0)
28
+ rspec-expectations (2.8.0)
36
29
  diff-lcs (~> 1.1.2)
37
- rspec-mocks (2.3.0)
30
+ rspec-mocks (2.8.0)
38
31
  ruby-prof (0.10.8)
39
- ruby2ruby (1.3.1)
40
- ruby_parser (~> 2.0)
41
- sexp_processor (~> 3.0)
42
- ruby_parser (2.3.1)
43
- sexp_processor (~> 3.0)
44
- sexp_processor (3.0.10)
45
32
  thor (0.14.6)
46
33
 
47
34
  PLATFORMS
@@ -56,8 +43,5 @@ DEPENDENCIES
56
43
  guard-shell (~> 0.1.1)
57
44
  jeweler (~> 1.6.4)
58
45
  rb-fsevent (~> 0.4.3.1)
59
- rcov
60
- reek (~> 1.2.8)
61
- roodi (~> 2.1.0)
62
- rspec (~> 2.3.0)
46
+ rspec
63
47
  ruby-prof
data/Rakefile CHANGED
@@ -31,24 +31,6 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
31
31
  spec.pattern = FileList['spec/**/*_spec.rb']
32
32
  end
33
33
 
34
- RSpec::Core::RakeTask.new(:rcov) do |spec|
35
- spec.pattern = 'spec/**/*_spec.rb'
36
- spec.rcov = true
37
- end
38
-
39
- require 'reek/rake/task'
40
- Reek::Rake::Task.new do |t|
41
- t.fail_on_error = true
42
- t.verbose = false
43
- t.source_files = 'lib/**/*.rb'
44
- end
45
-
46
- require 'roodi'
47
- require 'roodi_task'
48
- RoodiTask.new do |t|
49
- t.verbose = false
50
- end
51
-
52
34
  task :default => :spec
53
35
 
54
36
  require 'rake/rdoctask'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.1
1
+ 0.8.0
data/aspector.gemspec CHANGED
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{aspector}
8
- s.version = "0.7.1"
7
+ s.name = "aspector"
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{2012-02-09}
13
- s.description = %q{}
14
- s.email = %q{gcao99@gmail.com}
12
+ s.date = "2012-02-14"
13
+ s.description = ""
14
+ s.email = "gcao99@gmail.com"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
17
  "README.rdoc"
@@ -43,7 +43,6 @@ Gem::Specification.new do |s|
43
43
  "lib/aspector/aspect_instances.rb",
44
44
  "lib/aspector/base.rb",
45
45
  "lib/aspector/base_class_methods.rb",
46
- "lib/aspector/context.rb",
47
46
  "lib/aspector/deferred_logic.rb",
48
47
  "lib/aspector/deferred_option.rb",
49
48
  "lib/aspector/method_matcher.rb",
@@ -72,21 +71,18 @@ Gem::Specification.new do |s|
72
71
  "spec/unit/deferred_logic_spec.rb",
73
72
  "spec/unit/method_matcher_spec.rb"
74
73
  ]
75
- s.homepage = %q{http://github.com/gcao/aspector}
74
+ s.homepage = "http://github.com/gcao/aspector"
76
75
  s.licenses = ["MIT"]
77
76
  s.require_paths = ["lib"]
78
- s.rubygems_version = %q{1.4.2}
79
- s.summary = %q{Aspect Oriented Ruby Programming}
77
+ s.rubygems_version = "1.8.15"
78
+ s.summary = "Aspect Oriented Ruby Programming"
80
79
 
81
80
  if s.respond_to? :specification_version then
82
81
  s.specification_version = 3
83
82
 
84
83
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
85
- s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
84
+ s.add_development_dependency(%q<rspec>, [">= 0"])
86
85
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
87
- s.add_development_dependency(%q<rcov>, [">= 0"])
88
- s.add_development_dependency(%q<reek>, ["~> 1.2.8"])
89
- s.add_development_dependency(%q<roodi>, ["~> 2.1.0"])
90
86
  s.add_development_dependency(%q<ruby-prof>, [">= 0"])
91
87
  s.add_development_dependency(%q<guard>, ["~> 0.8.4"])
92
88
  s.add_development_dependency(%q<guard-bundler>, ["~> 0.1.3"])
@@ -96,11 +92,8 @@ Gem::Specification.new do |s|
96
92
  s.add_development_dependency(%q<growl>, ["~> 1.0.3"])
97
93
  s.add_development_dependency(%q<awesome_print>, [">= 0"])
98
94
  else
99
- s.add_dependency(%q<rspec>, ["~> 2.3.0"])
95
+ s.add_dependency(%q<rspec>, [">= 0"])
100
96
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
101
- s.add_dependency(%q<rcov>, [">= 0"])
102
- s.add_dependency(%q<reek>, ["~> 1.2.8"])
103
- s.add_dependency(%q<roodi>, ["~> 2.1.0"])
104
97
  s.add_dependency(%q<ruby-prof>, [">= 0"])
105
98
  s.add_dependency(%q<guard>, ["~> 0.8.4"])
106
99
  s.add_dependency(%q<guard-bundler>, ["~> 0.1.3"])
@@ -111,11 +104,8 @@ Gem::Specification.new do |s|
111
104
  s.add_dependency(%q<awesome_print>, [">= 0"])
112
105
  end
113
106
  else
114
- s.add_dependency(%q<rspec>, ["~> 2.3.0"])
107
+ s.add_dependency(%q<rspec>, [">= 0"])
115
108
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
116
- s.add_dependency(%q<rcov>, [">= 0"])
117
- s.add_dependency(%q<reek>, ["~> 1.2.8"])
118
- s.add_dependency(%q<roodi>, ["~> 2.1.0"])
119
109
  s.add_dependency(%q<ruby-prof>, [">= 0"])
120
110
  s.add_dependency(%q<guard>, ["~> 0.8.4"])
121
111
  s.add_dependency(%q<guard-bundler>, ["~> 0.1.3"])
@@ -46,12 +46,15 @@ require 'aspector'
46
46
  class CacheAspect < Aspector::Base
47
47
  default :ttl => 60
48
48
 
49
- around options[:method], :context_arg => true do |context, &block|
50
- key = context.method_name
51
- ttl = context.options[:ttl]
52
-
53
- SimpleCache.cache key, ttl do
54
- block.call
49
+ def after_initialize
50
+ aspect = self
51
+ around options[:method], :method_name_arg => true do |method, &block|
52
+ key = method
53
+ ttl = aspect.options[:ttl]
54
+
55
+ SimpleCache.cache key, ttl do
56
+ block.call
57
+ end
55
58
  end
56
59
  end
57
60
 
@@ -17,7 +17,7 @@ module Aspector
17
17
  end
18
18
 
19
19
  def with_method
20
- @with_method ||= "aspect_#{@parent.hash}_#{@parent.advices.index(self)}"
20
+ @with_method ||= "aop_#{hash.abs}"
21
21
  end
22
22
 
23
23
  def match? method, context = nil
@@ -3,7 +3,7 @@ module Aspector
3
3
 
4
4
  def apply_to_method method
5
5
  each do |aspect_instance|
6
- aspect_instance.apply_to_method(method)
6
+ aspect_instance.send :aop_apply_to_method, method
7
7
  end
8
8
  end
9
9
 
data/lib/aspector/base.rb CHANGED
@@ -3,152 +3,207 @@ require 'erb'
3
3
  module Aspector
4
4
  class Base
5
5
 
6
- attr :options
6
+ attr :aop_options
7
+ alias :options :aop_options
7
8
 
8
9
  def initialize target, options = {}
9
- @target = target
10
+ @aop_target = target
10
11
 
11
- default_options = self.class.default_options
12
+ default_options = self.class.aop_default_options
12
13
  if default_options and not default_options.empty?
13
- @options = default_options.merge(options)
14
+ @aop_options = default_options.merge(options)
14
15
  else
15
- @options = options
16
+ @aop_options = options
16
17
  end
17
18
 
18
- # @context is where advices will be applied (i.e. where methods are modified), can be different from target
19
- @context = get_context
19
+ # @aop_context is where advices will be applied (i.e. where methods are modified), can be different from target
20
+ @aop_context = aop_get_context
20
21
 
21
22
  after_initialize
22
23
  end
23
24
 
24
- def apply
25
- invoke_deferred_logics
26
- define_methods_for_advice_blocks
27
- add_to_instances
28
- apply_to_methods
29
- add_method_hooks
25
+ def aop_advices
26
+ shared_advices = self.class.aop_advices
27
+
28
+ if shared_advices and shared_advices.size > 0
29
+ if @aop_advices
30
+ @aop_advices + shared_advices
31
+ else
32
+ shared_advices
33
+ end
34
+ else
35
+ @aop_advices or []
36
+ end
37
+ end
38
+ alias :advices :aop_advices
39
+
40
+ def aop_apply
41
+ before_apply
42
+ aop_invoke_deferred_logics
43
+ aop_define_methods_for_advice_blocks
44
+ aop_add_to_instances
45
+ aop_apply_to_methods
30
46
  after_apply
31
47
  end
48
+ alias :apply :aop_apply
49
+
50
+ protected
51
+
52
+ # Hook method that runs after an aspect is instantiated
53
+ def after_initialize
54
+ end
55
+
56
+ # Hook method that runs before an aspect is applied
57
+ def before_apply
58
+ end
59
+
60
+ # Hook method that runs after an aspect is applied
61
+ def after_apply
62
+ end
63
+
64
+ def before_apply_to_method method, advices
65
+ end
66
+
67
+ def after_apply_to_method method, advices
68
+ end
69
+
70
+ def aop_before *methods, &block
71
+ @aop_advices ||= []
72
+ @aop_advices << self.class.send(:aop_create_advice, Aspector::AdviceMetadata::BEFORE, self, methods, &block)
73
+ end
74
+ alias :before :aop_before
75
+
76
+ def aop_before_filter *methods, &block
77
+ @aop_advices ||= []
78
+ @aop_advices << self.class.send(:aop_create_advice, Aspector::AdviceMetadata::BEFORE_FILTER, self, methods, &block)
79
+ end
80
+ alias :before_filter :aop_before_filter
81
+
82
+ def aop_after *methods, &block
83
+ @aop_advices ||= []
84
+ @aop_advices << self.class.send(:aop_create_advice, Aspector::AdviceMetadata::AFTER, self, methods, &block)
85
+ end
86
+ alias :after :aop_after
87
+
88
+ def aop_around *methods, &block
89
+ @aop_advices ||= []
90
+ @aop_advices << self.class.send(:aop_create_advice, Aspector::AdviceMetadata::AROUND, self, methods, &block)
91
+ end
92
+ alias :around :aop_around
32
93
 
33
- def deferred_logic_results logic
34
- @deferred_logic_results[logic]
94
+ private
95
+
96
+ def aop_deferred_logic_results logic
97
+ @aop_deferred_logic_results[logic]
35
98
  end
36
99
 
37
- def apply_to_methods
38
- @context.public_instance_methods.each do |method|
39
- apply_to_method(method, :public)
100
+ def aop_apply_to_methods
101
+ @aop_context.public_instance_methods.each do |method|
102
+ aop_apply_to_method(method.to_s, :public)
40
103
  end
41
104
 
42
- @context.protected_instance_methods.each do |method|
43
- apply_to_method(method, :protected)
105
+ @aop_context.protected_instance_methods.each do |method|
106
+ aop_apply_to_method(method.to_s, :protected)
44
107
  end
45
108
 
46
- if @options[:private_methods]
47
- @context.private_instance_methods.each do |method|
48
- apply_to_method(method, :private)
109
+ if @aop_options[:private_methods]
110
+ @aop_context.private_instance_methods.each do |method|
111
+ aop_apply_to_method(method.to_s, :private)
49
112
  end
50
113
  end
51
114
  end
52
115
 
53
- def apply_to_method method, scope = nil
54
- advices = advices_for_method method
116
+ def aop_apply_to_method method, scope = nil
117
+ advices = aop_advices_for_method method
55
118
  return if advices.empty?
56
119
 
57
- scope ||=
58
- if @context.private_instance_methods.include?(method.to_s)
59
- :private
60
- elsif @context.protected_instance_methods.include?(method.to_s)
61
- :protected
62
- else
63
- :public
64
- end
65
-
66
- recreate_method method, advices, scope
67
- end
120
+ before_apply_to_method method, advices
68
121
 
69
- protected
122
+ scope ||=
123
+ if @aop_context.private_instance_methods.include?(RUBY_VERSION.index('1.9') ? method.to_sym : method.to_s)
124
+ :private
125
+ elsif @aop_context.protected_instance_methods.include?(RUBY_VERSION.index('1.9') ? method.to_sym : method.to_s)
126
+ :protected
127
+ else
128
+ :public
129
+ end
70
130
 
71
- # Hook method that runs after an aspect is instantiated
72
- def after_initialize
73
- end
131
+ aop_recreate_method method, advices, scope
74
132
 
75
- # Hook method that runs after an aspect is applied
76
- def after_apply
133
+ after_apply_to_method method, advices
77
134
  end
78
135
 
79
- private
80
-
81
- def get_context
82
- return @target if @target.is_a?(Module) and not @options[:eigen_class]
136
+ def aop_get_context
137
+ return @aop_target if @aop_target.is_a?(Module) and not @aop_options[:eigen_class]
83
138
 
84
- class << @target
139
+ class << @aop_target
85
140
  self
86
141
  end
87
142
  end
88
143
 
89
- def invoke_deferred_logics
90
- return unless self.class.deferred_logics
144
+ def aop_invoke_deferred_logics
145
+ return unless (logics = self.class.send :aop_deferred_logics)
91
146
 
92
- @deferred_logic_results ||= {}
93
- self.class.deferred_logics.each do |logic|
94
- @deferred_logic_results[logic] = logic.apply @context, self
147
+ @aop_deferred_logic_results ||= {}
148
+ logics.each do |logic|
149
+ @aop_deferred_logic_results[logic] = logic.apply @aop_context, self
95
150
  end
96
151
  end
97
152
 
98
- def define_methods_for_advice_blocks
99
- self.class.advices.each do |advice|
153
+ def aop_define_methods_for_advice_blocks
154
+ aop_advices.each do |advice|
100
155
  next unless advice.advice_block
101
- @context.send :define_method, advice.with_method, advice.advice_block
102
- @context.send :private, advice.with_method
156
+ @aop_context.send :define_method, advice.with_method, advice.advice_block
157
+ @aop_context.send :private, advice.with_method
103
158
  end
104
159
  end
105
160
 
106
- def add_to_instances
107
- aspect_instances = @context.instance_variable_get(:@aspector_instances)
161
+ def aop_add_to_instances
162
+ aspect_instances = @aop_context.instance_variable_get(:@aop_instances)
108
163
  unless aspect_instances
109
164
  aspect_instances = AspectInstances.new
110
- @context.instance_variable_set(:@aspector_instances, aspect_instances)
165
+ @aop_context.instance_variable_set(:@aop_instances, aspect_instances)
111
166
  end
112
167
  aspect_instances << self
113
168
  end
114
169
 
115
- def add_method_hooks
116
- if @options[:eigen_class]
117
- return unless @target.is_a?(Module)
170
+ def aop_add_method_hooks
171
+ if @aop_options[:eigen_class]
172
+ return unless @aop_target.is_a?(Module)
118
173
 
119
- eigen_class = class << @target; self; end
120
- orig_singleton_method_added = @target.method(:singleton_method_added)
174
+ eigen_class = class << @aop_target; self; end
175
+ orig_singleton_method_added = @aop_target.method(:singleton_method_added)
121
176
 
122
177
  eigen_class.send :define_method, :singleton_method_added do |method|
123
- singleton_method_added_aspector(method) do
178
+ aop_singleton_method_added(method) do
124
179
  orig_singleton_method_added.call(method)
125
180
  end
126
181
  end
127
182
  else
128
- eigen_class = class << @target; self; end
183
+ eigen_class = class << @aop_target; self; end
129
184
 
130
- if @target.is_a? Module
131
- orig_method_added = @target.method(:method_added)
185
+ if @aop_target.is_a? Module
186
+ orig_method_added = @aop_target.method(:method_added)
132
187
  else
133
188
  orig_method_added = eigen_class.method(:method_added)
134
189
  end
135
190
 
136
191
  eigen_class.send :define_method, :method_added do |method|
137
- method_added_aspector(method) do
192
+ aop_method_added(method) do
138
193
  orig_method_added.call(method)
139
194
  end
140
195
  end
141
196
  end
142
197
  end
143
198
 
144
- def advices_for_method method
145
- self.class.advices.select do |advice|
199
+ def aop_advices_for_method method
200
+ aop_advices.select do |advice|
146
201
  advice.match?(method, self)
147
202
  end
148
203
  end
149
204
 
150
- def recreate_method method, advices, scope
151
- @context.instance_variable_set(:@aspector_creating_method, true)
205
+ def aop_recreate_method method, advices, scope
206
+ @aop_context.instance_variable_set(:@aop_creating_method, true)
152
207
 
153
208
  before_advices = advices.select {|advice| advice.before? }
154
209
  after_advices = advices.select {|advice| advice.after? }
@@ -157,21 +212,21 @@ module Aspector
157
212
  if around_advices.size > 1
158
213
  (around_advices.size - 1).downto(1) do |i|
159
214
  advice = around_advices[i]
160
- recreate_method_with_advices method, [], [], advice
215
+ aop_recreate_method_with_advices method, [], [], advice
161
216
  end
162
217
  end
163
218
 
164
- recreate_method_with_advices method, before_advices, after_advices, around_advices.first
219
+ aop_recreate_method_with_advices method, before_advices, after_advices, around_advices.first
165
220
 
166
- @context.send scope, method if scope != :public
221
+ @aop_context.send scope, method if scope != :public
167
222
  ensure
168
- @context.instance_variable_set(:@aspector_creating_method, nil)
223
+ @aop_context.instance_variable_set(:@aop_creating_method, nil)
169
224
  end
170
225
 
171
- def recreate_method_with_advices method, before_advices, after_advices, around_advice
226
+ def aop_recreate_method_with_advices method, before_advices, after_advices, around_advice
172
227
  code = METHOD_TEMPLATE.result(binding)
173
228
  #puts code
174
- @context.class_eval code, __FILE__, __LINE__ + 4
229
+ @aop_context.class_eval code, __FILE__, __LINE__ + 4
175
230
  end
176
231
 
177
232
  METHOD_TEMPLATE = ERB.new <<-CODE
@@ -181,12 +236,7 @@ module Aspector
181
236
  define_method :<%= method %> do |*args, &block|
182
237
  # Before advices
183
238
  <% before_advices.each do |advice|
184
- if advice.options[:context_arg]
185
- %>
186
- context = Aspector::Context.new(target, <%= self.hash %>, <%= advice.hash %>)
187
- context.method_name = '<%= method %>'
188
- result = <%= advice.with_method %> context, *args
189
- <% elsif advice.options[:method_name_arg] %>
239
+ if advice.options[:method_name_arg] %>
190
240
  result = <%= advice.with_method %> '<%= method %>', *args
191
241
  <% else %>
192
242
  result = <%= advice.with_method %> *args
@@ -200,14 +250,7 @@ module Aspector
200
250
  %>
201
251
 
202
252
  <% if around_advice
203
- if around_advice.options[:context_arg]
204
- %>
205
- context = Aspector::Context.new(target, <%= self.hash %>, <%= around_advice.hash %>)
206
- context.method_name = '<%= method %>'
207
- result = <%= around_advice.with_method %> context, *args do |*args|
208
- wrapped_method.bind(self).call *args, &block
209
- end
210
- <% elsif around_advice.options[:method_name_arg] %>
253
+ if around_advice.options[:method_name_arg] %>
211
254
  result = <%= around_advice.with_method %> '<%= method %>', *args do |*args|
212
255
  wrapped_method.bind(self).call *args, &block
213
256
  end
@@ -225,30 +268,19 @@ module Aspector
225
268
  # After advices
226
269
  <% unless after_advices.empty?
227
270
  after_advices.each do |advice|
228
- if advice.options[:context_arg]
229
- if advice.options[:result_arg]
230
- %>
231
- context = Aspector::Context.new(target, <%= self.hash %>, <%= advice.hash %>)
232
- context.method_name = '<%= method %>'
233
- result = <%= advice.with_method %> context, result, *args
234
- <% else %>
235
- <%= advice.with_method %> context, *args
236
- <% end
237
- elsif advice.options[:method_name_arg]
271
+ if advice.options[:method_name_arg]
238
272
  if advice.options[:result_arg]
239
273
  %>
240
274
  result = <%= advice.with_method %> '<%= method %>', result, *args
241
275
  <% else %>
242
276
  <%= advice.with_method %> '<%= method %>', *args
243
277
  <% end
244
- else
245
- if advice.options[:result_arg]
278
+ elsif advice.options[:result_arg]
246
279
  %>
247
280
  result = <%= advice.with_method %> result, *args
248
- <% else %>
281
+ <% else %>
249
282
  <%= advice.with_method %> *args
250
- <% end
251
- end
283
+ <% end
252
284
  end
253
285
  %>
254
286
  result