Permalink
Browse files

keep a cache of writer methods

  • Loading branch information...
1 parent f208cc8 commit 55ac7db11bd2fc0cf06d7184f013018fa4be0e9a @tenderlove tenderlove committed Jul 3, 2013
Showing with 34 additions and 11 deletions.
  1. +34 −11 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 ClassMethods
# 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

0 comments on commit 55ac7db

Please sign in to comment.