mirror of
https://github.com/github/rails.git
synced 2026-01-29 16:28:09 -05:00
Strip out punctuation on predicates or bang methods being aliased with alias_method_chain since target?_without_feature is not a valid method name. Add tests for Module#alias_method_chain. [Marcel Molina Jr.]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4311 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Strip out punctuation on predicates or bang methods being aliased with alias_method_chain since target?_without_feature is not a valid method name. Add tests for Module#alias_method_chain. [Marcel Molina Jr.]
|
||||
|
||||
* Replace Ruby's deprecated append_features in favor of included. [Marcel Molina Jr.]
|
||||
|
||||
* Allow default options in with_options to be overridden. Closes #4480. [murphy@cYcnus.de]
|
||||
|
||||
@@ -10,7 +10,10 @@ class Module
|
||||
#
|
||||
# And both aliases are set up for you.
|
||||
def alias_method_chain(target, feature)
|
||||
alias_method "#{target}_without_#{feature}", target
|
||||
alias_method target, "#{target}_with_#{feature}"
|
||||
# Strip out punctuation on predicates or bang methods since
|
||||
# e.g. target?_without_feature is not a valid method name.
|
||||
aliased_target = target.to_s.sub(/[?!]/, '')
|
||||
alias_method "#{aliased_target}_without_#{feature}", target
|
||||
alias_method target, "#{aliased_target}_with_#{feature}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -98,4 +98,62 @@ class ModuleTest < Test::Unit::TestCase
|
||||
assert_equal 'yz/zy', Yz::Zy.as_load_path
|
||||
assert_equal 'yz', Yz.as_load_path
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module BarMethodAliaser
|
||||
def self.included(foo_class)
|
||||
foo_class.alias_method_chain :bar, :baz
|
||||
end
|
||||
|
||||
def bar_with_baz
|
||||
bar_without_baz << '_with_baz'
|
||||
end
|
||||
|
||||
def quux_with_baz
|
||||
quux_without_baz << '_with_baz'
|
||||
end
|
||||
end
|
||||
|
||||
class MethodAliasingTest < Test::Unit::TestCase
|
||||
|
||||
def setup
|
||||
Object.const_set(:FooClassWithBarMethod, Class.new)
|
||||
FooClassWithBarMethod.send(:define_method, 'bar', Proc.new { 'bar' })
|
||||
@instance = FooClassWithBarMethod.new
|
||||
end
|
||||
|
||||
def teardown
|
||||
Object.send(:remove_const, :FooClassWithBarMethod)
|
||||
end
|
||||
|
||||
def test_alias_method_chain
|
||||
assert @instance.respond_to? :bar
|
||||
feature_aliases = [:bar_with_baz, :bar_without_baz]
|
||||
|
||||
feature_aliases.each do |method|
|
||||
assert !@instance.respond_to?(method)
|
||||
end
|
||||
|
||||
assert_equal 'bar', @instance.bar
|
||||
|
||||
FooClassWithBarMethod.send(:include, BarMethodAliaser)
|
||||
|
||||
feature_aliases.each do |method|
|
||||
assert @instance.respond_to?(method)
|
||||
end
|
||||
|
||||
assert_equal 'bar_with_baz', @instance.bar
|
||||
assert_equal 'bar', @instance.bar_without_baz
|
||||
end
|
||||
|
||||
def test_alias_method_chain_with_punctuation_method
|
||||
FooClassWithBarMethod.send(:define_method, 'quux!', Proc.new { 'quux' })
|
||||
assert !@instance.respond_to?(:quux_with_baz)
|
||||
FooClassWithBarMethod.send(:include, BarMethodAliaser)
|
||||
FooClassWithBarMethod.alias_method_chain :quux!, :baz
|
||||
assert @instance.respond_to?(:quux_with_baz)
|
||||
|
||||
assert_equal 'quux_with_baz', @instance.quux!
|
||||
assert_equal 'quux', @instance.quux_without_baz
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user