Skip to content

Commit

Permalink
Add pry to development requirement, support new_methods_only and old_…
Browse files Browse the repository at this point in the history
…methods_only
  • Loading branch information
gcao committed Mar 13, 2012
1 parent 31cc0e9 commit 90d8152
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 7 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ group :development do
gem 'growl', "~> 1.0.3"

gem 'awesome_print'
gem 'pry'
end
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ GEM
remote: http://rubygems.org/
specs:
awesome_print (1.0.2)
coderay (1.0.5)
diff-lcs (1.1.3)
git (1.2.5)
growl (1.0.3)
Expand All @@ -18,6 +19,11 @@ GEM
bundler (~> 1.0)
git (>= 1.2.5)
rake
method_source (0.7.1)
pry (0.9.8.4)
coderay (~> 1.0.5)
method_source (~> 0.7.1)
slop (>= 2.4.4, < 3)
rake (0.9.2.2)
rb-fsevent (0.4.3.1)
rspec (2.8.0)
Expand All @@ -29,6 +35,7 @@ GEM
diff-lcs (~> 1.1.2)
rspec-mocks (2.8.0)
ruby-prof (0.10.8)
slop (2.4.4)
thor (0.14.6)

PLATFORMS
Expand All @@ -42,6 +49,7 @@ DEPENDENCIES
guard-rspec (~> 0.5.2)
guard-shell (~> 0.1.1)
jeweler (~> 1.6.4)
pry
rb-fsevent (~> 0.4.3.1)
rspec
ruby-prof
1 change: 1 addition & 0 deletions lib/aspector/aspect_instances.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class AspectInstances < Array

def apply_to_method method
each do |aspect_instance|
next if aspect_instance.aop_options[:old_methods_only]
aspect_instance.aop_apply_to_method method, aspect_instance.aop_advices
end
end
Expand Down
32 changes: 29 additions & 3 deletions lib/aspector/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,34 @@ def initialize target, options = {}
after_initialize
end

def aop_enable
class << self
def aop_disabled?; end
end
end
alias enable aop_enable

def aop_disable
class << self
def aop_disabled?; true; end
end
end
alias disable aop_disable

def aop_reset_disabled
class << self
remove_method :aop_disabled?
end
end
alias reset_disabled :aop_reset_disabled

def aop_disabled?
end

def disabled?
aop_disabled
end

def aop_advices
shared_advices = self.class.aop_advices

Expand All @@ -48,8 +73,9 @@ def aop_apply
before_apply
aop_invoke_deferred_logics
aop_define_methods_for_advice_blocks
aop_add_to_instances
aop_apply_to_methods
aop_add_to_instances unless @aop_options[:old_methods_only]
aop_apply_to_methods unless @aop_options[:new_methods_only]
aop_add_method_hooks unless @aop_options[:old_methods_only]
after_apply
end
alias :apply :aop_apply
Expand Down Expand Up @@ -249,7 +275,7 @@ def aop_recreate_method_with_advices method, before_advices, after_advices, arou
return orig_method.bind(self).call(*args, &block) if aspect.aop_disabled?
<% if is_outermost %>
catch(:aop_return) do
catch(:aop_returns) do
<% end %>
# Before advices
Expand Down
3 changes: 2 additions & 1 deletion lib/aspector/base_class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def aop_default_options
def aop_apply target, options = {}
aspect_instance = new(target, options)
aspect_instance.send :aop_apply
aspect_instance.send :aop_add_method_hooks
aspect_instance
end
alias :apply :aop_apply
Expand Down Expand Up @@ -64,6 +63,8 @@ def aop_around *methods, &block
alias :around :aop_around

def aop_target code = nil, &block
return unless code or block_given?

logic = DeferredLogic.new(code || block)
aop_deferred_logics << logic
logic
Expand Down
6 changes: 3 additions & 3 deletions lib/aspector/object_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def Aspector options = {}, &block
klass
end

def aop_return value = nil
throw :aop_return, value
def aop_returns value = nil
throw :aop_returns, value
end
alias :returns :aop_return
alias :returns :aop_returns

end
end
Expand Down
70 changes: 70 additions & 0 deletions spec/functional/aspector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,74 @@ class TestAspect < Aspector::Base
obj.value.should == %w"before_test before_test test"
end

it "if new_methods_only is true, do not apply to existing methods" do
aspect = Aspector do
before(:test) { value << 'before_test' }
end

klass = create_test_class
aspect.apply(klass, :new_methods_only => true)

obj = klass.new
obj.test
obj.value.should == %w"test"
end

it "if new_methods_only is true, do apply to new methods" do
aspect = Aspector do
before(:test) { value << 'before_test' }
end

klass = Class.new do
def value
@value ||= []
end
end

aspect.apply(klass, :new_methods_only => true)

klass.send :define_method, :test do
value << "test"
end

obj = klass.new
obj.test
obj.value.should == %w"before_test test"
end

it "if old_methods_only is true, do apply to methods already defined" do
aspect = Aspector do
before(:test) { value << 'before_test' }
end

klass = create_test_class
aspect.apply(klass, :old_methods_only => true)

obj = klass.new
obj.test
obj.value.should == %w"before_test test"
end

it "if old_methods_only is true, do not apply to new methods" do
aspect = Aspector do
before(:test) { value << 'before_test' }
end

klass = Class.new do
def value
@value ||= []
end
end

aspect.apply(klass, :old_methods_only => true)

klass.send :define_method, :test do
value << "test"
end

obj = klass.new
obj.test
obj.value.should == %w"test"
end

end

0 comments on commit 90d8152

Please sign in to comment.