Skip to content

Instantly share code, notes, and snippets.

@mbj
Created December 20, 2011 17:30
Show Gist options
  • Save mbj/1502413 to your computer and use it in GitHub Desktop.
Save mbj/1502413 to your computer and use it in GitHub Desktop.
DataMapper embedded document for mongodb
module DataMapper
class Property
class EmbeddedDocument < Property
coercion_method nil
dump_as Hash
accept_options :document_class
protected
def initialize(model, name, options = {})
super
@document_class = @options.fetch(:document_class) { raise ArgumentError,'missing :document_class' }
@load_as = @document_class
end
public
def load(value)
if value.nil?
nil
else
@document_class.datamapper_load value
end
end
def typecast(value)
case value
when @document_class
value
when NilClass
nil
else
raise ArgumentError,"+value+ must be of kind #{@document_class} or nil was: #{value.class}"
end
end
def dump(value)
if value.nil?
nil
else
@document_class.datamapper_dump value
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment