diff options
| author | David Heinemeier Hansson <david@loudthinking.com> | 2006-08-03 18:47:43 +0000 | 
|---|---|---|
| committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-08-03 18:47:43 +0000 | 
| commit | 9d00b0ce858a6f642089a477e7d39fb3f9cf6777 (patch) | |
| tree | 1da460b1b198591eca304b91e35eb1d27c42e9b4 | |
| parent | b5c2366569573c76ab9dcbf871a4a00b91d7f141 (diff) | |
| download | rails-9d00b0ce858a6f642089a477e7d39fb3f9cf6777.tar.gz rails-9d00b0ce858a6f642089a477e7d39fb3f9cf6777.tar.bz2 rails-9d00b0ce858a6f642089a477e7d39fb3f9cf6777.zip | |
Added Module#alias_attribute [Jamis/DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4653 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
| -rw-r--r-- | activesupport/CHANGELOG | 17 | ||||
| -rw-r--r-- | activesupport/lib/active_support/core_ext/date/conversions.rb | 8 | ||||
| -rw-r--r-- | activesupport/lib/active_support/core_ext/module/aliasing.rb | 27 | ||||
| -rw-r--r-- | activesupport/test/core_ext/module/attribute_aliasing_test.rb | 31 | ||||
| -rw-r--r-- | activesupport/test/core_ext/module_test.rb | 1 | 
5 files changed, 80 insertions, 4 deletions
| diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 98aa5b4468..027c8d5351 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,22 @@  *SVN* +* Added Module#alias_attribute [Jamis/DHH]. 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" +  * Deprecation: easier to work with warning behavior as procs; default behaviors for each environment so users needn't update env.rb; and testing pleasure with assert_deprecated, assert_not_deprecated. [Jeremy Kemper]    By default, test prints to $stderr, dev logs, production ignores.    Provide your own per-environment in e.g. config/environments/development.rb: diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index 627c8dd485..bae20c66db 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -25,9 +25,11 @@ module ActiveSupport #:nodoc:          def to_time(form = :local)            ::Time.send(form, year, month, day)          end -         -        alias :xmlschema :to_s + +        def xmlschema +          to_time.xmlschema +        end        end      end    end -end
\ No newline at end of file +end 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 diff --git a/activesupport/test/core_ext/module/attribute_aliasing_test.rb b/activesupport/test/core_ext/module/attribute_aliasing_test.rb new file mode 100644 index 0000000000..66ddbfe62a --- /dev/null +++ b/activesupport/test/core_ext/module/attribute_aliasing_test.rb @@ -0,0 +1,31 @@ +require File.dirname(__FILE__) + '/../../abstract_unit' + +module AttributeAliasing +  class Content +    attr_accessor :title +   +    def title? +      !title.nil? +    end +  end + +  class Email < Content +    alias_attribute :subject, :title +  end +end + +class AttributeAliasingTest < Test::Unit::TestCase +  def test_attribute_alias +    e = AttributeAliasing::Email.new + +    assert !e.subject? + +    e.title = "Upgrade computer" +    assert_equal "Upgrade computer", e.subject +    assert e.subject? + +    e.subject = "We got a long way to go" +    assert_equal "We got a long way to go", e.title +    assert e.title? +  end +end diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index 5507ef3120..cdcb511e45 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -117,7 +117,6 @@ module BarMethodAliaser  end  class MethodAliasingTest < Test::Unit::TestCase -    def setup      Object.const_set(:FooClassWithBarMethod, Class.new)      FooClassWithBarMethod.send(:define_method, 'bar', Proc.new { 'bar' }) | 
