diff --git a/lib/couch_rest.rb b/lib/couch_rest.rb index f07114d..1eb5d1e 100644 --- a/lib/couch_rest.rb +++ b/lib/couch_rest.rb @@ -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}/" diff --git a/lib/database.rb b/lib/database.rb index 7d0bfec..7482fd8 100644 --- a/lib/database.rb +++ b/lib/database.rb @@ -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 diff --git a/spec/couchrest_spec.rb b/spec/couchrest_spec.rb index 310ced5..179c5d4 100644 --- a/spec/couchrest_spec.rb +++ b/spec/couchrest_spec.rb @@ -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)