Browse files

Properly restore plain Ruby embedded objects.

All integration tests now pass.
  • Loading branch information...
1 parent 0bf3f73 commit b317c7659527299acdbc54ba2e762c2d47b05df5 @elliotcm committed Mar 21, 2010
Showing with 14 additions and 3 deletions.
  1. +0 −1 integration/object_embedding_spec.rb
  2. +14 −2 lib/document/serialization/serializer.rb
View
1 integration/object_embedding_spec.rb
@@ -30,7 +30,6 @@ class Comment
end
it "is represented as embedded MongoDB documents and eager restored." do
- pending
stored_article = Article.find.first
stored_article.title.should == @title
View
16 lib/document/serialization/serializer.rb
@@ -14,9 +14,21 @@ def deserialize(object_to_deserialize)
return Marshal.load(object_to_deserialize['_data'])
end
- if object_to_deserialize.has_key?('_embed') and object_to_deserialize['_embed'] == true
+ if object_to_deserialize.has_key?('_class_name')
class_name = object_to_deserialize.delete('_class_name')
- return Object.const_get(class_name).find(object_to_deserialize['_id']).first
+
+ if !object_to_deserialize.has_key?('_id')
+ object = Object.const_get(class_name).new
+ object_to_deserialize.each_pair do |attr_name, attr_value|
+ object.instance_variable_set '@'+attr_name, attr_value
+ end
+
+ return object
+ end
+
+ if object_to_deserialize.has_key?('_embed') and object_to_deserialize['_embed'] == true
+ return Object.const_get(class_name).find(object_to_deserialize['_id']).first
+ end
end
return hash_deserialize(object_to_deserialize)

0 comments on commit b317c76

Please sign in to comment.