aspector 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -14,4 +14,5 @@ group :development do
14
14
  gem 'growl', "~> 1.0.3"
15
15
 
16
16
  gem 'awesome_print'
17
+ gem 'pry'
17
18
  end
data/Gemfile.lock CHANGED
@@ -2,6 +2,7 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
4
  awesome_print (1.0.2)
5
+ coderay (1.0.5)
5
6
  diff-lcs (1.1.3)
6
7
  git (1.2.5)
7
8
  growl (1.0.3)
@@ -18,6 +19,11 @@ GEM
18
19
  bundler (~> 1.0)
19
20
  git (>= 1.2.5)
20
21
  rake
22
+ method_source (0.7.1)
23
+ pry (0.9.8.4)
24
+ coderay (~> 1.0.5)
25
+ method_source (~> 0.7.1)
26
+ slop (>= 2.4.4, < 3)
21
27
  rake (0.9.2.2)
22
28
  rb-fsevent (0.4.3.1)
23
29
  rspec (2.8.0)
@@ -29,6 +35,7 @@ GEM
29
35
  diff-lcs (~> 1.1.2)
30
36
  rspec-mocks (2.8.0)
31
37
  ruby-prof (0.10.8)
38
+ slop (2.4.4)
32
39
  thor (0.14.6)
33
40
 
34
41
  PLATFORMS
@@ -42,6 +49,7 @@ DEPENDENCIES
42
49
  guard-rspec (~> 0.5.2)
43
50
  guard-shell (~> 0.1.1)
44
51
  jeweler (~> 1.6.4)
52
+ pry
45
53
  rb-fsevent (~> 0.4.3.1)
46
54
  rspec
47
55
  ruby-prof
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.0
1
+ 0.9.1
data/aspector.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "aspector"
8
- s.version = "0.9.0"
8
+ s.version = "0.9.1"
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 = "2012-03-08"
12
+ s.date = "2012-03-13"
13
13
  s.description = ""
14
14
  s.email = "gcao99@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -91,6 +91,7 @@ Gem::Specification.new do |s|
91
91
  s.add_development_dependency(%q<rb-fsevent>, ["~> 0.4.3.1"])
92
92
  s.add_development_dependency(%q<growl>, ["~> 1.0.3"])
93
93
  s.add_development_dependency(%q<awesome_print>, [">= 0"])
94
+ s.add_development_dependency(%q<pry>, [">= 0"])
94
95
  else
95
96
  s.add_dependency(%q<rspec>, [">= 0"])
96
97
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
@@ -102,6 +103,7 @@ Gem::Specification.new do |s|
102
103
  s.add_dependency(%q<rb-fsevent>, ["~> 0.4.3.1"])
103
104
  s.add_dependency(%q<growl>, ["~> 1.0.3"])
104
105
  s.add_dependency(%q<awesome_print>, [">= 0"])
106
+ s.add_dependency(%q<pry>, [">= 0"])
105
107
  end
106
108
  else
107
109
  s.add_dependency(%q<rspec>, [">= 0"])
@@ -114,6 +116,7 @@ Gem::Specification.new do |s|
114
116
  s.add_dependency(%q<rb-fsevent>, ["~> 0.4.3.1"])
115
117
  s.add_dependency(%q<growl>, ["~> 1.0.3"])
116
118
  s.add_dependency(%q<awesome_print>, [">= 0"])
119
+ s.add_dependency(%q<pry>, [">= 0"])
117
120
  end
118
121
  end
119
122
 
@@ -3,6 +3,7 @@ module Aspector
3
3
 
4
4
  def apply_to_method method
5
5
  each do |aspect_instance|
6
+ next if aspect_instance.aop_options[:old_methods_only]
6
7
  aspect_instance.aop_apply_to_method method, aspect_instance.aop_advices
7
8
  end
8
9
  end
data/lib/aspector/base.rb CHANGED
@@ -26,6 +26,34 @@ module Aspector
26
26
  after_initialize
27
27
  end
28
28
 
29
+ def aop_enable
30
+ class << self
31
+ def aop_disabled?; end
32
+ end
33
+ end
34
+ alias enable aop_enable
35
+
36
+ def aop_disable
37
+ class << self
38
+ def aop_disabled?; true; end
39
+ end
40
+ end
41
+ alias disable aop_disable
42
+
43
+ def aop_reset_disabled
44
+ class << self
45
+ remove_method :aop_disabled?
46
+ end
47
+ end
48
+ alias reset_disabled :aop_reset_disabled
49
+
50
+ def aop_disabled?
51
+ end
52
+
53
+ def disabled?
54
+ aop_disabled
55
+ end
56
+
29
57
  def aop_advices
30
58
  shared_advices = self.class.aop_advices
31
59
 
@@ -45,14 +73,30 @@ module Aspector
45
73
  before_apply
46
74
  aop_invoke_deferred_logics
47
75
  aop_define_methods_for_advice_blocks
48
- aop_add_to_instances
49
- aop_apply_to_methods
76
+ aop_add_to_instances unless @aop_options[:old_methods_only]
77
+ aop_apply_to_methods unless @aop_options[:new_methods_only]
78
+ aop_add_method_hooks unless @aop_options[:old_methods_only]
50
79
  after_apply
51
80
  end
52
81
  alias :apply :aop_apply
53
82
 
54
83
  def aop_apply_to_methods
55
84
  advices = aop_advices
85
+
86
+ # If method/methods option is set and all are String or Symbol, apply to those only, instead of
87
+ # iterating through all methods
88
+ methods = [@aop_options[:method] || @aop_options[:methods]]
89
+ methods.compact!
90
+ methods.flatten!
91
+
92
+ if not methods.empty? and methods.all?{|method| method.is_a? String or method.is_a? Symbol }
93
+ methods.each do |method|
94
+ aop_apply_to_method(method.to_s, advices)
95
+ end
96
+
97
+ return
98
+ end
99
+
56
100
  @aop_context.public_instance_methods.each do |method|
