Skip to content

Commit

Permalink
Finishing up the commands with delete all and destroy all
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Oct 11, 2009
1 parent 9ba21ef commit 806cfb9
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/mongoid/commands.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require "mongoid/commands/create"
require "mongoid/commands/delete"
require "mongoid/commands/delete_all"
require "mongoid/commands/destroy"
require "mongoid/commands/destroy_all"
require "mongoid/commands/save"
14 changes: 14 additions & 0 deletions lib/mongoid/commands/delete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Mongoid #:nodoc:
module Commands #:nodoc:
class Delete #:nodoc:
# Performs a delete of the supplied +Document+ without any callbacks.
#
# Options:
#
# doc: A new +Document+ that is going to be deleted.
def self.execute(doc)
doc.collection.remove(:_id => doc.id)
end
end
end
end
20 changes: 20 additions & 0 deletions lib/mongoid/commands/delete_all.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Mongoid #:nodoc:
module Commands #:nodoc:
class DeleteAll #:nodoc:
# Performs a delete of the all the +Documents+ that match the criteria
# supplied.
#
# Options:
#
# params: A set of conditions to find the +Documents+ by.
# klass: The class of the +Document+ to execute the find on.
#
# Example:
#
# <tt>DeleteAll.execute(Person, :conditions => { :field => "value" })</tt>
def self.execute(klass, params)
klass.find(:all, params).each { |doc| Delete.execute(doc) }
end
end
end
end
2 changes: 1 addition & 1 deletion lib/mongoid/commands/destroy.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Mongoid #:nodoc:
module Commands #:nodoc:
class Destroy #:nodoc:
# Performs a destroy of the supplied Document, with the necessary
# Performs a destroy of the supplied +Document+, with the necessary
# callbacks. It then deletes the record from the collection.
#
# Options:
Expand Down
20 changes: 20 additions & 0 deletions lib/mongoid/commands/destroy_all.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Mongoid #:nodoc:
module Commands #:nodoc:
class DestroyAll #:nodoc:
# Performs a destroy of the all the +Documents+ that match the criteria
# supplied. Will execute all the destroy callbacks for each +Document+.
#
# Options:
#
# params: A set of conditions to find the +Documents+ by.
# klass: The class of the +Document+ to execute the find on.
#
# Example:
#
# <tt>DestroyAll.execute(Person, :conditions => { :field => "value" })</tt>
def self.execute(klass, params)
klass.find(:all, params).each { |doc| Destroy.execute(doc) }
end
end
end
end
2 changes: 1 addition & 1 deletion lib/mongoid/commands/save.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Mongoid #:nodoc:
module Commands #:nodoc:
class Save #:nodoc:
# Performs a save of the supplied Document, handling all associated
# Performs a save of the supplied +Document+, handling all associated
# callbacks and validation.
#
# Options:
Expand Down
22 changes: 22 additions & 0 deletions spec/unit/mongoid/commands/delete_all_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")

describe Mongoid::Commands::DeleteAll do

describe "#execute" do

before do
@doc = mock
@docs = [@doc]
@klass = mock
@conditions = { :conditions => { :title => "Sir" } }
end

it "destroys each document that the criteria finds" do
@klass.expects(:find).with(:all, @conditions).returns(@docs)
Mongoid::Commands::Delete.expects(:execute).with(@doc)
Mongoid::Commands::DeleteAll.execute(@klass, @conditions)
end

end

end
19 changes: 19 additions & 0 deletions spec/unit/mongoid/commands/delete_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")

describe Mongoid::Commands::Delete do

describe "#execute" do

before do
@collection = mock
@document = stub(:collection => @collection, :id => "1")
end

it "removes the document from its collection" do
@collection.expects(:remove).with({ :_id => @document.id })
Mongoid::Commands::Delete.execute(@document)
end

end

end
22 changes: 22 additions & 0 deletions spec/unit/mongoid/commands/destroy_all_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")

describe Mongoid::Commands::DestroyAll do

describe "#execute" do

before do
@doc = mock
@docs = [@doc]
@klass = mock
@conditions = { :conditions => { :title => "Sir" } }
end

it "destroys each document that the criteria finds" do
@klass.expects(:find).with(:all, @conditions).returns(@docs)
Mongoid::Commands::Destroy.expects(:execute).with(@doc)
Mongoid::Commands::DestroyAll.execute(@klass, @conditions)
end

end

end

0 comments on commit 806cfb9

Please sign in to comment.