Created
December 20, 2011 17:30
-
-
Save mbj/1502413 to your computer and use it in GitHub Desktop.
DataMapper embedded document for mongodb
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
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