57
101
  aop_apply_to_method(method.to_s, advices, :public)
58
102
  end
@@ -243,10 +287,10 @@ module Aspector
243
287
  <% end %>
244
288
 
245
289
  define_method :<%= method %> do |*args, &block|
246
- return orig_method.bind(self).call(*args, &block) if aspect.class.aop_disabled?
290
+ return orig_method.bind(self).call(*args, &block) if aspect.aop_disabled?
247
291
 
248
292
  <% if is_outermost %>
249
- catch(:aop_return) do
293
+ catch(:aop_returns) do
250
294
  <% end %>
251
295
 
252
296
  # Before advices
@@ -4,20 +4,18 @@ module Aspector
4
4
  ::Aspector::Base.extend(self)
5
5
 
6
6
  def aop_enable
7
- @aop_disabled = nil
7
+ def aop_disabled?
8
+ end
8
9
  end
9
10
  alias :enable :aop_enable
10
11
 
11
12
  def aop_disable
12
- @aop_disabled = true
13
+ def aop_disabled?
14
+ true
15
+ end
13
16
  end
14
17
  alias :disable :aop_disable
15
18
 
16
- def aop_disabled?
17
- @aop_disabled
18
- end
19
- alias :disabled? :aop_disabled?
20
-
21
19
  def aop_advices
22
20
  @aop_advices ||= []
23
21
  end
@@ -31,7 +29,6 @@ module Aspector
31
29
  def aop_apply target, options = {}
32
30
  aspect_instance = new(target, options)
33
31
  aspect_instance.send :aop_apply
34
- aspect_instance.send :aop_add_method_hooks
35
32
  aspect_instance
36
33
  end
37
34
  alias :apply :aop_apply
@@ -66,6 +63,8 @@ module Aspector
66
63
  alias :around :aop_around
67
64
 
68
65
  def aop_target code = nil, &block
66
+ return unless code or block_given?
67
+
69
68
  logic = DeferredLogic.new(code || block)
70
69
  aop_deferred_logics << logic
71
70
  logic
@@ -21,10 +21,10 @@ module Aspector
21
21
  klass
22
22
  end
23
23
 
24
- def aop_return value = nil
25
- throw :aop_return, value
24
+ def aop_returns value = nil
25
+ throw :aop_returns, value
26
26
  end
27
- alias :returns :aop_return
27
+ alias :returns :aop_returns
28
28
 
29
29
  end
30
30
  end
@@ -95,4 +95,74 @@ describe "Aspector" do
95
95
  obj.value.should == %w"before_test before_test test"
96
96
  end
97
97
 
98
+ it "if new_methods_only is true, do not apply to existing methods" do
99
+ aspect = Aspector do
100
+ before(:test) { value << 'before_test' }
101
+ end
102
+
103
+ klass = create_test_class
104
+ aspect.apply(klass, :new_methods_only => true)
105
+
106
+ obj = klass.new
107
+ obj.test
108
+ obj.value.should == %w"test"
109
+ end
110
+
111
+ it "if new_methods_only is true, do apply to new methods" do
112
+ aspect = Aspector do
113
+ before(:test) { value << 'before_test' }
114
+ end
115
+
116
+ klass = Class.new do
117
+ def value
118
+ @value ||= []
119
+ end
120
+ end
121
+
122
+ aspect.apply(klass, :new_methods_only => true)
123
+
124
+ klass.send :define_method, :test do
125
+ value << "test"
126
+ end
127
+
128
+ obj = klass.new
129
+ obj.test
130
+ obj.value.should == %w"before_test test"
131
+ end
132
+
133
+ it "if old_methods_only is true, do apply to methods already defined" do
134
+ aspect = Aspector do
135
+ before(:test) { value << 'before_test' }
136
+ end
137
+
138
+ klass = create_test_class
139
+ aspect.apply(klass, :old_methods_only => true)
140
+
141
+ obj = klass.new
142
+ obj.test
143
+ obj.value.should == %w"before_test test"
144
+ end
145
+
146
+ it "if old_methods_only is true, do not apply to new methods" do
147
+ aspect = Aspector do
148
+ before(:test) { value << 'before_test' }
149
+ end
150
+
151
+ klass = Class.new do
152
+ def value
153
+ @value ||= []
154
+ end
155
+ end
156
+
157
+ aspect.apply(klass, :old_methods_only => true)
158
+
159
+ klass.send :define_method, :test do
160
+ value << "test"
161
+ end
162
+
163
+ obj = klass.new
164
+ obj.test
165
+ obj.value.should == %w"test"
166
+ end
167
+
98
168
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aspector
3
3
  version: !ruby/object:Gem::Version
4
- hash: 59
4
+ hash: 57
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 0
10
- version: 0.9.0
9
+ - 1
10
+ version: 0.9.1
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: 2012-03-08 00:00:00 Z
18
+ date: 2012-03-13 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  requirement: &id001 !ruby/object:Gem::Requirement
@@ -172,6 +172,20 @@ dependencies:
172
172
  name: awesome_print
173
173
  prerelease: false
174
174
  type: :development
175
+ - !ruby/object:Gem::Dependency
176
+ requirement: &id011 !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ hash: 3
182
+ segments:
183
+ - 0
184
+ version: "0"
185
+ version_requirements: *id011
186
+ name: pry
187
+ prerelease: false
188
+ type: :development
175
189
  description: ""
176
190
  email: gcao99@gmail.com
177
191
  executables: []