Created
August 23, 2012 03:32
-
-
Save ssimeonov/3431917 to your computer and use it in GitHub Desktop.
Storing & retrieving proto objects from 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
add = function(., name, p) { | |
data <- list(name=name, type=p$..Name, data=as.list(p)) | |
.$collection()$insert(data) | |
}, | |
get = function(., name) { | |
doc <- .$collection()$query_one(name=name) | |
if (!is.null(doc)) { | |
factory <- base:::get(doc$type, pos=1) | |
do.call(factory$new, doc$data) | |
} else { | |
NULL | |
} | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment