From 9d00b0ce858a6f642089a477e7d39fb3f9cf6777 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 3 Aug 2006 18:47:43 +0000 Subject: Added Module#alias_attribute [Jamis/DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4653 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_support/core_ext/module/aliasing.rb | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'activesupport/lib/active_support/core_ext/module') diff --git a/activesupport/lib/active_support/core_ext/module/aliasing.rb b/activesupport/lib/active_support/core_ext/module/aliasing.rb index 09209884b6..a4c7e539e3 100644 --- a/activesupport/lib/active_support/core_ext/module/aliasing.rb +++ b/activesupport/lib/active_support/core_ext/module/aliasing.rb @@ -27,4 +27,31 @@ class Module alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}" end + + # Allows you to make aliases for attributes, which includes + # getter, setter, and query methods. + # + # Example: + # + # class Content < ActiveRecord::Base + # # has a title attribute + # end + # + # class Email < ActiveRecord::Base + # alias_attribute :subject, :title + # end + # + # e = Email.find(1) + # e.title # => "Superstars" + # e.subject # => "Superstars" + # e.subject? # => true + # e.subject = "Megastars" + # e.title # => "Megastars" + def alias_attribute(new_name, old_name) + module_eval <<-STR, __FILE__, __LINE__+1 + def #{new_name}; #{old_name}; end + def #{new_name}?; #{old_name}?; end + def #{new_name}=(v); self.#{old_name} = v; end + STR + end end -- cgit v1.2.3