From 5cd2eaf18ae20ad9e21a77135627cd2b764a8c4d Mon Sep 17 00:00:00 2001 From: Matt Aimonetti Date: Wed, 22 Jul 2009 16:05:55 -0700 Subject: [PATCH] made get not raise an exception anymore, use get! instead if you need to raise exceptions. * ExtendedDocument.get doesn't raise an exception anymore. If no documents are found nil is returned. * ExtendedDocument.get! works the say #get used to work and will raise an exception if a document isn't found. --- lib/couchrest/mixins/document_queries.rb | 31 +++++++++++++++++++ spec/couchrest/more/extended_doc_spec.rb | 11 ++++++- spec/couchrest/more/extended_doc_view_spec.rb | 7 +++-- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/couchrest/mixins/document_queries.rb b/lib/couchrest/mixins/document_queries.rb index defff3d..a1312d9 100644 --- a/lib/couchrest/mixins/document_queries.rb +++ b/lib/couchrest/mixins/document_queries.rb @@ -39,7 +39,38 @@ module CouchRest end # Load a document from the database by id + # No exceptions will be raised if the document isn't found + # + # ==== Returns + # Object:: if the document was found + # or + # Nil:: + # + # === Parameters + # id:: Document ID + # db:: optional option to pass a custom database to use def get(id, db = database) + begin + doc = db.get id + rescue + nil + else + new(doc) + end + end + + # Load a document from the database by id + # An exception will be raised if the document isn't found + # + # ==== Returns + # Object:: if the document was found + # or + # Exception + # + # === Parameters + # id:: Document ID + # db:: optional option to pass a custom database to use + def get!(id, db = database) doc = db.get id new(doc) end diff --git a/spec/couchrest/more/extended_doc_spec.rb b/spec/couchrest/more/extended_doc_spec.rb index 5d71d7f..f3097ff 100644 --- a/spec/couchrest/more/extended_doc_spec.rb +++ b/spec/couchrest/more/extended_doc_spec.rb @@ -216,6 +216,15 @@ describe "ExtendedDocument" do foundart = Article.get @art.id foundart.title.should == "All About Getting" end + + it "should return nil if `get` is used and the document doesn't exist" do + foundart = Article.get 'matt aimonetti' + foundart.should be_nil + end + + it "should raise an error if `get!` is used and the document doesn't exist" do + lambda{foundart = Article.get!('matt aimonetti')}.should raise_error + end end describe "getting a model with a subobjects array" do @@ -507,7 +516,7 @@ describe "ExtendedDocument" do end it "should make it go away" do @dobj.destroy - lambda{Basic.get(@dobj.id)}.should raise_error + lambda{Basic.get!(@dobj.id)}.should raise_error end end diff --git a/spec/couchrest/more/extended_doc_view_spec.rb b/spec/couchrest/more/extended_doc_view_spec.rb index 1444f4b..be6056e 100644 --- a/spec/couchrest/more/extended_doc_view_spec.rb +++ b/spec/couchrest/more/extended_doc_view_spec.rb @@ -168,8 +168,11 @@ describe "ExtendedDocument views" do end things[0]["doc"]["title"].should =='aaa' end - it "should barf on get if no database given" do - lambda{Unattached.get("aaa")}.should raise_error + it "should return nil on get if no database given" do + Unattached.get("aaa").should be_nil + end + it "should barf on get! if no database given" do + lambda{Unattached.get!("aaa")}.should raise_error end it "should get from specific database" do u = Unattached.get(@first_id, @db)