Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

alias_method_chain works with accessor= methods also. Closes #6153.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5091 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 74f60c032e92b2473b5feeeb85782836c2b83fdd 1 parent 3c0e7b1
@jeremy jeremy authored
View
2  activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* alias_method_chain works with accessor= methods also. #6153 [Caio Chassot]
+
* Fix loadable_constants_for_path to handle load paths that do not end with a slash. [Nicholas Seckar]
* Fix logic error in determining what was loaded by a given file. Closes #6039. [Nicholas Seckar]
View
2  activesupport/lib/active_support/core_ext/module/aliasing.rb
@@ -23,7 +23,7 @@ class Module
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
+ aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target
alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}"
end
View
31 activesupport/test/core_ext/module_test.rb
@@ -108,12 +108,16 @@ def bar_with_baz
end
def quux_with_baz!
- quux_without_baz! << '_with_baz!'
+ quux_without_baz! << '_with_baz'
end
def quux_with_baz?
false
end
+
+ def quux_with_baz=(v)
+ send(:quux_without_baz=, v) << '_with_baz'
+ end
end
class MethodAliasingTest < Test::Unit::TestCase
@@ -154,26 +158,35 @@ def test_alias_method_chain_with_punctuation_method
FooClassWithBarMethod.alias_method_chain :quux!, :baz
assert @instance.respond_to?(:quux_with_baz!)
- assert_equal 'quux_with_baz!', @instance.quux!
+ assert_equal 'quux_with_baz', @instance.quux!
assert_equal 'quux', @instance.quux_without_baz!
end
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 { 'quux!' })
FooClassWithBarMethod.send(:define_method, 'quux?', Proc.new { true })
+ FooClassWithBarMethod.send(:define_method, 'quux=', Proc.new { 'quux=' })
assert !@instance.respond_to?(:quux_with_baz!)
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 @instance.respond_to?(:quux_with_baz=)
+
+
+ FooClassWithBarMethod.alias_method_chain :quux!, :baz
+ assert_equal 'quux!_with_baz', @instance.quux!
+ assert_equal 'quux!', @instance.quux_without_baz!
+
+ FooClassWithBarMethod.alias_method_chain :quux?, :baz
assert_equal false, @instance.quux?
assert_equal true, @instance.quux_without_baz?
+
+ FooClassWithBarMethod.alias_method_chain :quux=, :baz
+ assert_equal 'quux=_with_baz', @instance.send(:quux=, 1234)
+ assert_equal 'quux=', @instance.send(:quux_without_baz=, 1234)
end
def test_alias_method_chain_with_feature_punctuation
@@ -183,7 +196,7 @@ def test_alias_method_chain_with_feature_punctuation
FooClassWithBarMethod.alias_method_chain :quux, :baz!
assert_nothing_raised do
- assert_equal 'quux_with_baz!', @instance.quux_with_baz!
+ assert_equal 'quux_with_baz', @instance.quux_with_baz!
end
assert_raise(NameError) do
Please sign in to comment.
Something went wrong with that request. Please try again.