diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-07-03 10:56:56 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-07-03 10:58:07 -0700 |
commit | 55ac7db11bd2fc0cf06d7184f013018fa4be0e9a (patch) | |
tree | 775a5fa6abd80be42a77eeed37db4b512e2f8d23 | |
parent | f208cc8108d6d3ae70de2705cc1111edcf9b82b1 (diff) | |
download | rails-55ac7db11bd2fc0cf06d7184f013018fa4be0e9a.tar.gz rails-55ac7db11bd2fc0cf06d7184f013018fa4be0e9a.tar.bz2 rails-55ac7db11bd2fc0cf06d7184f013018fa4be0e9a.zip |
keep a cache of writer methods
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/write.rb | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb index 9c7f643283..813a85d2f7 100644 --- a/activerecord/lib/active_record/attribute_methods/write.rb +++ b/activerecord/lib/active_record/attribute_methods/write.rb @@ -1,6 +1,36 @@ module ActiveRecord module AttributeMethods module Write + WriterMethodCache = Class.new { + include Mutex_m + + def initialize + super + @module = Module.new + @method_cache = {} + end + + def [](name) + synchronize do + @method_cache.fetch(name) { + safe_name = name.unpack('h*').first + temp_method = "__temp__#{safe_name}=" + + ActiveRecord::AttributeMethods::AttrNames.set_name_cache safe_name, name + + @module.module_eval <<-STR, __FILE__, __LINE__ + 1 + def #{temp_method}(value) + name = ::ActiveRecord::AttributeMethods::AttrNames::ATTR_#{safe_name} + write_attribute(name, value) + end + STR + + @method_cache[name] = @module.instance_method temp_method + } + end + end + }.new + extend ActiveSupport::Concern included do @@ -13,17 +43,10 @@ module ActiveRecord # See define_method_attribute in read.rb for an explanation of # this code. def define_method_attribute=(name) - safe_name = name.unpack('h*').first - ActiveRecord::AttributeMethods::AttrNames.set_name_cache safe_name, name - - generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1 - def __temp__#{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}= - STR + method = WriterMethodCache[name] + generated_attribute_methods.module_eval { + define_method "#{name}=", method + } end end |