From 7895182d0fce11131024305f53d0cbb32817e65c Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Wed, 30 Nov 2011 22:31:52 +0000 Subject: omg computer science! Implement a mini state machine for serialized attributes. This means we do not have to deserialize the values upon initialization, which means that if we never actually access the attribute, we never have to deserialize it. --- activerecord/lib/active_record/attribute_methods/write.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record/attribute_methods/write.rb') diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb index b605c09889..650156f3cf 100644 --- a/activerecord/lib/active_record/attribute_methods/write.rb +++ b/activerecord/lib/active_record/attribute_methods/write.rb @@ -28,10 +28,8 @@ module ActiveRecord @attributes_cache.delete(attr_name) column = column_for_attribute(attr_name) - if column && column.number? - @attributes[attr_name] = convert_number_column_value(value) - elsif column || @attributes.has_key?(attr_name) - @attributes[attr_name] = value + if column || @attributes.has_key?(attr_name) + @attributes[attr_name] = type_cast_attribute_for_write(column, attr_name, value) else raise ActiveModel::MissingAttributeError, "can't write unknown attribute `#{attr_name}'" end @@ -43,6 +41,14 @@ module ActiveRecord def attribute=(attribute_name, value) write_attribute(attribute_name, value) end + + def type_cast_attribute_for_write(column, attr_name, value) + if column && column.number? + convert_number_column_value(value) + else + value + end + end end end end -- cgit v1.2.3