Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4653

Show
Ignore:
Timestamp:
08/03/06 18:47:43 (2 years ago)
Author:
david
Message:

Added Module#alias_attribute [Jamis/DHH]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r4647 r4653  
    11*SVN* 
     2 
     3* Added Module#alias_attribute [Jamis/DHH]. Example: 
     4 
     5    class Content < ActiveRecord::Base 
     6      # has a title attribute 
     7    end 
     8  
     9    class Email < ActiveRecord::Base 
     10      alias_attribute :subject, :title 
     11    end 
     12  
     13    e = Email.find(1) 
     14    e.title    # => "Superstars" 
     15    e.subject  # => "Superstars" 
     16    e.subject? # => true 
     17    e.subject = "Megastars" 
     18    e.title    # => "Megastars" 
    219 
    320* 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] 
  • trunk/activesupport/lib/active_support/core_ext/date/conversions.rb

    r3952 r4653  
    2626          ::Time.send(form, year, month, day) 
    2727        end 
    28          
    29         alias :xmlschema :to_s 
     28 
     29        def xmlschema 
     30          to_time.xmlschema 
     31        end 
    3032      end 
    3133    end 
  • trunk/activesupport/lib/active_support/core_ext/module/aliasing.rb

    r4534 r4653  
    2828    alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}" 
    2929  end 
     30 
     31  # Allows you to make aliases for attributes, which includes  
     32  # getter, setter, and query methods. 
     33  # 
     34  # Example: 
     35  # 
     36  #   class Content < ActiveRecord::Base 
     37  #     # has a title attribute 
     38  #   end 
     39  # 
     40  #   class Email < ActiveRecord::Base 
     41  #     alias_attribute :subject, :title 
     42  #   end 
     43  # 
     44  #   e = Email.find(1) 
     45  #   e.title    # => "Superstars" 
     46  #   e.subject  # => "Superstars" 
     47  #   e.subject? # => true 
     48  #   e.subject = "Megastars" 
     49  #   e.title    # => "Megastars" 
     50  def alias_attribute(new_name, old_name) 
     51    module_eval <<-STR, __FILE__, __LINE__+1 
     52      def #{new_name}; #{old_name}; end 
     53      def #{new_name}?; #{old_name}?; end 
     54      def #{new_name}=(v); self.#{old_name} = v; end 
     55    STR 
     56  end 
    3057end 
  • trunk/activesupport/test/core_ext/module_test.rb

    r4595 r4653  
    118118 
    119119class MethodAliasingTest < Test::Unit::TestCase 
    120  
    121120  def setup 
    122121    Object.const_set(:FooClassWithBarMethod, Class.new)