From 794d93f7a5ede9801929ef2a5292a0380f0700b5 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Wed, 26 Apr 2006 17:46:43 +0000 Subject: [PATCH] Added Module#alias_method_chain git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4276 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 2 ++ .../lib/active_support/core_ext/module.rb | 1 + .../active_support/core_ext/module/aliasing.rb | 16 ++++++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/module/aliasing.rb diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index e38eb9ad31..3a4b4e477f 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added Module#alias_method_chain [Jamis Buck] + * Updated to Builder 2.0 [DHH] * Add Array#split for dividing arrays into one or more subarrays by value or block. [Sam Stephenson] diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb index e67cf940f6..45069f52af 100644 --- a/activesupport/lib/active_support/core_ext/module.rb +++ b/activesupport/lib/active_support/core_ext/module.rb @@ -3,3 +3,4 @@ require File.dirname(__FILE__) + '/module/delegation' require File.dirname(__FILE__) + '/module/introspection' require File.dirname(__FILE__) + '/module/loading' +require File.dirname(__FILE__) + '/module/aliasing' diff --git a/activesupport/lib/active_support/core_ext/module/aliasing.rb b/activesupport/lib/active_support/core_ext/module/aliasing.rb new file mode 100644 index 0000000000..a80ff0f26d --- /dev/null +++ b/activesupport/lib/active_support/core_ext/module/aliasing.rb @@ -0,0 +1,16 @@ +class Module + # Encapsulates the common pattern of: + # + # alias_method :foo_without_feature, :foo + # alias_method :foo, :foo_with_feature + # + # With this, you simply do: + # + # alias_method_chain :foo, :feature + # + # 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}" + end +end