Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created May 12, 2012 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahoward/2665260 to your computer and use it in GitHub Desktop.
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
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