mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
db driver registration
This commit is contained in:
parent
177e12bb60
commit
cc1545a58e
3 changed files with 43 additions and 0 deletions
19
spec/std/db/driver_spec.cr
Normal file
19
spec/std/db/driver_spec.cr
Normal 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
16
src/db/db.cr
Normal 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
8
src/db/driver.cr
Normal file
|
@ -0,0 +1,8 @@
|
|||
module DB
|
||||
abstract class Driver
|
||||
getter options
|
||||
|
||||
def initialize(@options)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue