From 91299f630bba09b93d4c0c7502f8e017a8e14f20 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Sun, 7 Sep 2008 12:54:10 -0700 Subject: [PATCH] added database factory method --- lib/couch_rest.rb | 10 +++++++++- spec/couchrest_spec.rb | 14 +++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/couch_rest.rb b/lib/couch_rest.rb index 1eb5d1e..66c7bfe 100644 --- a/lib/couch_rest.rb +++ b/lib/couch_rest.rb @@ -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 diff --git a/spec/couchrest_spec.rb b/spec/couchrest_spec.rb index 179c5d4..af69cdc 100644 --- a/spec/couchrest_spec.rb +++ b/spec/couchrest_spec.rb @@ -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'