Fixed that Module#alias_method_chain should work with both foo? foo! and foo at the same time (closes #4954) [anna@wota.jp]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4429 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
b85c5356c9
commit
e72ff355c3
@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Fixed that Module#alias_method_chain should work with both foo? foo! and foo at the same time #4954 [anna@wota.jp]
|
||||
|
||||
* to_xml fixes, features, and speedup: introduce :dasherize option that converts updated_at to updated-at if true (the existing default); binary columns get encoding="base64" attribute; nil values get nil="true" attribute to distinguish empty values; add type information for float columns; allow arbitrarily deep :include; include SQL type information as the type attribute. #4989 [Blair Zajac <blair@orcaware.com>]
|
||||
|
||||
* Add OrderedHash#values. [Sam Stephenson]
|
||||
|
@ -9,11 +9,17 @@ class Module
|
||||
# alias_method_chain :foo, :feature
|
||||
#
|
||||
# And both aliases are set up for you.
|
||||
#
|
||||
# A punctuation is moved to the end on predicates or bang methods.
|
||||
#
|
||||
# alias_method_chain :foo?, :feature
|
||||
#
|
||||
# generates "foo_without_feature?" method for old one,
|
||||
# and expects "foo_with_feature?" method for new one.
|
||||
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.
|
||||
punctuation = target.to_s.scan(/[?!]/).first
|
||||
aliased_target = target.to_s.sub(/[?!]/, '')
|
||||
alias_method "#{aliased_target}_without_#{feature}", target
|
||||
alias_method target, "#{aliased_target}_with_#{feature}"
|
||||
alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target
|
||||
alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}"
|
||||
end
|
||||
end
|
||||
|
@ -109,8 +109,12 @@ def bar_with_baz
|
||||
bar_without_baz << '_with_baz'
|
||||
end
|
||||
|
||||
def quux_with_baz
|
||||
quux_without_baz << '_with_baz'
|
||||
def quux_with_baz!
|
||||
quux_without_baz! << '_with_baz!'
|
||||
end
|
||||
|
||||
def quux_with_baz?
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
@ -148,12 +152,30 @@ def test_alias_method_chain
|
||||
|
||||
def test_alias_method_chain_with_punctuation_method
|
||||
FooClassWithBarMethod.send(:define_method, 'quux!', Proc.new { 'quux' })
|
||||
assert !@instance.respond_to?(:quux_with_baz)
|
||||
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 @instance.respond_to?(:quux_with_baz!)
|
||||
|
||||
assert_equal 'quux_with_baz!', @instance.quux!
|
||||
assert_equal 'quux', @instance.quux_without_baz!
|
||||
end
|
||||
|
||||
assert_equal 'quux_with_baz', @instance.quux!
|
||||
assert_equal 'quux', @instance.quux_without_baz
|
||||
def test_alias_method_chain_with_same_names_between_predicates_and_bang_methods
|
||||
FooClassWithBarMethod.send(:define_method, 'quux!', Proc.new { 'quux' })
|
||||
FooClassWithBarMethod.send(:define_method, 'quux?', Proc.new { true })
|
||||
assert !@instance.respond_to?(:quux_with_baz!)
|
||||
assert !@instance.respond_to?(:quux_with_baz?)
|
||||
|
||||
FooClassWithBarMethod.send(:include, BarMethodAliaser)
|
||||
FooClassWithBarMethod.alias_method_chain :quux!, :baz
|
||||
FooClassWithBarMethod.alias_method_chain :quux?, :baz
|
||||
|
||||
assert @instance.respond_to?(:quux_with_baz!)
|
||||
assert @instance.respond_to?(:quux_with_baz?)
|
||||
assert_equal 'quux_with_baz!', @instance.quux!
|
||||
assert_equal 'quux', @instance.quux_without_baz!
|
||||
assert_equal false, @instance.quux?
|
||||
assert_equal true, @instance.quux_without_baz?
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user