Skip to content

Commit

Permalink
Delete alternatively takes a model
Browse files Browse the repository at this point in the history
  • Loading branch information
moonglum committed Nov 9, 2013
1 parent aeb0d8a commit 03b8db4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
15 changes: 11 additions & 4 deletions lib/guacamole/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions spec/unit/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 03b8db4

Please sign in to comment.