Skip to content
Browse files

alias_method_chain yields method target and punctuation to simplify w…

…rapper method definition. Used by the deprecate module method.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5113 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 3d3ba58 commit 643571ca25bc2fcc701e6def0975f56fe10a732f @jeremy jeremy committed
View
1 activesupport/lib/active_support/core_ext/module/aliasing.rb
@@ -24,6 +24,7 @@ def alias_method_chain(target, feature)
# Strip out punctuation on predicates or bang methods since
# e.g. target?_without_feature is not a valid method name.
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
+ yield(aliased_target, punctuation) if block_given?
alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target
alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}"
end
View
15 activesupport/lib/active_support/deprecation.rb
@@ -72,13 +72,14 @@ module ClassMethods
# Declare that a method has been deprecated.
def deprecate(*method_names)
method_names.each do |method_name|
- class_eval(<<-EOS, __FILE__, __LINE__)
- def #{method_name}_with_deprecation(*args, &block)
- ::ActiveSupport::Deprecation.warn("#{method_name} is deprecated and will be removed from Rails 2.0", caller)
- #{method_name}_without_deprecation(*args, &block)
- end
- EOS
- alias_method_chain(method_name, :deprecation)
+ alias_method_chain(method_name, :deprecation) do |target, punctuation|
+ class_eval(<<-EOS, __FILE__, __LINE__)
+ def #{target}_with_deprecation#{punctuation}(*args, &block)
+ ::ActiveSupport::Deprecation.warn("#{method_name} is deprecated and will be removed from Rails 2.0", caller)
+ #{target}_without_deprecation#{punctuation}(*args, &block)
+ end
+ EOS
+ end
end
end
end
View
15 activesupport/test/core_ext/module_test.rb
@@ -100,9 +100,12 @@ def test_as_load_path
module BarMethodAliaser
def self.included(foo_class)
+ foo_class.send :include, BarMethods
foo_class.alias_method_chain :bar, :baz
end
+end
+module BarMethods
def bar_with_baz
bar_without_baz << '_with_baz'
end
@@ -203,4 +206,16 @@ def test_alias_method_chain_with_feature_punctuation
FooClassWithBarMethod.alias_method_chain :quux?, :baz!
end
end
+
+ def test_alias_method_chain_yields_target_and_punctuation
+ FooClassWithBarMethod.send(:define_method, :quux?, Proc.new { })
+ FooClassWithBarMethod.send :include, BarMethods
+ block_called = false
+ FooClassWithBarMethod.alias_method_chain :quux?, :baz do |target, punctuation|
+ block_called = true
+ assert_equal 'quux', target
+ assert_equal '?', punctuation
+ end
+ assert block_called
+ end
end

0 comments on commit 643571c

Please sign in to comment.
Something went wrong with that request. Please try again.