mongoid 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/mongoid.rb +12 -12
- data/lib/mongoid/commands.rb +105 -0
- data/lib/mongoid/commands/create.rb +20 -0
- data/lib/mongoid/commands/delete.rb +14 -0
- data/lib/mongoid/commands/delete_all.rb +20 -0
- data/lib/mongoid/commands/destroy.rb +17 -0
- data/lib/mongoid/commands/destroy_all.rb +20 -0
- data/lib/mongoid/commands/save.rb +22 -0
- data/lib/mongoid/document.rb +4 -45
- data/mongoid.gemspec +21 -2
- data/spec/spec_helper.rb +1 -0
- data/spec/unit/mongoid/commands/create_spec.rb +30 -0
- data/spec/unit/mongoid/commands/delete_all_spec.rb +22 -0
- data/spec/unit/mongoid/commands/delete_spec.rb +19 -0
- data/spec/unit/mongoid/commands/destroy_all_spec.rb +22 -0
- data/spec/unit/mongoid/commands/destroy_spec.rb +27 -0
- data/spec/unit/mongoid/commands/save_spec.rb +75 -0
- data/spec/unit/mongoid/document_spec.rb +2 -2
- metadata +21 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
data/lib/mongoid.rb
CHANGED
@@ -34,28 +34,28 @@ require "mongo"
|
|
34
34
|
require "mongoid/associations"
|
35
35
|
require "mongoid/criteria"
|
36
36
|
require "mongoid/extensions"
|
37
|
+
require "mongoid/commands"
|
37
38
|
require "mongoid/document"
|
38
39
|
|
39
40
|
module Mongoid
|
40
41
|
|
41
|
-
#
|
42
|
-
class NoConnectionError < RuntimeError
|
43
|
-
end
|
42
|
+
# Raised when the database connection has not been set up.
|
43
|
+
class NoConnectionError < RuntimeError; end
|
44
44
|
|
45
|
-
#
|
45
|
+
# Raised when :document_class is not provided in the attributes
|
46
46
|
# hash when creating a new Document
|
47
|
-
class ClassNotProvidedError < RuntimeError
|
48
|
-
end
|
47
|
+
class ClassNotProvidedError < RuntimeError; end
|
49
48
|
|
50
|
-
#
|
49
|
+
# Raised when an association is defined on the class, but the
|
51
50
|
# attribute in the hash is not an Array or Hash.
|
52
|
-
class TypeMismatchError < RuntimeError
|
53
|
-
end
|
51
|
+
class TypeMismatchError < RuntimeError; end
|
54
52
|
|
55
|
-
#
|
53
|
+
# Raised when an association is defined that is not valid. Must
|
56
54
|
# be belongs_to, has_many, has_one
|
57
|
-
class InvalidAssociationError < RuntimeError
|
58
|
-
|
55
|
+
class InvalidAssociationError < RuntimeError; end
|
56
|
+
|
57
|
+
# Raised when a persisence method ending in ! fails validation.
|
58
|
+
class ValidationsError < RuntimeError; end
|
59
59
|
|
60
60
|
# Connect to the database name supplied. This should be run
|
61
61
|
# for initial setup, potentially in a rails initializer.
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require "mongoid/commands/create"
|
2
|
+
require "mongoid/commands/delete"
|
3
|
+
require "mongoid/commands/delete_all"
|
4
|
+
require "mongoid/commands/destroy"
|
5
|
+
require "mongoid/commands/destroy_all"
|
6
|
+
require "mongoid/commands/save"
|
7
|
+
|
8
|
+
module Mongoid #:nodoc:
|
9
|
+
|
10
|
+
# This module is included in the +Document+ to provide all the persistence
|
11
|
+
# methods required on the +Document+ object and class. The following methods
|
12
|
+
# are provided:
|
13
|
+
#
|
14
|
+
# <tt>create</tt>,
|
15
|
+
# <tt>create!</tt>,
|
16
|
+
# <tt>delete</tt>,
|
17
|
+
# <tt>delete_all</tt>,
|
18
|
+
# <tt>destroy</tt>,
|
19
|
+
# <tt>destroy_all</tt>,
|
20
|
+
# <tt>save</tt>,
|
21
|
+
# <tt>save!</tt>,
|
22
|
+
# <tt>update_attributes</tt>,
|
23
|
+
# <tt>update_attributes!</tt>
|
24
|
+
#
|
25
|
+
# These methods will delegate to their respective commands.
|
26
|
+
module Commands
|
27
|
+
def self.included(base)
|
28
|
+
base.class_eval do
|
29
|
+
include InstanceMethods
|
30
|
+
extend ClassMethods
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module InstanceMethods
|
35
|
+
|
36
|
+
# Delete the +Document+ from the database. Delegates to the Delete
|
37
|
+
# command.
|
38
|
+
def delete
|
39
|
+
Delete.execute(self)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Destroy the +Document+. Delegates to the Destroy command.
|
43
|
+
def destroy
|
44
|
+
Destroy.execute(self)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Save the +Document+. Delegates to the Save command.
|
48
|
+
def save
|
49
|
+
Save.execute(self)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Save the +Document+. Delegates to the Save command. If the command
|
53
|
+
# returns false then a +ValidationError+ will be raised.
|
54
|
+
def save!
|
55
|
+
Save.execute(self) || (raise ValidationsError)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Update the attributes of the +Document+. Will call save after the
|
59
|
+
# attributes have been updated.
|
60
|
+
def update_attributes(attributes = {})
|
61
|
+
self.attributes = attributes; save
|
62
|
+
end
|
63
|
+
|
64
|
+
# Update the attributes of the +Document+. Will call save! after the
|
65
|
+
# attributes have been updated, causing a +ValidationError+ if the
|
66
|
+
# +Document+ failed validation.
|
67
|
+
def update_attributes!(attribtues = {})
|
68
|
+
self.attributes = attributes; save!
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
module ClassMethods
|
74
|
+
|
75
|
+
# Create a new +Document+ with the supplied attributes. Will delegate to
|
76
|
+
# the Create command.
|
77
|
+
def create(attributes = {})
|
78
|
+
Create.execute(new(attributes))
|
79
|
+
end
|
80
|
+
|
81
|
+
# Create a new +Document+ with the supplied attributes. Will delegate to
|
82
|
+
# the Create command or raise +ValidationError+ if the save failed
|
83
|
+
# validation.
|
84
|
+
def create!(attributes = {})
|
85
|
+
document = Create.execute(new(attributes))
|
86
|
+
raise ValidationsError unless document.errors.empty?
|
87
|
+
return document
|
88
|
+
end
|
89
|
+
|
90
|
+
# Delete all the +Documents+ in the database given the supplied
|
91
|
+
# conditions.
|
92
|
+
def delete_all(conditions = {})
|
93
|
+
DeleteAll.execute(self, conditions)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Destroy all the +Documents+ in the database given the supplied
|
97
|
+
# conditions.
|
98
|
+
def destroy_all(conditions = {})
|
99
|
+
DestroyAll.execute(self, conditions)
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Mongoid #:nodoc:
|
2
|
+
module Commands
|
3
|
+
class Create #:nodoc:
|
4
|
+
# Performs a create of the supplied Document, with the necessary
|
5
|
+
# callbacks. It then delegates to the Save command.
|
6
|
+
#
|
7
|
+
# Options:
|
8
|
+
#
|
9
|
+
# doc: A new +Document+ that is going to be persisted.
|
10
|
+
#
|
11
|
+
# Returns: +Document+.
|
12
|
+
def self.execute(doc)
|
13
|
+
doc.run_callbacks :before_create
|
14
|
+
Save.execute(doc)
|
15
|
+
doc.run_callbacks :after_create
|
16
|
+
return doc
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Mongoid #:nodoc:
|
2
|
+
module Commands
|
3
|
+
class Delete #:nodoc:
|
4
|
+
# Performs a delete of the supplied +Document+ without any callbacks.
|
5
|
+
#
|
6
|
+
# Options:
|
7
|
+
#
|
8
|
+
# doc: A new +Document+ that is going to be deleted.
|
9
|
+
def self.execute(doc)
|
10
|
+
doc.collection.remove(:_id => doc.id)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Mongoid #:nodoc:
|
2
|
+
module Commands
|
3
|
+
class DeleteAll #:nodoc:
|
4
|
+
# Performs a delete of the all the +Documents+ that match the criteria
|
5
|
+
# supplied.
|
6
|
+
#
|
7
|
+
# Options:
|
8
|
+
#
|
9
|
+
# params: A set of conditions to find the +Documents+ by.
|
10
|
+
# klass: The class of the +Document+ to execute the find on.
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# <tt>DeleteAll.execute(Person, :conditions => { :field => "value" })</tt>
|
15
|
+
def self.execute(klass, params)
|
16
|
+
klass.find(:all, params).each { |doc| Delete.execute(doc) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Mongoid #:nodoc:
|
2
|
+
module Commands
|
3
|
+
class Destroy #:nodoc:
|
4
|
+
# Performs a destroy of the supplied +Document+, with the necessary
|
5
|
+
# callbacks. It then deletes the record from the collection.
|
6
|
+
#
|
7
|
+
# Options:
|
8
|
+
#
|
9
|
+
# doc: A new +Document+ that is going to be destroyed.
|
10
|
+
def self.execute(doc)
|
11
|
+
doc.run_callbacks :before_destroy
|
12
|
+
doc.collection.remove(:_id => doc.id)
|
13
|
+
doc.run_callbacks :after_destroy
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Mongoid #:nodoc:
|
2
|
+
module Commands
|
3
|
+
class DestroyAll #:nodoc:
|
4
|
+
# Performs a destroy of the all the +Documents+ that match the criteria
|
5
|
+
# supplied. Will execute all the destroy callbacks for each +Document+.
|
6
|
+
#
|
7
|
+
# Options:
|
8
|
+
#
|
9
|
+
# params: A set of conditions to find the +Documents+ by.
|
10
|
+
# klass: The class of the +Document+ to execute the find on.
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# <tt>DestroyAll.execute(Person, :conditions => { :field => "value" })</tt>
|
15
|
+
def self.execute(klass, params)
|
16
|
+
klass.find(:all, params).each { |doc| Destroy.execute(doc) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Mongoid #:nodoc:
|
2
|
+
module Commands
|
3
|
+
class Save #:nodoc:
|
4
|
+
# Performs a save of the supplied +Document+, handling all associated
|
5
|
+
# callbacks and validation.
|
6
|
+
#
|
7
|
+
# Options:
|
8
|
+
#
|
9
|
+
# doc: A +Document+ that is going to be persisted.
|
10
|
+
#
|
11
|
+
# Returns: +Document+ if validation passes, +false+ if not.
|
12
|
+
def self.execute(doc)
|
13
|
+
return false unless doc.valid?
|
14
|
+
doc.run_callbacks :before_save
|
15
|
+
parent = doc.parent
|
16
|
+
parent ? Save.execute(parent) : doc.collection.save(doc.attributes)
|
17
|
+
doc.run_callbacks :after_save
|
18
|
+
return doc
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/mongoid/document.rb
CHANGED
@@ -2,16 +2,19 @@ module Mongoid #:nodoc:
|
|
2
2
|
class Document #:nodoc:
|
3
3
|
include ActiveSupport::Callbacks
|
4
4
|
include Validatable
|
5
|
+
include Commands
|
5
6
|
|
6
7
|
AGGREGATE_REDUCE = "function(obj, prev) { prev.count++; }"
|
7
8
|
GROUP_BY_REDUCE = "function(obj, prev) { prev.group.push(obj); }"
|
8
9
|
|
9
|
-
|
10
|
+
attr_accessor :attributes, :parent
|
10
11
|
|
11
12
|
define_callbacks \
|
12
13
|
:after_create,
|
14
|
+
:after_destroy,
|
13
15
|
:after_save,
|
14
16
|
:before_create,
|
17
|
+
:before_destroy,
|
15
18
|
:before_save
|
16
19
|
|
17
20
|
class << self
|
@@ -33,17 +36,6 @@ module Mongoid #:nodoc:
|
|
33
36
|
@collection ||= Mongoid.database.collection(@collection_name)
|
34
37
|
end
|
35
38
|
|
36
|
-
# Create a new Document with the supplied attribtues, and insert it into the database.
|
37
|
-
def create(attributes = {})
|
38
|
-
new(attributes).save(true)
|
39
|
-
end
|
40
|
-
|
41
|
-
# Deletes all records from the collection. Will actually call drop on the
|
42
|
-
# Mongo::Collection for efficiency.
|
43
|
-
def destroy_all
|
44
|
-
collection.drop
|
45
|
-
end
|
46
|
-
|
47
39
|
# Defines all the fields that are accessable on the Document
|
48
40
|
# For each field that is defined, a getter and setter will be
|
49
41
|
# added as an instance method to the Document.
|
@@ -135,11 +127,6 @@ module Mongoid #:nodoc:
|
|
135
127
|
self.class.collection
|
136
128
|
end
|
137
129
|
|
138
|
-
# Delete this Document from the database.
|
139
|
-
def destroy
|
140
|
-
collection.remove(:_id => id)
|
141
|
-
end
|
142
|
-
|
143
130
|
# Get the Mongo::ObjectID associated with this object.
|
144
131
|
# This is in essence the primary key.
|
145
132
|
def id
|
@@ -158,39 +145,11 @@ module Mongoid #:nodoc:
|
|
158
145
|
@attributes[:_id].nil?
|
159
146
|
end
|
160
147
|
|
161
|
-
# Set the parent to this document.
|
162
|
-
def parent=(document)
|
163
|
-
@parent = document
|
164
|
-
end
|
165
|
-
|
166
|
-
# Save this document to the database. If this document is the root document
|
167
|
-
# in the object graph, it will save itself, and return self. If the
|
168
|
-
# document is embedded within another document, or is multiple levels down
|
169
|
-
# the tree, the root object will get saved, and return itself.
|
170
|
-
def save(creating = false)
|
171
|
-
if @parent
|
172
|
-
@parent.save
|
173
|
-
else
|
174
|
-
return false unless valid?
|
175
|
-
run_callbacks(:before_create) if creating
|
176
|
-
run_callbacks(:before_save)
|
177
|
-
collection.save(@attributes)
|
178
|
-
run_callbacks(:after_create) if creating
|
179
|
-
run_callbacks(:after_save)
|
180
|
-
creating ? self : true
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
148
|
# Returns the id of the Document
|
185
149
|
def to_param
|
186
150
|
id.to_s
|
187
151
|
end
|
188
152
|
|
189
|
-
# Update the attributes of this Document and return true
|
190
|
-
def update_attributes(attributes)
|
191
|
-
@attributes = attributes.symbolize_keys!; save; true
|
192
|
-
end
|
193
|
-
|
194
153
|
private
|
195
154
|
|
196
155
|
class << self
|
data/mongoid.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "0.4.
|
8
|
+
s.version = "0.4.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Durran Jordan"]
|
12
|
-
s.date = %q{2009-10-
|
12
|
+
s.date = %q{2009-10-11}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.textile"
|
@@ -28,6 +28,13 @@ Gem::Specification.new do |s|
|
|
28
28
|
"lib/mongoid/associations/factory.rb",
|
29
29
|
"lib/mongoid/associations/has_many_association.rb",
|
30
30
|
"lib/mongoid/associations/has_one_association.rb",
|
31
|
+
"lib/mongoid/commands.rb",
|
32
|
+
"lib/mongoid/commands/create.rb",
|
33
|
+
"lib/mongoid/commands/delete.rb",
|
34
|
+
"lib/mongoid/commands/delete_all.rb",
|
35
|
+
"lib/mongoid/commands/destroy.rb",
|
36
|
+
"lib/mongoid/commands/destroy_all.rb",
|
37
|
+
"lib/mongoid/commands/save.rb",
|
31
38
|
"lib/mongoid/criteria.rb",
|
32
39
|
"lib/mongoid/document.rb",
|
33
40
|
"lib/mongoid/extensions.rb",
|
@@ -42,6 +49,12 @@ Gem::Specification.new do |s|
|
|
42
49
|
"spec/unit/mongoid/associations/factory_spec.rb",
|
43
50
|
"spec/unit/mongoid/associations/has_many_association_spec.rb",
|
44
51
|
"spec/unit/mongoid/associations/has_one_association_spec.rb",
|
52
|
+
"spec/unit/mongoid/commands/create_spec.rb",
|
53
|
+
"spec/unit/mongoid/commands/delete_all_spec.rb",
|
54
|
+
"spec/unit/mongoid/commands/delete_spec.rb",
|
55
|
+
"spec/unit/mongoid/commands/destroy_all_spec.rb",
|
56
|
+
"spec/unit/mongoid/commands/destroy_spec.rb",
|
57
|
+
"spec/unit/mongoid/commands/save_spec.rb",
|
45
58
|
"spec/unit/mongoid/criteria_spec.rb",
|
46
59
|
"spec/unit/mongoid/document_spec.rb",
|
47
60
|
"spec/unit/mongoid/extensions/array/conversions_spec.rb",
|
@@ -60,6 +73,12 @@ Gem::Specification.new do |s|
|
|
60
73
|
"spec/unit/mongoid/associations/factory_spec.rb",
|
61
74
|
"spec/unit/mongoid/associations/has_many_association_spec.rb",
|
62
75
|
"spec/unit/mongoid/associations/has_one_association_spec.rb",
|
76
|
+
"spec/unit/mongoid/commands/create_spec.rb",
|
77
|
+
"spec/unit/mongoid/commands/delete_all_spec.rb",
|
78
|
+
"spec/unit/mongoid/commands/delete_spec.rb",
|
79
|
+
"spec/unit/mongoid/commands/destroy_all_spec.rb",
|
80
|
+
"spec/unit/mongoid/commands/destroy_spec.rb",
|
81
|
+
"spec/unit/mongoid/commands/save_spec.rb",
|
63
82
|
"spec/unit/mongoid/criteria_spec.rb",
|
64
83
|
"spec/unit/mongoid/document_spec.rb",
|
65
84
|
"spec/unit/mongoid/extensions/array/conversions_spec.rb",
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
+
|
3
|
+
describe Mongoid::Commands::Create do
|
4
|
+
|
5
|
+
describe "#execute" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@document = stub(:run_callbacks)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "executes a save command" do
|
12
|
+
Mongoid::Commands::Save.expects(:execute).with(@document).returns(@document)
|
13
|
+
Mongoid::Commands::Create.execute(@document)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "runs the before and after create callbacks" do
|
17
|
+
@document.expects(:run_callbacks).with(:before_create)
|
18
|
+
Mongoid::Commands::Save.expects(:execute).with(@document).returns(@document)
|
19
|
+
@document.expects(:run_callbacks).with(:after_create)
|
20
|
+
Mongoid::Commands::Create.execute(@document)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns the document" do
|
24
|
+
Mongoid::Commands::Save.expects(:execute).with(@document).returns(@document)
|
25
|
+
Mongoid::Commands::Create.execute(@document).should == @document
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
+
|
3
|
+
describe Mongoid::Commands::DeleteAll do
|
4
|
+
|
5
|
+
describe "#execute" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@doc = mock
|
9
|
+
@docs = [@doc]
|
10
|
+
@klass = mock
|
11
|
+
@conditions = { :conditions => { :title => "Sir" } }
|
12
|
+
end
|
13
|
+
|
14
|
+
it "destroys each document that the criteria finds" do
|
15
|
+
@klass.expects(:find).with(:all, @conditions).returns(@docs)
|
16
|
+
Mongoid::Commands::Delete.expects(:execute).with(@doc)
|
17
|
+
Mongoid::Commands::DeleteAll.execute(@klass, @conditions)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
+
|
3
|
+
describe Mongoid::Commands::Delete do
|
4
|
+
|
5
|
+
describe "#execute" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@collection = mock
|
9
|
+
@document = stub(:collection => @collection, :id => "1")
|
10
|
+
end
|
11
|
+
|
12
|
+
it "removes the document from its collection" do
|
13
|
+
@collection.expects(:remove).with({ :_id => @document.id })
|
14
|
+
Mongoid::Commands::Delete.execute(@document)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
+
|
3
|
+
describe Mongoid::Commands::DestroyAll do
|
4
|
+
|
5
|
+
describe "#execute" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@doc = mock
|
9
|
+
@docs = [@doc]
|
10
|
+
@klass = mock
|
11
|
+
@conditions = { :conditions => { :title => "Sir" } }
|
12
|
+
end
|
13
|
+
|
14
|
+
it "destroys each document that the criteria finds" do
|
15
|
+
@klass.expects(:find).with(:all, @conditions).returns(@docs)
|
16
|
+
Mongoid::Commands::Destroy.expects(:execute).with(@doc)
|
17
|
+
Mongoid::Commands::DestroyAll.execute(@klass, @conditions)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
+
|
3
|
+
describe Mongoid::Commands::Destroy do
|
4
|
+
|
5
|
+
describe "#execute" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@collection = stub_everything
|
9
|
+
@document = stub(:run_callbacks => true,
|
10
|
+
:collection => @collection,
|
11
|
+
:id => "1")
|
12
|
+
end
|
13
|
+
|
14
|
+
it "runs the before and after destroy callbacks" do
|
15
|
+
@document.expects(:run_callbacks).with(:before_destroy)
|
16
|
+
@document.expects(:run_callbacks).with(:after_destroy)
|
17
|
+
Mongoid::Commands::Destroy.execute(@document)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "removes the document from its collection" do
|
21
|
+
@collection.expects(:remove).with({ :_id => @document.id })
|
22
|
+
Mongoid::Commands::Destroy.execute(@document)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
+
|
3
|
+
describe Mongoid::Commands::Save do
|
4
|
+
|
5
|
+
describe "#execute" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@parent_collection = stub(:save)
|
9
|
+
@doc_collection = stub(:save)
|
10
|
+
@parent = stub(:collection => @parent_collection,
|
11
|
+
:valid? => true,
|
12
|
+
:run_callbacks => true,
|
13
|
+
:parent => nil,
|
14
|
+
:attributes => {})
|
15
|
+
@document = stub(:collection => @doc_collection,
|
16
|
+
:run_callbacks => true,
|
17
|
+
:parent => @parent,
|
18
|
+
:attributes => {})
|
19
|
+
end
|
20
|
+
|
21
|
+
context "when document is valid" do
|
22
|
+
|
23
|
+
before do
|
24
|
+
@document.expects(:valid?).returns(true)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "runs the before and after callbacks" do
|
28
|
+
@document.expects(:run_callbacks).with(:before_save)
|
29
|
+
@document.expects(:run_callbacks).with(:after_save)
|
30
|
+
Mongoid::Commands::Save.execute(@document)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "returns the document" do
|
34
|
+
Mongoid::Commands::Save.execute(@document).should == @document
|
35
|
+
end
|
36
|
+
|
37
|
+
context "when the document has a parent" do
|
38
|
+
|
39
|
+
it "executes a save on the parent" do
|
40
|
+
@parent_collection.expects(:save).with(@parent.attributes)
|
41
|
+
Mongoid::Commands::Save.execute(@document)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
context "when the document has no parent" do
|
47
|
+
|
48
|
+
before do
|
49
|
+
@document.expects(:parent).returns(nil)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "calls save on the document collection" do
|
53
|
+
@doc_collection.expects(:save).with(@document.attributes)
|
54
|
+
Mongoid::Commands::Save.execute(@document)
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when document is invalid" do
|
62
|
+
|
63
|
+
before do
|
64
|
+
@document.expects(:valid?).returns(false)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "returns false" do
|
68
|
+
Mongoid::Commands::Save.execute(@document).should be_false
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
@@ -85,8 +85,8 @@ describe Mongoid::Document do
|
|
85
85
|
|
86
86
|
it "returns nil" do
|
87
87
|
id = Mongo::ObjectID.new
|
88
|
-
@collection.expects(:remove).with(:_id => id)
|
89
88
|
person = Person.new(:_id => id)
|
89
|
+
Mongoid::Commands::Destroy.expects(:execute).with(person)
|
90
90
|
person.destroy.should be_nil
|
91
91
|
end
|
92
92
|
|
@@ -97,7 +97,7 @@ describe Mongoid::Document do
|
|
97
97
|
describe "#destroy_all" do
|
98
98
|
|
99
99
|
it "deletes all documents from the collection" do
|
100
|
-
|
100
|
+
Mongoid::Commands::DestroyAll.expects(:execute).with(Person, {})
|
101
101
|
Person.destroy_all
|
102
102
|
end
|
103
103
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-11 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -74,6 +74,13 @@ files:
|
|
74
74
|
- lib/mongoid/associations/factory.rb
|
75
75
|
- lib/mongoid/associations/has_many_association.rb
|
76
76
|
- lib/mongoid/associations/has_one_association.rb
|
77
|
+
- lib/mongoid/commands.rb
|
78
|
+
- lib/mongoid/commands/create.rb
|
79
|
+
- lib/mongoid/commands/delete.rb
|
80
|
+
- lib/mongoid/commands/delete_all.rb
|
81
|
+
- lib/mongoid/commands/destroy.rb
|
82
|
+
- lib/mongoid/commands/destroy_all.rb
|
83
|
+
- lib/mongoid/commands/save.rb
|
77
84
|
- lib/mongoid/criteria.rb
|
78
85
|
- lib/mongoid/document.rb
|
79
86
|
- lib/mongoid/extensions.rb
|
@@ -88,6 +95,12 @@ files:
|
|
88
95
|
- spec/unit/mongoid/associations/factory_spec.rb
|
89
96
|
- spec/unit/mongoid/associations/has_many_association_spec.rb
|
90
97
|
- spec/unit/mongoid/associations/has_one_association_spec.rb
|
98
|
+
- spec/unit/mongoid/commands/create_spec.rb
|
99
|
+
- spec/unit/mongoid/commands/delete_all_spec.rb
|
100
|
+
- spec/unit/mongoid/commands/delete_spec.rb
|
101
|
+
- spec/unit/mongoid/commands/destroy_all_spec.rb
|
102
|
+
- spec/unit/mongoid/commands/destroy_spec.rb
|
103
|
+
- spec/unit/mongoid/commands/save_spec.rb
|
91
104
|
- spec/unit/mongoid/criteria_spec.rb
|
92
105
|
- spec/unit/mongoid/document_spec.rb
|
93
106
|
- spec/unit/mongoid/extensions/array/conversions_spec.rb
|
@@ -128,6 +141,12 @@ test_files:
|
|
128
141
|
- spec/unit/mongoid/associations/factory_spec.rb
|
129
142
|
- spec/unit/mongoid/associations/has_many_association_spec.rb
|
130
143
|
- spec/unit/mongoid/associations/has_one_association_spec.rb
|
144
|
+
- spec/unit/mongoid/commands/create_spec.rb
|
145
|
+
- spec/unit/mongoid/commands/delete_all_spec.rb
|
146
|
+
- spec/unit/mongoid/commands/delete_spec.rb
|
147
|
+
- spec/unit/mongoid/commands/destroy_all_spec.rb
|
148
|
+
- spec/unit/mongoid/commands/destroy_spec.rb
|
149
|
+
- spec/unit/mongoid/commands/save_spec.rb
|
131
150
|
- spec/unit/mongoid/criteria_spec.rb
|
132
151
|
- spec/unit/mongoid/document_spec.rb
|
133
152
|
- spec/unit/mongoid/extensions/array/conversions_spec.rb
|