1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Use encode_with for marshalling

This commit is contained in:
Jon Leighton 2011-01-06 17:01:24 +00:00 committed by Aaron Patterson
parent 2efd780dcb
commit 441118458d
2 changed files with 27 additions and 0 deletions

View file

@ -870,6 +870,16 @@ module ActiveRecord #:nodoc:
reset_scoped_methods
end
# Specifies how the record is loaded by +Marshal+.
#
# +_load+ sets an instance variable for each key in the hash it takes as input.
# Override this method if you require more complex marshalling.
def _load(data)
record = allocate
record.init_with(Marshal.load(data))
record
end
private
def relation #:nodoc:
@ -1425,6 +1435,16 @@ MSG
_run_initialize_callbacks
end
# Specifies how the record is dumped by +Marshal+.
#
# +_dump+ emits a marshalled hash which has been passed to +encode_with+. Override this
# method if you require more complex marshalling.
def _dump(level)
dump = {}
encode_with(dump)
Marshal.dump(dump)
end
# Returns a String, which Action Pack uses for constructing an URL to this
# object. The default implementation returns this record's id as a String,
# or nil if this record's unsaved.

View file

@ -1503,4 +1503,11 @@ class BasicsTest < ActiveRecord::TestCase
ensure
Object.class_eval{ remove_const :UnloadablePost } if defined?(UnloadablePost)
end
def test_marshal_round_trip
expected = posts(:welcome)
actual = Marshal.load(Marshal.dump(expected))
assert_equal expected.attributes, actual.attributes
end
end