db driver registration

This commit is contained in:
Brian J. Cardiff 2016-01-28 19:41:12 -03:00
parent 177e12bb60
commit cc1545a58e
3 changed files with 43 additions and 0 deletions

View file

@ -0,0 +1,19 @@
require "spec"
require "db"
class DummyDriver < DB::Driver
end
DB.register_driver "dummy", DummyDriver
describe DB::Driver do
it "should get driver class by name" do
DB.driver_class("dummy").should eq(DummyDriver)
end
it "should instantiate driver with options" do
driver = DB.driver "dummy", {"host": "localhost", "port": "1027"}
driver.options["host"].should eq("localhost")
driver.options["port"].should eq("1027")
end
end

16
src/db/db.cr Normal file
View file

@ -0,0 +1,16 @@
module DB
def self.driver_class(name) # : Driver.class
@@drivers.not_nil![name]
end
def self.register_driver(name, klass : Driver.class)
@@drivers ||= {} of String => Driver.class
@@drivers.not_nil![name] = klass
end
def self.driver(name, options)
driver_class(name).new(options)
end
end
require "./driver"

8
src/db/driver.cr Normal file
View file

@ -0,0 +1,8 @@
module DB
abstract class Driver
getter options
def initialize(@options)
end
end
end