Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@banker

banker/model.rb Secret

Created April 10, 2012 21:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banker/89abe09bb3b6298278a2 to your computer and use it in GitHub Desktop.
Save banker/89abe09bb3b6298278a2 to your computer and use it in GitHub Desktop.
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