-
-
Save ahoward/2665260 to your computer and use it in GitHub Desktop.
serialization cannot be allowed to return new objects for the same inputs in the same object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class C | |
include Mongoid::Document | |
class Struct < ::Hash | |
def method_missing(method, *args, &block) | |
key = method.to_s | |
has_key?(key) ? fetch(key) : super | |
end | |
class Type | |
old_skool = defined?(Mongoid::Fields::Serializable) | |
if old_skool | |
include Mongoid::Fields::Serializable | |
def deserialize(hash) | |
Struct.new(hash) | |
end | |
def serialize(hash) | |
hash | |
end | |
else | |
def self.demongoize(hash) | |
Struct.new(hash) | |
end | |
def mongoize(hash) | |
hash | |
end | |
end | |
end | |
end | |
field(:data, :type => Struct::Type, :default => proc{ Struct.new }) | |
end | |
c = C.new | |
data = c.data | |
data.update(:key => :val) | |
p data #=> {:key=>:val} | |
p c.data #=> {} | |
p data.object_id #=> 70178108577780 | |
p c.data.object_id #=> 70178108577340 | |
p c.data.object_id #=> 70178108577220 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment