From 59482f93ec84a54069ef720ed4ecf5c4a3d302a0 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 2 Jul 2013 17:32:33 -0700 Subject: stop storing multiple copies of a particular attribute name --- .../lib/active_record/attribute_methods.rb | 22 ++++++++++------------ .../lib/active_record/attribute_methods/read.rb | 5 +++-- .../lib/active_record/attribute_methods/write.rb | 5 +++-- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 9f8f309c02..13d7c6b781 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -19,6 +19,15 @@ module ActiveRecord include Serialization end + AttrNames = Module.new { + def self.set_name_cache(name, value) + const_name = "ATTR_#{name}" + unless const_defined? const_name + const_set const_name, value + end + end + } + module ClassMethods def inherited(child_class) #:nodoc: child_class.initialize_generated_modules @@ -26,18 +35,7 @@ module ActiveRecord end def initialize_generated_modules # :nodoc: - @generated_attribute_methods = Module.new { - extend Mutex_m - - const_set :AttrNames, Module.new { - def self.set_name_cache(name, value) - const_name = "ATTR_#{name}" - unless const_defined? const_name - const_set const_name, value - end - end - } - } + @generated_attribute_methods = Module.new { extend Mutex_m } @attribute_methods_generated = false include @generated_attribute_methods end diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb index d83c5a84b4..2c8ae9c774 100644 --- a/activerecord/lib/active_record/attribute_methods/read.rb +++ b/activerecord/lib/active_record/attribute_methods/read.rb @@ -49,11 +49,12 @@ module ActiveRecord # key the @attributes_cache in read_attribute. def define_method_attribute(name) safe_name = name.unpack('h*').first - generated_attribute_methods::AttrNames.set_name_cache safe_name, name + ActiveRecord::AttributeMethods::AttrNames.set_name_cache safe_name, name generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1 def __temp__#{safe_name} - read_attribute(AttrNames::ATTR_#{safe_name}) { |n| missing_attribute(n, caller) } + name = ::ActiveRecord::AttributeMethods::AttrNames::ATTR_#{safe_name} + read_attribute(name) { |n| missing_attribute(n, caller) } end alias_method #{name.inspect}, :__temp__#{safe_name} undef_method :__temp__#{safe_name} diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb index 541856c678..9c7f643283 100644 --- a/activerecord/lib/active_record/attribute_methods/write.rb +++ b/activerecord/lib/active_record/attribute_methods/write.rb @@ -14,11 +14,12 @@ module ActiveRecord # this code. def define_method_attribute=(name) safe_name = name.unpack('h*').first - generated_attribute_methods::AttrNames.set_name_cache safe_name, name + ActiveRecord::AttributeMethods::AttrNames.set_name_cache safe_name, name generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1 def __temp__#{safe_name}=(value) - write_attribute(AttrNames::ATTR_#{safe_name}, value) + name = ::ActiveRecord::AttributeMethods::AttrNames::ATTR_#{safe_name} + write_attribute(name, value) end alias_method #{(name + '=').inspect}, :__temp__#{safe_name}= undef_method :__temp__#{safe_name}= -- cgit v1.2.3