-
-
Save banker/89abe09bb3b6298278a2 to your computer and use it in GitHub Desktop.
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
require 'rubygems' | |
require 'mongo' | |
module DatabaseConnector | |
extend self | |
def connection | |
@@connection ||= Mongo::Connection.new | |
end | |
def database=(name) | |
@@database = name | |
end | |
def database | |
@@database ||= 'test' | |
end | |
def handle | |
connection[self.database] | |
end | |
end | |
class MongoModel | |
def collection | |
DatabaseConnector.handle[self.class.name.downcase] | |
end | |
end | |
class Foo < MongoModel | |
end | |
class Bar < MongoModel | |
end | |
class Baz < MongoModel | |
end | |
foo = Foo.new | |
p foo.collection.save({:a => 1}) | |
p foo.collection.find_one | |
baz = Baz.new | |
p baz.collection.save({:a => 1}) | |
p baz.collection.find_one | |
bar = Bar.new | |
p bar.collection.save({:b => 1}) | |
p bar.collection.find_one |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment