How To Write A Database Adapter - michaeltelford/wgit GitHub Wiki
The default database used with Wgit is MongDB. But this can be changed in theory to any database by writing a "database adapter" class and telling Wgit to use it instead.
The database methods required by Wgit are listed in the Wgit::Database::DatabaseAdapter
class (which should be inherited from - see below).
Steps
- Create a subclass of
Wgit::Database::DatabaseAdapter
and copy it's "interface" method stubs e.g.
my_adapter.rb
require 'wgit'
module Wgit::Database
class MyAdapter < DatabaseAdapter
# Paste the copied method stubs here...
end
end
- Implement the required methods by filling out the stubs with logic for your DB. If you miss a method implementation you'll likely get a
NotImplementedError
during usage with Wgit. - In your
main.rb
file do:
require 'wgit'
require_relative './my_adapter.rb'
Wgit::Database.adapter_class = Wgit::Database::MyAdapter
# Use Wgit with your new DB adapter implementation...
The order of statements above is important. First we require the wgit
gem which will use MongoDB as the default DB adapter. Then we require
our adapter and tell Wgit to use it instead.
Now that you've implemented the required methods for Wgit to call your adapter, you can use Wgit as you would with its default Wgit::Database.adapter_class
. Feel free to add additional methods to your adapter as needed for your own purposes.
Example
To see an example database adapter class, check out the Wgit::Database::InMemory
class.