diff --git a/lib/guacamole/collection.rb b/lib/guacamole/collection.rb index 1b1336e..4c02c37 100644 --- a/lib/guacamole/collection.rb +++ b/lib/guacamole/collection.rb @@ -115,13 +115,20 @@ def save(model) model end - # Delete a model from the database by key + # Delete a model from the database # - # @param [String] key The key of the model + # @param [String, Model] model_or_key The key of the model or a model # @return [String] The key - # @example Delete a podcast + # @example Delete a podcast by key # PodcastsCollection.delete(podcast.key) - def delete(key) + # @example Delete a podcast by model + # PodcastsCollection.delete(podcast) + def delete(model_or_key) + key = if model_or_key.respond_to? :key + model_or_key.key + else + model_or_key + end fetch_document(key).delete key end diff --git a/spec/unit/collection_spec.rb b/spec/unit/collection_spec.rb index 0bf2364..902e7f9 100644 --- a/spec/unit/collection_spec.rb +++ b/spec/unit/collection_spec.rb @@ -197,20 +197,35 @@ class TestCollection describe 'delete' do let(:document) { double('Document') } let(:key) { double('Key') } + let(:model) { double('Model', key: key) } before do allow(connection).to receive(:fetch).with(key).and_return(document) allow(document).to receive(:delete) end - it 'should delete the according document' do - expect(document).to receive(:delete) + context 'a key was provided' do + it 'should delete the according document' do + expect(document).to receive(:delete) - subject.delete key + subject.delete key + end + + it 'should return the according key' do + expect(subject.delete(key)).to eq key + end end - it 'should return the according key' do - expect(subject.delete(key)).to eq key + context 'a model was provided' do + it 'should delete the according document' do + expect(document).to receive(:delete) + + subject.delete model + end + + it 'should return the according key' do + expect(subject.delete(model)).to eq key + end end end