added CR::Model#destroy

This commit is contained in:
Chris Anderson 2008-10-02 11:06:37 -07:00
parent f0be835f79
commit 10806b675e
2 changed files with 31 additions and 1 deletions

View file

@ -294,6 +294,15 @@ module CouchRest
end end
end end
def destroy
result = database.delete self
if result['ok']
self['_rev'] = nil
self['_id'] = nil
end
result['ok']
end
protected protected
def create def create
@ -321,7 +330,7 @@ module CouchRest
end end
include ::Extlib::Hook include ::Extlib::Hook
register_instance_hooks :save, :create, :update #, :destroy register_instance_hooks :save, :create, :update, :destroy
end # class Model end # class Model
end # module CouchRest end # module CouchRest

View file

@ -289,4 +289,25 @@ describe CouchRest::Model do
view['rows'].find{|r|r['key'] == 'cool'}['value'].should == 3 view['rows'].find{|r|r['key'] == 'cool'}['value'].should == 3
end end
end end
describe "destroying an instance" do
before(:each) do
@obj = Basic.new
@obj.save.should == true
end
it "should return true" do
result = @obj.destroy
result.should == true
end
it "should be resavable" do
@obj.destroy
@obj.rev.should be_nil
@obj.id.should be_nil
@obj.save.should == true
end
it "should make it go away" do
@obj.destroy
lambda{Basic.get(@obj.id)}.should raise_error
end
end
end end