diff options
author | Nate Smith <nate.smith@thescore.com> | 2015-05-14 11:21:24 -0400 |
---|---|---|
committer | Godfrey Chan <godfreykfc@gmail.com> | 2015-05-19 13:29:02 -0700 |
commit | af53280a4b5b3323ac87dc60deb2b1b781197b2b (patch) | |
tree | 654673a761ee89c5123173127f5750517fc19562 /activesupport/test/core_ext/object | |
parent | 0e12300c5e685ddc1b26c99fdb790e54a93153d5 (diff) | |
download | rails-af53280a4b5b3323ac87dc60deb2b1b781197b2b.tar.gz rails-af53280a4b5b3323ac87dc60deb2b1b781197b2b.tar.bz2 rails-af53280a4b5b3323ac87dc60deb2b1b781197b2b.zip |
Patch `Delegator` to work with `#try`
`Delegator` inherits from `BasicObject`, which means that it will not
have `Object#try` defined. It will then delegate the call to the
underlying object, which will not (necessarily) respond to the method
defined in the enclosing `Delegator`.
This patches `Delegator` with the `#try` method to work around the
surprising behaviour.
Fixes #5790
Diffstat (limited to 'activesupport/test/core_ext/object')
-rw-r--r-- | activesupport/test/core_ext/object/try_test.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/object/try_test.rb b/activesupport/test/core_ext/object/try_test.rb index 89438675c1..cdb76af1ac 100644 --- a/activesupport/test/core_ext/object/try_test.rb +++ b/activesupport/test/core_ext/object/try_test.rb @@ -96,4 +96,68 @@ class ObjectTryTest < ActiveSupport::TestCase assert_nil klass.new.try(:private_method) end + + class Decorator < SimpleDelegator + def delegator_method + 'delegator method' + end + + def reverse + 'overridden reverse' + end + + private + + def private_delegator_method + 'private delegator method' + end + end + + def test_try_with_method_on_delegator + assert_equal 'delegator method', Decorator.new(@string).try(:delegator_method) + end + + def test_try_with_method_on_delegator_target + assert_equal 5, Decorator.new(@string).size + end + + def test_try_with_overriden_method_on_delegator + assert_equal 'overridden reverse', Decorator.new(@string).reverse + end + + def test_try_with_private_method_on_delegator + assert_nil Decorator.new(@string).try(:private_delegator_method) + end + + def test_try_with_private_method_on_delegator_bang + assert_raise(NoMethodError) do + Decorator.new(@string).try!(:private_delegator_method) + end + end + + def test_try_with_private_method_on_delegator_target + klass = Class.new do + private + + def private_method + 'private method' + end + end + + assert_nil Decorator.new(klass.new).try(:private_method) + end + + def test_try_with_private_method_on_delegator_target_bang + klass = Class.new do + private + + def private_method + 'private method' + end + end + + assert_raise(NoMethodError) do + Decorator.new(klass.new).try!(:private_method) + end + end end |