From f936a1f100e75082081e782e5cceb272885c2df7 Mon Sep 17 00:00:00 2001 From: Eric Chapweske Date: Sat, 17 Oct 2009 12:37:15 -0500 Subject: Refactoring attributes/types [#3348 state:resolved] Signed-off-by: Joshua Peek --- .../lib/active_record/attributes/aliasing.rb | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 activerecord/lib/active_record/attributes/aliasing.rb (limited to 'activerecord/lib/active_record/attributes/aliasing.rb') 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 + -- cgit v1.2.3