Permalink
Browse files

Add rvm and bundler config, don't apply advice on advice method itsel…

…f, :new_methods_only option etc
  • Loading branch information...
1 parent d4a1bb1 commit 59f56f84139da121a7addf9bdf0755e69045f54c @gcao committed Nov 3, 2011
View
@@ -19,5 +19,5 @@ rdoc
pkg
## PROJECT::SPECIFIC
-test/output
-test/experiment*.rb
+performance-tests/output
+performance-tests/experiment*.rb
View
@@ -0,0 +1,12 @@
+rvm_gemset_create_on_use_flag=1
+
+if [ "${RUBY_PLATFORM}" == "1.8.7" ] ; then
+ rvm use 1.8.7@aspect4r
+elif [ "${RUBY_PLATFORM}" == "1.9.2" ] ; then
+ rvm use 1.9.2@aspect4r
+elif [ "${RUBY_PLATFORM}" == "jruby" ] ; then
+ rvm use jruby@aspect4r
+else
+ rvm use 1.8.7@aspect4r
+fi
+
View
@@ -0,0 +1,4 @@
+source :rubygems
+
+gem 'rspec', "~>1.3.0"
+gem 'mocha'
View
@@ -0,0 +1,14 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ metaclass (0.0.1)
+ mocha (0.10.0)
+ metaclass (~> 0.0.1)
+ rspec (1.3.2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ mocha
+ rspec (~> 1.3.0)
View
@@ -16,7 +16,7 @@ def self.included(base)
eigen_class.extend(ClassMethods)
end
- module ClassMethods
+ module ClassMethods
def method_added method
super method
@@ -32,12 +32,12 @@ def method_added method
Aspect4r::Helper.create_method self, method
end
end
-
+
def singleton_method_added method
super method
return if Aspect4r::Helper.creating_method?
-
+
method = method.to_s
return if method[0..2] == "a4r"
@@ -54,10 +54,10 @@ def singleton_method_added method
def a4r_data
@a4r_data ||= Aspect4r::Model::AspectData.new(self)
end
-
+
def a4r_group &block
a4r_data.change_group
-
+
if block_given?
instance_eval &block
a4r_data.change_group
@@ -69,11 +69,11 @@ def a4r_disable_advices_temporarily *methods
method = method.to_s
advices = a4r_data.advices_for_method(method)
next if advices.empty?
-
+
send :alias_method, :"#{method}_with_advices", method
Aspect4r::Helper.define_method self, method, a4r_data.wrapped_methods[method]
end
-
+
yield
ensure
methods.each do |method|
@@ -87,4 +87,4 @@ def a4r_disable_advices_temporarily *methods
end
end
end
-end
+end
@@ -8,11 +8,11 @@ def self.find_available_method_name klass, method_name_prefix
return m unless klass.private_instance_methods(false).include?(RUBY_VERSION =~ /^1\.8/ ? m : m.to_sym)
end
end
-
+
def self.creating_method?
@creating_method
end
-
+
def self.define_method klass_or_module, *args, &block
@creating_method = true
klass_or_module.send :define_method, *args, &block
@@ -40,20 +40,20 @@ def self.process_advice meta_data, klass_or_module, *methods, &block
else
with_method = methods.pop
end
-
+
a4r_data = klass_or_module.a4r_data
- advice = Aspect4r::Model::Advice.new(meta_data.advice_type,
- Aspect4r::Model::MethodMatcher.new(*methods),
- with_method,
- a4r_data.group,
+ advice = Aspect4r::Model::Advice.new(meta_data.advice_type,
+ Aspect4r::Model::MethodMatcher.new(*methods),
+ with_method,
+ a4r_data.group,
options)
a4r_data << advice
methods.each do |method|
next unless method.is_a? String
-
+
wrapped_method = a4r_data.wrapped_methods[method]
-
+
if not wrapped_method and klass_or_module.instance_methods.include?(RUBY_VERSION =~ /^1\.8/ ? method : method.to_sym)
wrapped_method = klass_or_module.instance_method(method)
a4r_data.wrapped_methods[method] = wrapped_method
@@ -62,36 +62,36 @@ def self.process_advice meta_data, klass_or_module, *methods, &block
create_method klass_or_module, method if wrapped_method
end
end
-
+
# method - target method
def self.create_method klass, method
@creating_method = true
-
+
advices = klass.a4r_data.advices_for_method method
return if advices.empty?
-
+
grouped_advices = []
group = nil
inner_most = true
advices.each do |advice|
if ((group and group != advice.group) or advice.around?) and not grouped_advices.empty?
create_method_with_advices klass, method, grouped_advices, inner_most
-
+
grouped_advices = []
inner_most = false
end
-
+
grouped_advices << advice
group = advice.group
end
-
+
# create wrap method for before/after advices which are not wrapped inside around advice.
create_method_with_advices klass, method, grouped_advices, inner_most unless grouped_advices.empty?
ensure
@creating_method = nil
end
-
+
# method
# before_advices
# after_advices
@@ -147,19 +147,19 @@ def self.create_method klass, method
<%= definition.with_method %> *args
<% end %>
<% end %>
-
+
result
end
CODE
-
+
def self.create_method_with_advices klass, method, advices, inner_most
before_advices = advices.select {|advice| advice.before? or advice.before_filter? }
after_advices = advices.select {|advice| advice.after? }
around_advice = advices.first if advices.first.around?
-
+
code = METHOD_TEMPLATE.result(binding)
# puts code
klass.class_eval code, __FILE__, __LINE__
end
end
-end
+end
@@ -4,42 +4,53 @@ class Advice
BEFORE = 1
AFTER = 2
AROUND = 3
-
+
attr :group
attr_accessor :type, :method_matcher, :with_method, :options
-
+
def initialize type, method_matcher, with_method, group, options = {}
@type = type
@method_matcher = method_matcher
@with_method = with_method
@group = group
@options = options
end
-
+
def name
options[:name] || with_method
end
-
+
+ def match? method
+ return if method == with_method
+ return unless @method_matcher.match?(method)
+
+ return true unless @options[:except]
+
+ @except ||= MethodMatcher.new([@options[:except]].flatten)
+
+ not @except.match?(method)
+ end
+
def before?
type == BEFORE and not options[:skip_if_false]
end
-
+
def before_filter?
type == BEFORE and options[:skip_if_false]
end
-
+
def after?
type == AFTER
end
-
+
def around?
type == AROUND
end
-
+
def invoke obj, *args, &block
obj.send with_method, *args, &block
end
-
+
def to_s
s = "<" << @group << "> "
case @type
@@ -65,4 +76,4 @@ def to_s
end
end
end
-end
+end
@@ -8,7 +8,7 @@ def initialize advice_type, default_options = {}, mandatory_options = {}
@default_options = default_options || {}
@mandatory_options = mandatory_options || {}
end
-
+
def with_method_prefix
case advice_type
when Aspect4r::Model::Advice::BEFORE then "a4r_before_"
@@ -17,11 +17,11 @@ def with_method_prefix
else raise "Aspect4r internal error."
end
end
-
- BEFORE = new Aspect4r::Model::Advice::BEFORE, nil, :skip_if_false => false
- BEFORE_FILTER = new Aspect4r::Model::Advice::BEFORE, nil, :skip_if_false => true
- AFTER = new Aspect4r::Model::Advice::AFTER, :result_arg => true
- AROUND = new Aspect4r::Model::Advice::AROUND
+
+ BEFORE = new Aspect4r::Model::Advice::BEFORE, { :new_methods_only => true }, :skip_if_false => false
+ BEFORE_FILTER = new Aspect4r::Model::Advice::BEFORE, { :new_methods_only => true }, :skip_if_false => true
+ AFTER = new Aspect4r::Model::Advice::AFTER, { :new_methods_only => true, :result_arg => true }
+ AROUND = new Aspect4r::Model::Advice::AROUND, { :new_methods_only => true }
end
end
-end
+end
@@ -6,7 +6,7 @@ class AspectData < Array
def initialize klass_or_module
@group = klass_or_module.hash
end
-
+
def wrapped_methods
@wrapped_methods ||= {}
end
@@ -22,11 +22,11 @@ def group
def change_group
@group_index = group_index + 1
end
-
+
def advices_for_method method
method = method.to_s
- select {|advice| advice.method_matcher.match?(method) }
+ select {|advice| advice.match?(method) }
end
end
end
-end
+end
@@ -4,14 +4,14 @@ class MethodMatcher
def initialize *match_data
@match_data = match_data
- # Performance improvement ideas:
+ # Performance improvement ideas:
# if there is only one item in match_data, generate simplified match? method on the fly
# Seems this does not help much
#
# if match_data.size == 1
# first_item = match_data.first
# eigen_class = class << self; self; end
- #
+ #
# if first_item.is_a? String
# eigen_class.send :define_method, :match? do |method|
# method == first_item
@@ -34,10 +34,14 @@ def match? method
(item.is_a? Regexp and item =~ method)
end
end
-
+
+ def has_regular_expressions?
+ @has_regexps ||= @match_data.detect { |item| item.is_a? Regexp }
+ end
+
def to_s
@match_data.map {|item| item.inspect }.join ", "
end
end
end
-end
+end
Oops, something went wrong.

0 comments on commit 59f56f8

Please sign in to comment.