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

serialize can take an arbitrary code object

上级 ee34b4cf
......@@ -536,7 +536,13 @@ def readonly_attributes
# serialize :preferences
# end
def serialize(attr_name, class_name = Object)
serialized_attributes[attr_name.to_s] = Coders::YAMLColumn.new(class_name)
coder = if [:load, :dump].all? { |x| class_name.respond_to?(x) }
class_name
else
Coders::YAMLColumn.new(class_name)
end
serialized_attributes[attr_name.to_s] = coder
end
# Guesses the table name (in forced lower-case) based on the name of the class in the
......
......@@ -1018,6 +1018,27 @@ def test_serialized_boolean_value_false
assert_equal topic.content, false
end
def test_serialize_with_coder
coder = Class.new {
# Identity
def load(thing)
thing
end
# base 64
def dump(thing)
[thing].pack('m')
end
}.new
Topic.serialize(:content, coder)
s = 'hello world'
topic = Topic.new(:content => s)
assert topic.save
topic = topic.reload
assert_equal [s].pack('m'), topic.content
end
def test_quote
author_name = "\\ \001 ' \n \\n \""
topic = Topic.create('author_name' => author_name)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册