yohanboniface/redis-limpyd — pull request #45 by twidi (2012-09-26 20:49:41+00:00)
Title: Update database, or only connection settings

Two things related to database in this pull request
* Add a way to change database on the fly
* Add a way to change database connection settings

Add a way to change database on the fly:
=======================================

The main scenario is: an external lib define some models, but as the
database is mandatory, it may not be on the correct redis server/db.
And another problem, you cannot use these models on related fields
defined by another/your database because related fields must be on the
same database.

So this pull-request introduces the "use_database" method on RedisModel.
It's a class method, so all subclasses of the concerned model will
have the new database (only if they shared the same one as the parent
model)

Example. Say we have a lib with some models:

class BaseModel(RedisModel):
database = RedisDatabase()
abstract = True

class Foo(BaseModel):
# ... fields ...

class Bar(BaseModel):
# ... fields ...

So in your code you can just do

database = RedisDatabase(**connection_settings)
BaseModel.use_database(database)

Add a way to change database connection settings
================================================

The RedisDatabase object now has a new "connect" method which accepts the
same parameters as the constructor to define new connection settings to
use for the next calls to redis.

main_database = RedisDatabase(host='localhost', port=6379, db=0)
# ... later ...
main_database.connect(host='localhost', port=6370, db=3)