mongoid 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.watchr +11 -0
- data/VERSION +1 -1
- data/lib/mongoid.rb +3 -2
- data/lib/mongoid/criteria.rb +90 -0
- data/lib/mongoid/document.rb +31 -30
- data/mongoid.gemspec +6 -2
- data/spec/integration/mongoid/document_spec.rb +2 -2
- data/spec/spec_helper.rb +0 -1
- data/spec/unit/mongoid/criteria_spec.rb +289 -0
- data/spec/unit/mongoid/document_spec.rb +24 -19
- metadata +6 -2
data/.watchr
ADDED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.3
|
data/lib/mongoid.rb
CHANGED
@@ -21,9 +21,9 @@
|
|
21
21
|
require "rubygems"
|
22
22
|
|
23
23
|
gem "activesupport", "2.3.4"
|
24
|
-
gem "
|
24
|
+
gem "mongo", "0.15.1"
|
25
25
|
gem "durran-validatable", "1.7.5"
|
26
|
-
gem "
|
26
|
+
gem "will_paginate", "2.3.11"
|
27
27
|
|
28
28
|
require "validatable"
|
29
29
|
require "active_support/callbacks"
|
@@ -32,6 +32,7 @@ require "delegate"
|
|
32
32
|
require "will_paginate/collection"
|
33
33
|
require "mongo"
|
34
34
|
require "mongoid/associations"
|
35
|
+
require "mongoid/criteria"
|
35
36
|
require "mongoid/extensions"
|
36
37
|
require "mongoid/document"
|
37
38
|
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module Mongoid #:nodoc:
|
2
|
+
class Criteria #:nodoc:
|
3
|
+
attr_reader :selector, :options, :type
|
4
|
+
|
5
|
+
# Supply a hash of arrays for field values that must match every single
|
6
|
+
# element in the array.
|
7
|
+
def all(selections = {})
|
8
|
+
selections.each { |key, value| @selector[key] = { "$all" => value } }; self
|
9
|
+
end
|
10
|
+
|
11
|
+
# Excludes the parameters passed using $ne in the selector.
|
12
|
+
def excludes(exclusions = {})
|
13
|
+
exclusions.each { |key, value| @selector[key] = { "$ne" => value } }; self
|
14
|
+
end
|
15
|
+
|
16
|
+
# Execute the criteria, which will retrieve the results from
|
17
|
+
# the collection.
|
18
|
+
def execute(collection, klass)
|
19
|
+
return klass.new(collection.find_one(@selector, @options)) if type == :first
|
20
|
+
return collection.find(@selector, @options).collect { |doc| klass.new(doc) }
|
21
|
+
end
|
22
|
+
|
23
|
+
# Defines criteria for matching any of the supplied parameters, similar to
|
24
|
+
# a SQL in statement.
|
25
|
+
def in(inclusions = {})
|
26
|
+
inclusions.each { |key, value| @selector[key] = { "$in" => value } }; self
|
27
|
+
end
|
28
|
+
|
29
|
+
# Adds an _id criteria to the selector.
|
30
|
+
def id(object_id)
|
31
|
+
@selector[:_id] = Mongo::ObjectID.from_string(object_id); self
|
32
|
+
end
|
33
|
+
|
34
|
+
# Create the new Criteria object. Does not take any params, just
|
35
|
+
# initializes the selector and options hashes that will be
|
36
|
+
# eventually passed to the driver.
|
37
|
+
def initialize(type)
|
38
|
+
@selector, @options, @type = {}, {}, type
|
39
|
+
end
|
40
|
+
|
41
|
+
# Limits the number of results returned by the query, usually used in
|
42
|
+
# conjunction with skip() for pagination.
|
43
|
+
def limit(value = 20)
|
44
|
+
@options[:limit] = value; self
|
45
|
+
end
|
46
|
+
|
47
|
+
# The conditions that must prove true on each record in the
|
48
|
+
# database in order for them to be a part of the result set.
|
49
|
+
# This is a hash that maps to a selector in the driver.
|
50
|
+
def select(selector = {})
|
51
|
+
@selector = selector; self
|
52
|
+
end
|
53
|
+
|
54
|
+
# Defines a clause that the criteria should ignore.
|
55
|
+
def not_in(exclusions)
|
56
|
+
exclusions.each { |key, value| @selector[key] = { "$nin" => value } }; self
|
57
|
+
end
|
58
|
+
|
59
|
+
# Define extras for the criteria.
|
60
|
+
def extras(extras)
|
61
|
+
@options = extras; self
|
62
|
+
end
|
63
|
+
|
64
|
+
# Specifies how to sort this Criteria. Current valid params
|
65
|
+
# are: { :field => 1 } or { :field => -1 }
|
66
|
+
def order_by(params = {})
|
67
|
+
@options[:sort] = params; self
|
68
|
+
end
|
69
|
+
|
70
|
+
# Specify what fields to be returned from the database.
|
71
|
+
# Similar to a SQL select field1, field2, field3
|
72
|
+
def only(*args)
|
73
|
+
@options[:fields] = args.flatten; self
|
74
|
+
end
|
75
|
+
|
76
|
+
# Skips the supplied number of records, as offset behaves in traditional
|
77
|
+
# pagination.
|
78
|
+
def skip(value = 0)
|
79
|
+
@options[:skip] = value; self
|
80
|
+
end
|
81
|
+
|
82
|
+
# Translate the supplied arguments into a criteria object.
|
83
|
+
def self.translate(*args)
|
84
|
+
type, params = args[0], args[1] || {}
|
85
|
+
return new(:first).id(type.to_s) if type.is_a?(String)
|
86
|
+
return new(type).select(params.delete(:conditions)).extras(params)
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
data/lib/mongoid/document.rb
CHANGED
@@ -45,44 +45,35 @@ module Mongoid #:nodoc:
|
|
45
45
|
collection.drop
|
46
46
|
end
|
47
47
|
|
48
|
+
# Defines all the fields that are accessable on the Document
|
49
|
+
# For each field that is defined, a getter and setter will be
|
50
|
+
# added as an instance method to the Document.
|
51
|
+
def fields(*names)
|
52
|
+
@fields ||= []
|
53
|
+
names.flatten.each do |name|
|
54
|
+
@fields << name
|
55
|
+
define_method(name) { read_attribute(name) }
|
56
|
+
define_method("#{name}=") { |value| write_attribute(name, value) }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
48
60
|
# Find all Documents in several ways.
|
49
61
|
# Model.find(:first, :attribute => "value")
|
50
62
|
# Model.find(:all, :attribute => "value")
|
51
63
|
def find(*args)
|
52
|
-
|
53
|
-
case type
|
54
|
-
when :all then find_all(params)
|
55
|
-
when :first then find_first(params)
|
56
|
-
else find_first(Mongo::ObjectID.from_string(type.to_s))
|
57
|
-
end
|
64
|
+
Criteria.translate(*args).execute(collection, self)
|
58
65
|
end
|
59
66
|
|
60
67
|
# Find a single Document given the passed selector, which is a Hash of attributes that
|
61
68
|
# must match the Document in the database exactly.
|
62
|
-
def
|
63
|
-
|
64
|
-
when Hash then new(collection.find_one(params[:conditions]))
|
65
|
-
else new(collection.find_one(params))
|
66
|
-
end
|
69
|
+
def first(*args)
|
70
|
+
find(:first, *args)
|
67
71
|
end
|
68
72
|
|
69
73
|
# Find all Documents given the passed selector, which is a Hash of attributes that
|
70
74
|
# must match the Document in the database exactly.
|
71
|
-
def
|
72
|
-
|
73
|
-
collection.find(selector, params).collect { |doc| new(doc) }
|
74
|
-
end
|
75
|
-
|
76
|
-
# Defines all the fields that are accessable on the Document
|
77
|
-
# For each field that is defined, a getter and setter will be
|
78
|
-
# added as an instance method to the Document.
|
79
|
-
def fields(*names)
|
80
|
-
@fields ||= []
|
81
|
-
names.flatten.each do |name|
|
82
|
-
@fields << name
|
83
|
-
define_method(name) { read_attribute(name) }
|
84
|
-
define_method("#{name}=") { |value| write_attribute(name, value) }
|
85
|
-
end
|
75
|
+
def all(*args)
|
76
|
+
find(:all, *args)
|
86
77
|
end
|
87
78
|
|
88
79
|
# Find all Documents given the supplied criteria, grouped by the fields
|
@@ -109,7 +100,10 @@ module Mongoid #:nodoc:
|
|
109
100
|
def has_timestamps
|
110
101
|
fields :created_at, :last_modified
|
111
102
|
class_eval do
|
112
|
-
|
103
|
+
before_create \
|
104
|
+
:update_created_at,
|
105
|
+
:update_last_modified
|
106
|
+
before_save :update_last_modified
|
113
107
|
end
|
114
108
|
end
|
115
109
|
|
@@ -178,12 +172,13 @@ module Mongoid #:nodoc:
|
|
178
172
|
if @parent
|
179
173
|
@parent.save
|
180
174
|
else
|
175
|
+
return false unless valid?
|
181
176
|
run_callbacks(:before_create) if creating
|
182
177
|
run_callbacks(:before_save)
|
183
178
|
collection.save(@attributes)
|
184
179
|
run_callbacks(:after_create) if creating
|
185
180
|
run_callbacks(:after_save)
|
186
|
-
|
181
|
+
creating ? self : true
|
187
182
|
end
|
188
183
|
end
|
189
184
|
|
@@ -219,9 +214,15 @@ module Mongoid #:nodoc:
|
|
219
214
|
@attributes[name.to_sym]
|
220
215
|
end
|
221
216
|
|
222
|
-
# Update the
|
223
|
-
|
217
|
+
# Update the created_at field on the Document to the current time. This is
|
218
|
+
# only called on create.
|
219
|
+
def update_created_at
|
224
220
|
self.created_at = Time.now
|
221
|
+
end
|
222
|
+
|
223
|
+
# Update the last_modified field on the Document to the current time.
|
224
|
+
# This is only called on create and on save.
|
225
|
+
def update_last_modified
|
225
226
|
self.last_modified = Time.now
|
226
227
|
end
|
227
228
|
|
data/mongoid.gemspec
CHANGED
@@ -5,17 +5,18 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.3"
|
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-09}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.textile"
|
16
16
|
]
|
17
17
|
s.files = [
|
18
18
|
".gitignore",
|
19
|
+
".watchr",
|
19
20
|
"MIT_LICENSE",
|
20
21
|
"README.textile",
|
21
22
|
"Rakefile",
|
@@ -27,6 +28,7 @@ Gem::Specification.new do |s|
|
|
27
28
|
"lib/mongoid/associations/factory.rb",
|
28
29
|
"lib/mongoid/associations/has_many_association.rb",
|
29
30
|
"lib/mongoid/associations/has_one_association.rb",
|
31
|
+
"lib/mongoid/criteria.rb",
|
30
32
|
"lib/mongoid/document.rb",
|
31
33
|
"lib/mongoid/extensions.rb",
|
32
34
|
"lib/mongoid/extensions/array/conversions.rb",
|
@@ -40,6 +42,7 @@ Gem::Specification.new do |s|
|
|
40
42
|
"spec/unit/mongoid/associations/factory_spec.rb",
|
41
43
|
"spec/unit/mongoid/associations/has_many_association_spec.rb",
|
42
44
|
"spec/unit/mongoid/associations/has_one_association_spec.rb",
|
45
|
+
"spec/unit/mongoid/criteria_spec.rb",
|
43
46
|
"spec/unit/mongoid/document_spec.rb",
|
44
47
|
"spec/unit/mongoid/extensions/array/conversions_spec.rb",
|
45
48
|
"spec/unit/mongoid/extensions/object/conversions_spec.rb"
|
@@ -57,6 +60,7 @@ Gem::Specification.new do |s|
|
|
57
60
|
"spec/unit/mongoid/associations/factory_spec.rb",
|
58
61
|
"spec/unit/mongoid/associations/has_many_association_spec.rb",
|
59
62
|
"spec/unit/mongoid/associations/has_one_association_spec.rb",
|
63
|
+
"spec/unit/mongoid/criteria_spec.rb",
|
60
64
|
"spec/unit/mongoid/document_spec.rb",
|
61
65
|
"spec/unit/mongoid/extensions/array/conversions_spec.rb",
|
62
66
|
"spec/unit/mongoid/extensions/object/conversions_spec.rb"
|
@@ -43,7 +43,7 @@ describe Mongoid::Document do
|
|
43
43
|
context "finding first document" do
|
44
44
|
|
45
45
|
it "returns the first document based on the selector provided" do
|
46
|
-
person = Person.find(:first, :title => "Test")
|
46
|
+
person = Person.find(:first, :conditions => { :title => "Test" })
|
47
47
|
person.title.should == "Test"
|
48
48
|
end
|
49
49
|
|
@@ -52,7 +52,7 @@ describe Mongoid::Document do
|
|
52
52
|
context "finding by id" do
|
53
53
|
|
54
54
|
it "finds the document by the supplied id" do
|
55
|
-
person = Person.find(@person.id)
|
55
|
+
person = Person.find(@person.id.to_s)
|
56
56
|
person.id.should == @person.id
|
57
57
|
end
|
58
58
|
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,289 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
|
+
|
3
|
+
describe Mongoid::Criteria do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@criteria = Mongoid::Criteria.new(:all)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#all" do
|
10
|
+
|
11
|
+
it "adds the $all query to the selector" do
|
12
|
+
@criteria.all(:title => ["title1", "title2"])
|
13
|
+
@criteria.selector.should == { :title => { "$all" => ["title1", "title2"] } }
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns self" do
|
17
|
+
@criteria.all(:title => [ "title1" ]).should == @criteria
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#excludes" do
|
23
|
+
|
24
|
+
it "adds the $ne query to the selector" do
|
25
|
+
@criteria.excludes(:title => "Bad Title", :text => "Bad Text")
|
26
|
+
@criteria.selector.should == { :title => { "$ne" => "Bad Title"}, :text => { "$ne" => "Bad Text" } }
|
27
|
+
end
|
28
|
+
|
29
|
+
it "returns self" do
|
30
|
+
@criteria.excludes(:title => "Bad").should == @criteria
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#execute" do
|
36
|
+
|
37
|
+
context "when type is :first" do
|
38
|
+
|
39
|
+
it "calls find on the collection with the selector and options" do
|
40
|
+
criteria = Mongoid::Criteria.new(:first)
|
41
|
+
collection = mock
|
42
|
+
collection.expects(:find_one).with(@criteria.selector, @criteria.options).returns({})
|
43
|
+
criteria.execute(collection, Person).should be_a_kind_of(Person)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
context "when type is not :first" do
|
49
|
+
|
50
|
+
it "calls find on the collection with the selector and options" do
|
51
|
+
criteria = Mongoid::Criteria.new(:all)
|
52
|
+
collection = mock
|
53
|
+
collection.expects(:find).with(@criteria.selector, @criteria.options).returns([])
|
54
|
+
criteria.execute(collection, Person).should == []
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#extras" do
|
62
|
+
|
63
|
+
it "adds the extras to the options" do
|
64
|
+
@criteria.extras({ :skip => 10 })
|
65
|
+
@criteria.options.should == { :skip => 10 }
|
66
|
+
end
|
67
|
+
|
68
|
+
it "returns self" do
|
69
|
+
@criteria.extras({}).should == @criteria
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "#id" do
|
75
|
+
|
76
|
+
it "adds the _id query to the selector" do
|
77
|
+
id = Mongo::ObjectID.new
|
78
|
+
@criteria.id(id.to_s)
|
79
|
+
@criteria.selector.should == { :_id => id }
|
80
|
+
end
|
81
|
+
|
82
|
+
it "returns self" do
|
83
|
+
id = Mongo::ObjectID.new
|
84
|
+
@criteria.id(id.to_s).should == @criteria
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
describe "#in" do
|
90
|
+
|
91
|
+
it "adds the $in clause to the selector" do
|
92
|
+
@criteria.in(:title => ["title1", "title2"], :text => ["test"])
|
93
|
+
@criteria.selector.should == { :title => { "$in" => ["title1", "title2"] }, :text => { "$in" => ["test"] } }
|
94
|
+
end
|
95
|
+
|
96
|
+
it "returns self" do
|
97
|
+
@criteria.in(:title => ["title1"]).should == @criteria
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "#limit" do
|
103
|
+
|
104
|
+
context "when value provided" do
|
105
|
+
|
106
|
+
it "adds the limit to the options" do
|
107
|
+
@criteria.limit(100)
|
108
|
+
@criteria.options.should == { :limit => 100 }
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
context "when value not provided" do
|
114
|
+
|
115
|
+
it "defaults to 20" do
|
116
|
+
@criteria.limit
|
117
|
+
@criteria.options.should == { :limit => 20 }
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
it "returns self" do
|
123
|
+
@criteria.limit.should == @criteria
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "#select" do
|
129
|
+
|
130
|
+
it "adds the clause to the selector" do
|
131
|
+
@criteria.select(:title => "Title", :text => "Text")
|
132
|
+
@criteria.selector.should == { :title => "Title", :text => "Text" }
|
133
|
+
end
|
134
|
+
|
135
|
+
it "returns self" do
|
136
|
+
@criteria.select.should == @criteria
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
describe "#not_in" do
|
142
|
+
|
143
|
+
it "adds the exclusion to the selector" do
|
144
|
+
@criteria.not_in(:title => ["title1", "title2"], :text => ["test"])
|
145
|
+
@criteria.selector.should == { :title => { "$nin" => ["title1", "title2"] }, :text => { "$nin" => ["test"] } }
|
146
|
+
end
|
147
|
+
|
148
|
+
it "returns self" do
|
149
|
+
@criteria.not_in(:title => ["title1"]).should == @criteria
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
|
154
|
+
describe "#order_by" do
|
155
|
+
|
156
|
+
context "when field names and direction specified" do
|
157
|
+
|
158
|
+
it "adds the sort to the options" do
|
159
|
+
@criteria.order_by(:title => 1, :text => -1)
|
160
|
+
@criteria.options.should == { :sort => { :title => 1, :text => -1 }}
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
it "returns self" do
|
166
|
+
@criteria.order_by.should == @criteria
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
describe "#only" do
|
172
|
+
|
173
|
+
it "adds the options for limiting by fields" do
|
174
|
+
@criteria.only(:title, :text)
|
175
|
+
@criteria.options.should == { :fields => [ :title, :text ] }
|
176
|
+
end
|
177
|
+
|
178
|
+
it "returns self" do
|
179
|
+
@criteria.only.should == @criteria
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
describe "#skip" do
|
185
|
+
|
186
|
+
context "when value provided" do
|
187
|
+
|
188
|
+
it "adds the skip value to the options" do
|
189
|
+
@criteria.skip(20)
|
190
|
+
@criteria.options.should == { :skip => 20 }
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
context "when value not provided" do
|
196
|
+
|
197
|
+
it "defaults to zero" do
|
198
|
+
@criteria.skip
|
199
|
+
@criteria.options.should == { :skip => 0 }
|
200
|
+
end
|
201
|
+
|
202
|
+
end
|
203
|
+
|
204
|
+
it "returns self" do
|
205
|
+
@criteria.skip.should == @criteria
|
206
|
+
end
|
207
|
+
|
208
|
+
end
|
209
|
+
|
210
|
+
describe "#translate" do
|
211
|
+
|
212
|
+
context "single argument as a string" do
|
213
|
+
|
214
|
+
it "creates a new select criteria" do
|
215
|
+
id = Mongo::ObjectID.new
|
216
|
+
criteria = Mongoid::Criteria.translate(id.to_s)
|
217
|
+
criteria.selector.should == { :_id => id }
|
218
|
+
end
|
219
|
+
|
220
|
+
end
|
221
|
+
|
222
|
+
context "multiple arguments" do
|
223
|
+
|
224
|
+
context "when :first, :conditions => {}" do
|
225
|
+
|
226
|
+
before do
|
227
|
+
@criteria = Mongoid::Criteria.translate(:first, :conditions => { :title => "Test" })
|
228
|
+
end
|
229
|
+
|
230
|
+
it "returns a criteria with a selector from the conditions" do
|
231
|
+
@criteria.selector.should == { :title => "Test" }
|
232
|
+
end
|
233
|
+
|
234
|
+
it "returns a criteria with type :first" do
|
235
|
+
@criteria.type.should == :first
|
236
|
+
end
|
237
|
+
|
238
|
+
end
|
239
|
+
|
240
|
+
context "when :all, :conditions => {}" do
|
241
|
+
|
242
|
+
before do
|
243
|
+
@criteria = Mongoid::Criteria.translate(:all, :conditions => { :title => "Test" })
|
244
|
+
end
|
245
|
+
|
246
|
+
it "returns a criteria with a selector from the conditions" do
|
247
|
+
@criteria.selector.should == { :title => "Test" }
|
248
|
+
end
|
249
|
+
|
250
|
+
it "returns a criteria with type :all" do
|
251
|
+
@criteria.type.should == :all
|
252
|
+
end
|
253
|
+
|
254
|
+
end
|
255
|
+
|
256
|
+
context "when :last, :conditions => {}" do
|
257
|
+
|
258
|
+
before do
|
259
|
+
@criteria = Mongoid::Criteria.translate(:last, :conditions => { :title => "Test" })
|
260
|
+
end
|
261
|
+
|
262
|
+
it "returns a criteria with a selector from the conditions" do
|
263
|
+
@criteria.selector.should == { :title => "Test" }
|
264
|
+
end
|
265
|
+
|
266
|
+
it "returns a criteria with type :last" do
|
267
|
+
@criteria.type.should == :last
|
268
|
+
end
|
269
|
+
|
270
|
+
end
|
271
|
+
|
272
|
+
context "when options are provided" do
|
273
|
+
|
274
|
+
before do
|
275
|
+
@criteria = Mongoid::Criteria.translate(:last, :conditions => { :title => "Test" }, :skip => 10)
|
276
|
+
end
|
277
|
+
|
278
|
+
it "adds the criteria and the options" do
|
279
|
+
@criteria.selector.should == { :title => "Test" }
|
280
|
+
@criteria.options.should == { :skip => 10 }
|
281
|
+
end
|
282
|
+
|
283
|
+
end
|
284
|
+
|
285
|
+
end
|
286
|
+
|
287
|
+
end
|
288
|
+
|
289
|
+
end
|
@@ -3,13 +3,15 @@ require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
|
3
3
|
describe Mongoid::Document do
|
4
4
|
|
5
5
|
before do
|
6
|
-
@collection =
|
6
|
+
@collection = stub(:name => "people")
|
7
7
|
@database = stub(:collection => @collection)
|
8
8
|
Mongoid.stubs(:database).returns(@database)
|
9
9
|
end
|
10
10
|
|
11
11
|
after do
|
12
12
|
Person.instance_variable_set(:@collection, nil)
|
13
|
+
@database = nil
|
14
|
+
@collection = nil
|
13
15
|
end
|
14
16
|
|
15
17
|
describe "#aggregate" do
|
@@ -43,9 +45,12 @@ describe Mongoid::Document do
|
|
43
45
|
|
44
46
|
describe "#collection" do
|
45
47
|
|
48
|
+
before do
|
49
|
+
@person = Person.new
|
50
|
+
end
|
51
|
+
|
46
52
|
it "sets the collection name to the class pluralized" do
|
47
|
-
|
48
|
-
Person.collection
|
53
|
+
Person.collection.name.should == "people"
|
49
54
|
end
|
50
55
|
|
51
56
|
end
|
@@ -121,6 +126,7 @@ describe Mongoid::Document do
|
|
121
126
|
|
122
127
|
before do
|
123
128
|
@attributes = { :document_class => "Person" }
|
129
|
+
@criteria = mock
|
124
130
|
end
|
125
131
|
|
126
132
|
context "when an id is passed in" do
|
@@ -129,8 +135,9 @@ describe Mongoid::Document do
|
|
129
135
|
@id = Mongo::ObjectID.new
|
130
136
|
end
|
131
137
|
|
132
|
-
it "delegates to
|
133
|
-
|
138
|
+
it "delegates to criteria" do
|
139
|
+
Mongoid::Criteria.expects(:translate).with(@id.to_s).returns(@criteria)
|
140
|
+
@criteria.expects(:execute).with(@collection, Person).returns(@attributes)
|
134
141
|
Person.find(@id.to_s)
|
135
142
|
end
|
136
143
|
|
@@ -138,8 +145,9 @@ describe Mongoid::Document do
|
|
138
145
|
|
139
146
|
context "when finding first" do
|
140
147
|
|
141
|
-
it "delegates to
|
142
|
-
|
148
|
+
it "delegates to criteria" do
|
149
|
+
Mongoid::Criteria.expects(:translate).with(:first, :conditions => { :test => "Test" }).returns(@criteria)
|
150
|
+
@criteria.expects(:execute).with(@collection, Person).returns(@attributes)
|
143
151
|
Person.find(:first, :conditions => { :test => "Test" })
|
144
152
|
end
|
145
153
|
|
@@ -176,7 +184,7 @@ describe Mongoid::Document do
|
|
176
184
|
|
177
185
|
end
|
178
186
|
|
179
|
-
describe "#
|
187
|
+
describe "#first" do
|
180
188
|
|
181
189
|
before do
|
182
190
|
@attributes = { :document_class => "Person" }
|
@@ -185,8 +193,8 @@ describe Mongoid::Document do
|
|
185
193
|
context "when a selector is provided" do
|
186
194
|
|
187
195
|
it "finds the first document from the collection and instantiates it" do
|
188
|
-
@collection.expects(:find_one).with(:test => "Test").returns(@attributes)
|
189
|
-
Person.
|
196
|
+
@collection.expects(:find_one).with({ :test => "Test" }, {}).returns(@attributes)
|
197
|
+
Person.first(:conditions => {:test => "Test"}).attributes.should == @attributes
|
190
198
|
end
|
191
199
|
|
192
200
|
end
|
@@ -194,15 +202,15 @@ describe Mongoid::Document do
|
|
194
202
|
context "when a selector is not provided" do
|
195
203
|
|
196
204
|
it "finds the first document from the collection and instantiates it" do
|
197
|
-
@collection.expects(:find_one).with(nil).returns(@attributes)
|
198
|
-
Person.
|
205
|
+
@collection.expects(:find_one).with(nil, {}).returns(@attributes)
|
206
|
+
Person.first.attributes.should == @attributes
|
199
207
|
end
|
200
208
|
|
201
209
|
end
|
202
210
|
|
203
211
|
end
|
204
212
|
|
205
|
-
describe "#
|
213
|
+
describe "#all" do
|
206
214
|
|
207
215
|
before do
|
208
216
|
@cursor = mock
|
@@ -214,7 +222,7 @@ describe Mongoid::Document do
|
|
214
222
|
it "finds from the collection and instantiate objects for each returned" do
|
215
223
|
@collection.expects(:find).with({ :test => "Test" }, {}).returns(@cursor)
|
216
224
|
@cursor.expects(:collect).returns(@people)
|
217
|
-
Person.
|
225
|
+
Person.all(:conditions => {:test => "Test"})
|
218
226
|
end
|
219
227
|
|
220
228
|
end
|
@@ -224,7 +232,7 @@ describe Mongoid::Document do
|
|
224
232
|
it "finds from the collection and instantiate objects for each returned" do
|
225
233
|
@collection.expects(:find).with(nil, {}).returns(@cursor)
|
226
234
|
@cursor.expects(:collect).returns(@people)
|
227
|
-
Person.
|
235
|
+
Person.all
|
228
236
|
end
|
229
237
|
|
230
238
|
end
|
@@ -326,10 +334,7 @@ describe Mongoid::Document do
|
|
326
334
|
end
|
327
335
|
|
328
336
|
it "adds created_on and last_modified to the document" do
|
329
|
-
|
330
|
-
@tester.save
|
331
|
-
@tester.created_at.should_not be_nil
|
332
|
-
@tester.last_modified.should_not be_nil
|
337
|
+
Tester.instance_variable_get(:@fields).should include(:created_at, :last_modified)
|
333
338
|
end
|
334
339
|
|
335
340
|
end
|
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.3.
|
4
|
+
version: 0.3.3
|
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-09 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -62,6 +62,7 @@ extra_rdoc_files:
|
|
62
62
|
- README.textile
|
63
63
|
files:
|
64
64
|
- .gitignore
|
65
|
+
- .watchr
|
65
66
|
- MIT_LICENSE
|
66
67
|
- README.textile
|
67
68
|
- Rakefile
|
@@ -73,6 +74,7 @@ files:
|
|
73
74
|
- lib/mongoid/associations/factory.rb
|
74
75
|
- lib/mongoid/associations/has_many_association.rb
|
75
76
|
- lib/mongoid/associations/has_one_association.rb
|
77
|
+
- lib/mongoid/criteria.rb
|
76
78
|
- lib/mongoid/document.rb
|
77
79
|
- lib/mongoid/extensions.rb
|
78
80
|
- lib/mongoid/extensions/array/conversions.rb
|
@@ -86,6 +88,7 @@ files:
|
|
86
88
|
- spec/unit/mongoid/associations/factory_spec.rb
|
87
89
|
- spec/unit/mongoid/associations/has_many_association_spec.rb
|
88
90
|
- spec/unit/mongoid/associations/has_one_association_spec.rb
|
91
|
+
- spec/unit/mongoid/criteria_spec.rb
|
89
92
|
- spec/unit/mongoid/document_spec.rb
|
90
93
|
- spec/unit/mongoid/extensions/array/conversions_spec.rb
|
91
94
|
- spec/unit/mongoid/extensions/object/conversions_spec.rb
|
@@ -125,6 +128,7 @@ test_files:
|
|
125
128
|
- spec/unit/mongoid/associations/factory_spec.rb
|
126
129
|
- spec/unit/mongoid/associations/has_many_association_spec.rb
|
127
130
|
- spec/unit/mongoid/associations/has_one_association_spec.rb
|
131
|
+
- spec/unit/mongoid/criteria_spec.rb
|
128
132
|
- spec/unit/mongoid/document_spec.rb
|
129
133
|
- spec/unit/mongoid/extensions/array/conversions_spec.rb
|
130
134
|
- spec/unit/mongoid/extensions/object/conversions_spec.rb
|