diff options
author | Eric Chapweske <eric@chapweske.com> | 2009-10-17 12:37:15 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-10-17 12:37:15 -0500 |
commit | f936a1f100e75082081e782e5cceb272885c2df7 (patch) | |
tree | 6c5091faa38f15765b3be153141b81d693b02d18 /activerecord/lib/active_record/attributes/aliasing.rb | |
parent | e13d232150921cdf0ec3d713caefa628d235152e (diff) | |
download | rails-f936a1f100e75082081e782e5cceb272885c2df7.tar.gz rails-f936a1f100e75082081e782e5cceb272885c2df7.tar.bz2 rails-f936a1f100e75082081e782e5cceb272885c2df7.zip |
Refactoring attributes/types [#3348 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
Diffstat (limited to 'activerecord/lib/active_record/attributes/aliasing.rb')
-rw-r--r-- | activerecord/lib/active_record/attributes/aliasing.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/attributes/aliasing.rb b/activerecord/lib/active_record/attributes/aliasing.rb new file mode 100644 index 0000000000..db77739d1f --- /dev/null +++ b/activerecord/lib/active_record/attributes/aliasing.rb @@ -0,0 +1,42 @@ +module ActiveRecord + module Attributes + module Aliasing + # Allows access to keys using aliased names. + # + # Example: + # class Attributes < Hash + # include Aliasing + # end + # + # attributes = Attributes.new + # attributes.aliases['id'] = 'fancy_primary_key' + # attributes['fancy_primary_key'] = 2020 + # + # attributes['id'] + # => 2020 + # + # Additionally, symbols are always aliases of strings: + # attributes[:fancy_primary_key] + # => 2020 + # + def [](key) + super(unalias(key)) + end + + def []=(key, value) + super(unalias(key), value) + end + + def aliases + @aliases ||= {} + end + + def unalias(key) + key = key.to_s + aliases[key] || key + end + + end + end +end + |