Implement #get! and #find! class methods

This commit is contained in:
Peter Williams 2011-05-23 11:02:08 -06:00
parent fcd9e2ba8e
commit f28cce1f0a
4 changed files with 43 additions and 2 deletions

View file

@ -88,6 +88,13 @@ module CouchRest
end end
alias :find :get alias :find :get
def get!(id)
doc = @klass.get!(id, @database)
doc.database = @database if doc && doc.respond_to?(:database)
doc
end
alias :find! :get!
# Views # Views
def has_view?(view) def has_view?(view)

View file

@ -86,9 +86,12 @@ module CouchRest
# id<String, Integer>:: Document ID # id<String, Integer>:: Document ID
# db<Database>:: optional option to pass a custom database to use # db<Database>:: optional option to pass a custom database to use
def get!(id, db = database) def get!(id, db = database)
raise "Missing or empty document ID" if id.to_s.empty? raise CouchRest::Model::DocumentNotFound if id.blank?
doc = db.get id doc = db.get id
build_from_database(doc) build_from_database(doc)
rescue RestClient::ResourceNotFound
raise CouchRest::Model::DocumentNotFound
end end
alias :find! :get! alias :find! :get!

View file

@ -19,5 +19,7 @@ module CouchRest
end end
end end
end end
class DocumentNotFound < Errors::CouchRestModelError; end
end end
end end

View file

@ -123,6 +123,35 @@ describe "Proxy Class" do
u.respond_to?(:database).should be_false u.respond_to?(:database).should be_false
end end
end end
describe "#get!" do
it "raises exception when passed a nil" do
expect { @us.get!(nil)}.to raise_error(CouchRest::Model::DocumentNotFound)
end
it "raises exception when passed an empty string " do
expect { @us.get!("")}.to raise_error(CouchRest::Model::DocumentNotFound)
end
it "raises exception when document with provided id does not exist" do
expect { @us.get!("thisisnotreallyadocumentid")}.to raise_error(CouchRest::Model::DocumentNotFound)
end
end
describe "#find!" do
it "raises exception when passed a nil" do
expect { @us.find!(nil)}.to raise_error(CouchRest::Model::DocumentNotFound)
end
it "raises exception when passed an empty string " do
expect { @us.find!("")}.to raise_error(CouchRest::Model::DocumentNotFound)
end
it "raises exception when document with provided id does not exist" do
expect { @us.find!("thisisnotreallyadocumentid")}.to raise_error(CouchRest::Model::DocumentNotFound)
end
end
# Sam Lown 2010-04-07 # Sam Lown 2010-04-07
# Removed as unclear why this should happen as before my changes # Removed as unclear why this should happen as before my changes
# this happend by accident, not explicitly. # this happend by accident, not explicitly.