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 end
# Load a document from the database by id # 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) 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 doc = db.get id
new(doc) new(doc)
end end

View file

@ -216,6 +216,15 @@ describe "ExtendedDocument" do
foundart = Article.get @art.id foundart = Article.get @art.id
foundart.title.should == "All About Getting" foundart.title.should == "All About Getting"
end 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 end
describe "getting a model with a subobjects array" do describe "getting a model with a subobjects array" do
@ -507,7 +516,7 @@ describe "ExtendedDocument" do
end end
it "should make it go away" do it "should make it go away" do
@dobj.destroy @dobj.destroy
lambda{Basic.get(@dobj.id)}.should raise_error lambda{Basic.get!(@dobj.id)}.should raise_error
end end
end end

View file

@ -168,8 +168,11 @@ describe "ExtendedDocument views" do
end end
things[0]["doc"]["title"].should =='aaa' things[0]["doc"]["title"].should =='aaa'
end end
it "should barf on get if no database given" do it "should return nil on get if no database given" do
lambda{Unattached.get("aaa")}.should raise_error 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 end
it "should get from specific database" do it "should get from specific database" do
u = Unattached.get(@first_id, @db) u = Unattached.get(@first_id, @db)