How can I build a friendly nosql ORM without polluting the global scope?
For a whi开发者_JS百科le, I've been working on building a little Ruby library to interface with CouchDB, a neat little document database with a HTTP interface. Key features are:
- document objects are glorified hashes
- the JavaScript Map/Reduce functions are written in native Ruby, and parsed into JavaScript using S Expressions
- you can interface with multiple Couch databases
- it should integrate well with micro-frameworks like Camping
I want to be able to do something like this:
@recipes = Recipes.all
Where "Recipes" is a class defining a couple of required keys that the document has (the class name is automatically used as a "kind" key).
But then in tough times I might want to do something like this:
@recipes.each do |recipe|
recipe.cost = "too much!!"
recipe.push!
end
Now, obviously to be able to "push" like that, I either need the database to be.. somewhere in scope.. or for the document object itself to hold a reference to the database object? How is this done in well-established ORMs like ActiveRecord?
I don't want to have to do, you know, recipe.push!(@couch_database_object)
, or whatever, because that's yucky! But I don't wan to be some scope-polluting scumbag.
Any advice?
精彩评论