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 @uri = server
end 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 # list all databases on the server
def databases def databases
CouchRest.get "#{@uri}/_all_dbs" CouchRest.get "#{@uri}/_all_dbs"
@ -13,6 +25,11 @@ class CouchRest
CouchRest::Database.new(@uri, name) CouchRest::Database.new(@uri, name)
end end
# creates the database if it doesn't exist
def database! name
CouchRest::Database.new(@uri, name)
end
# get the welcome message # get the welcome message
def info def info
CouchRest.get "#{@uri}/" CouchRest.get "#{@uri}/"

View file

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

View file

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