From 10806b675e39f5f0aefcf369caa9f95e4632da3e Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Thu, 2 Oct 2008 11:06:37 -0700 Subject: [PATCH] added CR::Model#destroy --- lib/couchrest/core/model.rb | 11 ++++++++++- spec/couchrest/core/model_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index 5015999..e6ec6b4 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -294,6 +294,15 @@ module CouchRest end end + def destroy + result = database.delete self + if result['ok'] + self['_rev'] = nil + self['_id'] = nil + end + result['ok'] + end + protected def create @@ -321,7 +330,7 @@ module CouchRest end include ::Extlib::Hook - register_instance_hooks :save, :create, :update #, :destroy + register_instance_hooks :save, :create, :update, :destroy end # class Model end # module CouchRest \ No newline at end of file diff --git a/spec/couchrest/core/model_spec.rb b/spec/couchrest/core/model_spec.rb index 153f933..91ac358 100644 --- a/spec/couchrest/core/model_spec.rb +++ b/spec/couchrest/core/model_spec.rb @@ -289,4 +289,25 @@ describe CouchRest::Model do view['rows'].find{|r|r['key'] == 'cool'}['value'].should == 3 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 \ No newline at end of file