提交 6c63f1aa 编写于 作者: J Jon Leighton

Move some serialization stuff out of Base

上级 f4853dc1
......@@ -3,7 +3,41 @@ module AttributeMethods
module Serialization
extend ActiveSupport::Concern
included do
# Returns a hash of all the attributes that have been specified for serialization as
# keys and their class restriction as values.
class_attribute :serialized_attributes
self.serialized_attributes = {}
end
module ClassMethods
# If you have an attribute that needs to be saved to the database as an object, and retrieved as the same object,
# then specify the name of that attribute using this method and it will be handled automatically.
# The serialization is done through YAML. If +class_name+ is specified, the serialized object must be of that
# class on retrieval or SerializationTypeMismatch will be raised.
#
# ==== Parameters
#
# * +attr_name+ - The field name that should be serialized.
# * +class_name+ - Optional, class name that the object type should be equal to.
#
# ==== Example
# # Serialize a preferences attribute
# class User < ActiveRecord::Base
# serialize :preferences
# end
def serialize(attr_name, class_name = Object)
coder = if [:load, :dump].all? { |x| class_name.respond_to?(x) }
class_name
else
Coders::YAMLColumn.new(class_name)
end
# merge new serialized attribute and create new hash to ensure that each class in inheritance hierarchy
# has its own hash of own serialized attributes
self.serialized_attributes = serialized_attributes.merge(attr_name.to_s => coder)
end
def define_method_attribute(attr_name)
if serialized_attributes.include?(attr_name)
generated_attribute_methods.module_eval(<<-CODE, __FILE__, __LINE__)
......@@ -22,6 +56,14 @@ def cacheable_column?(column)
end
end
def set_serialized_attributes
sattrs = self.class.serialized_attributes
sattrs.each do |key, coder|
@attributes[key] = coder.load @attributes[key] if @attributes.key?(key)
end
end
def type_cast_attribute(column, value)
coder = self.class.serialized_attributes[column.name]
......
......@@ -433,11 +433,6 @@ class Base
class_attribute :default_scopes, :instance_writer => false
self.default_scopes = []
# Returns a hash of all the attributes that have been specified for serialization as
# keys and their class restriction as values.
class_attribute :serialized_attributes
self.serialized_attributes = {}
class_attribute :_attr_readonly, :instance_writer => false
self._attr_readonly = []
......@@ -560,33 +555,6 @@ def readonly_attributes
self._attr_readonly
end
# If you have an attribute that needs to be saved to the database as an object, and retrieved as the same object,
# then specify the name of that attribute using this method and it will be handled automatically.
# The serialization is done through YAML. If +class_name+ is specified, the serialized object must be of that
# class on retrieval or SerializationTypeMismatch will be raised.
#
# ==== Parameters
#
# * +attr_name+ - The field name that should be serialized.
# * +class_name+ - Optional, class name that the object type should be equal to.
#
# ==== Example
# # Serialize a preferences attribute
# class User < ActiveRecord::Base
# serialize :preferences
# end
def serialize(attr_name, class_name = Object)
coder = if [:load, :dump].all? { |x| class_name.respond_to?(x) }
class_name
else
Coders::YAMLColumn.new(class_name)
end
# merge new serialized attribute and create new hash to ensure that each class in inheritance hierarchy
# has its own hash of own serialized attributes
self.serialized_attributes = serialized_attributes.merge(attr_name.to_s => coder)
end
def deprecated_property_setter(property, value, block) #:nodoc:
if block
ActiveSupport::Deprecation.warn(
......@@ -2004,14 +1972,6 @@ def to_ary # :nodoc:
nil
end
def set_serialized_attributes
sattrs = self.class.serialized_attributes
sattrs.each do |key, coder|
@attributes[key] = coder.load @attributes[key] if @attributes.key?(key)
end
end
# Sets the attribute used for single table inheritance to this class name if this is not the
# ActiveRecord::Base descendant.
# Considering the hierarchy Reply < Message < ActiveRecord::Base, this makes it possible to
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册