From 6ef85ec328fd5b5ed02dff1619528aabd799bc93 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Wed, 8 Oct 2008 10:52:04 -0500 Subject: [PATCH] Marshal serialized attributes for optimization. --- .../lib/active_record/attribute_methods.rb | 2 +- activerecord/lib/active_record/base.rb | 18 +++++++++++++++--- .../connection_adapters/abstract/quoting.rb | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 1c75352..f0310e3 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -291,7 +291,7 @@ module ActiveRecord # Returns the unserialized object of the attribute. def unserialize_attribute(attr_name) - unserialized_object = object_from_yaml(@attributes[attr_name]) + unserialized_object = object_from_serialized_data(@attributes[attr_name]) if unserialized_object.is_a?(self.class.serialized_attributes[attr_name]) || unserialized_object.nil? @attributes.frozen? ? unserialized_object : @attributes[attr_name] = unserialized_object diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 6a1a379..e6aa082 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -316,7 +316,7 @@ module ActiveRecord #:nodoc: # # == Saving arrays, hashes, and other non-mappable objects in text columns # - # Active Record can serialize any object in text columns using YAML. To do so, you must specify this with a call to the class method +serialize+. + # Active Record can serialize any object in text columns using Marshal. To do so, you must specify this with a call to the class method +serialize+. # This makes it possible to store arrays, hashes, and other non-mappable objects without doing any additional work. Example: # # class User < ActiveRecord::Base @@ -2936,9 +2936,21 @@ module ActiveRecord #:nodoc: comma_pair_list(quote_columns(quoter, hash)) end - def object_from_yaml(string) + def object_from_serialized_data(string) return string unless string.is_a?(String) - YAML::load(string) rescue string + if string.starts_with?('---') + object_from_yaml(string) + else + object_from_marshal(string) + end + end + + def object_from_yaml(string) + YAML.load(string) rescue string + end + + def object_from_marshal(string) + Marshal.load(string) rescue string end def clone_attributes(reader_method = :read_attribute, attributes = {}) diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb index 3a7bf35..41d0bcb 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb @@ -28,7 +28,7 @@ module ActiveRecord if value.acts_like?(:date) || value.acts_like?(:time) "'#{quoted_date(value)}'" else - "#{quoted_string_prefix}'#{quote_string(value.to_yaml)}'" + "#{quoted_string_prefix}'#{quote_string(Marshal.dump(value))}'" end end end -- 1.5.6