added easy method for ensuring databases exist
This commit is contained in:
parent
1eac462612
commit
5d56f1961f
3 changed files with 28 additions and 0 deletions
|
@ -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}/"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue