提交 a6cf6ec9 编写于 作者: A Aaron Patterson

move the coders to the serialized_attributes hash

上级 69600a9f
......@@ -106,14 +106,10 @@ def unserializable_attribute?(attr_name, column)
# Returns the unserialized object of the attribute.
def unserialize_attribute(attr_name)
unserialized_object = object_from_yaml(@attributes[attr_name])
coder = self.class.serialized_attributes[attr_name]
unserialized_object = coder.load(@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
else
raise SerializationTypeMismatch,
"#{attr_name} was supposed to be a #{self.class.serialized_attributes[attr_name]}, but was a #{unserialized_object.class.to_s}"
end
@attributes.frozen? ? unserialized_object : @attributes[attr_name] = unserialized_object
end
private
......
......@@ -536,7 +536,7 @@ def readonly_attributes
# serialize :preferences
# end
def serialize(attr_name, class_name = Object)
serialized_attributes[attr_name.to_s] = class_name
serialized_attributes[attr_name.to_s] = Coders::YAMLColumn.new(class_name)
end
# Guesses the table name (in forced lower-case) based on the name of the class in the
......@@ -1738,8 +1738,9 @@ def arel_attributes_values(include_primary_key = true, include_readonly_attribut
if include_readonly_attributes || (!include_readonly_attributes && !self.class.readonly_attributes.include?(name))
value = read_attribute(name)
if !value.nil? && self.class.serialized_attributes.key?(name)
value = YAML.dump value
coder = self.class.serialized_attributes[name]
if !value.nil? && coder
value = coder.dump value
end
attrs[self.class.arel_table[name]] = value
end
......@@ -1865,11 +1866,6 @@ def convert_number_column_value(value)
end
end
def object_from_yaml(string)
return string unless string.is_a?(String) && string =~ /^---/
YAML::load(string) rescue string
end
def populate_with_current_scope_attributes
if scope = self.class.send(:current_scoped_methods)
create_with = scope.scope_for_create
......
......@@ -14,16 +14,21 @@ def initialize(object_class = Object)
@object_class = object_class
end
def dump(obj)
YAML.dump obj
end
def load(yaml)
return yaml unless yaml.is_a?(String) && yaml =~ /^---/
begin
obj = YAML::load(yaml)
obj = YAML.load(yaml)
unless obj.is_a?(object_class) || obj.nil?
raise SerializationTypeMismatch,
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}"
end
obj
rescue *RESCUE_ERRORS
yaml
end
......
......@@ -15,8 +15,10 @@ def setup(klass)
def validate_each(record, attribute, value)
finder_class = find_finder_class_for(record)
if value && record.class.serialized_attributes.key?(attribute.to_s)
value = YAML.dump value
coder = record.class.serialized_attributes[attribute.to_s]
if value && coder
value = coder.dump value
end
sql, params = mount_sql_and_params(finder_class, record.class.quoted_table_name, attribute, value)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册