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.
This commit is contained in:
Matt Aimonetti 2009-07-22 16:05:55 -07:00
parent 87d246d30e
commit 5cd2eaf18a
3 changed files with 46 additions and 3 deletions

View file

@ -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<String, Integer>:: Document ID
# db<Database>:: 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<String, Integer>:: Document ID
# db<Database>:: optional option to pass a custom database to use
def get!(id, db = database)
doc = db.get id
new(doc)
end

View file

@ -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

View file

@ -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)