diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-07-18 15:50:02 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-08-15 12:08:23 +0100 |
commit | 6f4b405250157c76fea86c42c8b0854ca4a3c4b8 (patch) | |
tree | 64a3cf1ee7825fba9a0cb08a1b5e3001b5531528 /activesupport/lib | |
parent | 652ab436db674a112bcbc72d8c73e21f2ced512a (diff) | |
download | rails-6f4b405250157c76fea86c42c8b0854ca4a3c4b8.tar.gz rails-6f4b405250157c76fea86c42c8b0854ca4a3c4b8.tar.bz2 rails-6f4b405250157c76fea86c42c8b0854ca4a3c4b8.zip |
Backport Object#public_send to 1.8 so that we can implement Module#delegate such that non-public methods raise
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/object.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/public_send.rb | 25 |
2 files changed, 26 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/object.rb b/activesupport/lib/active_support/core_ext/object.rb index 9ad1e12699..249c2e93c5 100644 --- a/activesupport/lib/active_support/core_ext/object.rb +++ b/activesupport/lib/active_support/core_ext/object.rb @@ -3,6 +3,7 @@ require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/duplicable' require 'active_support/core_ext/object/try' require 'active_support/core_ext/object/inclusion' +require 'active_support/core_ext/object/public_send' require 'active_support/core_ext/object/conversions' require 'active_support/core_ext/object/instance_variables' diff --git a/activesupport/lib/active_support/core_ext/object/public_send.rb b/activesupport/lib/active_support/core_ext/object/public_send.rb new file mode 100644 index 0000000000..233e69b4f8 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/public_send.rb @@ -0,0 +1,25 @@ +require 'active_support/core_ext/kernel/singleton_class' + +class Object + unless Object.public_method_defined?(:public_send) + # Backports Object#public_send from 1.9 + def public_send(method, *args, &block) # :nodoc: + # Don't create a singleton class for the object if it doesn't already have one + # (This also protects us from classes like Fixnum and Symbol, which cannot have a + # singleton class.) + klass = singleton_methods.any? ? self.singleton_class : self.class + + if klass.public_method_defined?(method) + send(method, *args, &block) + else + if klass.private_method_defined?(method) + raise NoMethodError, "private method `#{method}' called for #{inspect}" + elsif klass.protected_method_defined?(method) + raise NoMethodError, "protected method `#{method}' called for #{inspect}" + else + raise NoMethodError, "undefined method `#{method}' for #{inspect}" + end + end + end + end +end |