added easy method for ensuring databases exist

This commit is contained in:
Chris Anderson 2008-09-07 12:43:13 -07:00
parent 1eac462612
commit 5d56f1961f
3 changed files with 28 additions and 0 deletions

View file

@ -4,6 +4,18 @@ class CouchRest
@uri = server
end
# ensure that a database exists
# creates it if it isn't already there
# returns it after it's been created
def self.database! url
uri = URI.parse url
path = uri.path
uri.path = ''
cr = CouchRest.new(uri.to_s)
cr.create_db(path) rescue nil
cr.database!(path)
end
# list all databases on the server
def databases
CouchRest.get "#{@uri}/_all_dbs"
@ -13,6 +25,11 @@ class CouchRest
CouchRest::Database.new(@uri, name)
end
# creates the database if it doesn't exist
def database! name
CouchRest::Database.new(@uri, name)
end
# get the welcome message
def info
CouchRest.get "#{@uri}/"

View file

@ -14,6 +14,10 @@ class CouchRest
@root
end
def info
CouchRest.get @root
end
def documents params = nil
url = CouchRest.paramify_url "#{@root}/_all_docs", params
CouchRest.get url

View file

@ -40,6 +40,13 @@ describe CouchRest do
end
end
describe "ensuring a 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'
end
end
describe "successfully creating a database" do
it "should start without a database" do
@cr.databases.should_not include(TESTDB)