aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-01-05 14:59:19 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2011-01-05 14:59:19 -0800
commiteba8411652cb39529839083cf903f6ce76a69f4a (patch)
tree3f70a9f3c1045824f5188049c0cd51cc251d6d53
parent97bc74c74611d3d71d58776ed907ebd0cdb98a15 (diff)
downloadrails-eba8411652cb39529839083cf903f6ce76a69f4a.tar.gz
rails-eba8411652cb39529839083cf903f6ce76a69f4a.tar.bz2
rails-eba8411652cb39529839083cf903f6ce76a69f4a.zip
adding an `encode_with` method for Psych dump/load methods
-rw-r--r--activerecord/lib/active_record/base.rb16
-rw-r--r--activerecord/test/cases/yaml_serialization_test.rb7
2 files changed, 23 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 0a2e436ecc..d3a739b98b 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1386,6 +1386,22 @@ MSG
result
end
+ # Populate +coder+ with attributes about this record that should be
+ # serialized. The structure of +coder+ defined in this method is
+ # guaranteed to match the structure of +coder+ passed to the +init_with+
+ # method.
+ #
+ # Example:
+ #
+ # class Post < ActiveRecord::Base
+ # end
+ # coder = {}
+ # Post.new.encode_with(coder)
+ # coder # => { 'id' => nil, ... }
+ def encode_with(coder)
+ coder['attributes'] = attributes
+ end
+
# Initialize an empty model object from +coder+. +coder+ must contain
# the attributes necessary for initializing an empty model object. For
# example:
diff --git a/activerecord/test/cases/yaml_serialization_test.rb b/activerecord/test/cases/yaml_serialization_test.rb
index abcf61ffc0..0b54c309d1 100644
--- a/activerecord/test/cases/yaml_serialization_test.rb
+++ b/activerecord/test/cases/yaml_serialization_test.rb
@@ -18,6 +18,13 @@ class YamlSerializationTest < ActiveRecord::TestCase
assert_equal topic, t
end
+ def test_encode_with_coder
+ topic = Topic.first
+ coder = {}
+ topic.encode_with coder
+ assert_equal({'attributes' => topic.attributes}, coder)
+ end
+
begin
require 'psych'