added database factory method

This commit is contained in:
Chris Anderson 2008-09-07 12:54:10 -07:00
parent 5d56f1961f
commit 91299f630b
2 changed files with 22 additions and 2 deletions

View file

@ -12,10 +12,17 @@ class CouchRest
path = uri.path
uri.path = ''
cr = CouchRest.new(uri.to_s)
cr.create_db(path) rescue nil
cr.database!(path)
end
def self.database url
uri = URI.parse url
path = uri.path
uri.path = ''
cr = CouchRest.new(uri.to_s)
cr.database(path)
end
# list all databases on the server
def databases
CouchRest.get "#{@uri}/_all_dbs"
@ -27,6 +34,7 @@ class CouchRest
# creates the database if it doesn't exist
def database! name
create_db(path) rescue nil
CouchRest::Database.new(@uri, name)
end

View file

@ -40,7 +40,19 @@ describe CouchRest do
end
end
describe "ensuring a db exists" do
describe "easy initializing a database adapter" do
it "should be possible without an explicit CouchRest instantiation" do
db = CouchRest.database "http://localhost:5984/couchrest-test"
db.should be_an_instance_of(CouchRest::Database)
db.host.should == "http://localhost:5984"
end
it "should not create the database automatically" do
db = CouchRest.database "http://localhost:5984/couchrest-test"
lambda{db.info}.should raise_error(RestClient::ResourceNotFound)
end
end
describe "ensuring the db exists" do
it "should be super easy" do
db = CouchRest.database! "http://localhost:5984/couchrest-test-2"
db.info["db_name"].should == 'couchrest-test-2'