From 55ac7db11bd2fc0cf06d7184f013018fa4be0e9a Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 3 Jul 2013 10:56:56 -0700 Subject: keep a cache of writer methods --- .../lib/active_record/attribute_methods/write.rb | 45 ++++++++++++++++------ 1 file changed, 34 insertions(+), 11 deletions(-) (limited to 'activerecord') 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 -- cgit v1.2.3