csyork/csbot — pull request #46 by alanbriolat (2012-12-04 21:12:49+00:00)
Title: Refactor mongodb support to a plugin, where it belongs

It's essential to move the MongoDB support out of the ``Bot`` class and into a plugin so that unit tests can run in an environment without a MongoDB server. Unfortunately, this has some impact on other plugins: they can no longer assume ``self.db`` exists.

The docstring on the ``get_db()`` method gives an example of code for a plugin that requires a MongoDB database, using the plugin dependency and integration mechanisms.

Admittedly, this looks a little like boilerplate, which I like to avoid. It might be possible to implement a layer of evil magic mechanisms so that plugins can provide easy access for things like this to other plugins, however I think it's already difficult enough to follow the actual thread of execution.

As it is, a plugin's database setup probably consists of more than getting a database reference: it'll probably want to get references to some collections, maybe create some indexes, maybe add some initial data. In this case, the ``@Plugin.integrate_with('mongodb')``-decorated method gives an excellent place to do this.