mongoid 0.4.4 → 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +1 -1
- data/VERSION +1 -1
- data/lib/mongoid.rb +0 -4
- data/lib/mongoid/criteria.rb +23 -23
- data/lib/mongoid/document.rb +76 -6
- data/mongoid.gemspec +2 -2
- data/spec/integration/mongoid/document_spec.rb +3 -3
- data/spec/unit/mongoid/associations/has_many_association_spec.rb +4 -4
- data/spec/unit/mongoid/criteria_spec.rb +16 -16
- data/spec/unit/mongoid/document_spec.rb +35 -8
- metadata +2 -2
data/README.textile
CHANGED
@@ -98,7 +98,7 @@ Old School:
|
|
98
98
|
New School:
|
99
99
|
|
100
100
|
<pre>
|
101
|
-
|
101
|
+
Person.select(:first_name, :last_name).where(:title => "Sir").skip(10).limit(10).execute
|
102
102
|
Criteria.translate(:conditions => { :title => "Sir" }).execute
|
103
103
|
</pre>
|
104
104
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.5
|
data/lib/mongoid.rb
CHANGED
@@ -43,10 +43,6 @@ module Mongoid
|
|
43
43
|
# Raised when the database connection has not been set up.
|
44
44
|
class NoConnectionError < RuntimeError; end
|
45
45
|
|
46
|
-
# Raised when :document_class is not provided in the attributes
|
47
|
-
# hash when creating a new Document
|
48
|
-
class ClassNotProvidedError < RuntimeError; end
|
49
|
-
|
50
46
|
# Raised when an association is defined on the class, but the
|
51
47
|
# attribute in the hash is not an Array or Hash.
|
52
48
|
class TypeMismatchError < RuntimeError; end
|
data/lib/mongoid/criteria.rb
CHANGED
@@ -150,25 +150,6 @@ module Mongoid #:nodoc:
|
|
150
150
|
@options[:limit] = value; self
|
151
151
|
end
|
152
152
|
|
153
|
-
# Adds a criterion to the +Criteria+ that specifies values that must
|
154
|
-
# be matched in order to return results. This is similar to a SQL "WHERE"
|
155
|
-
# clause. This is the actual selector that will be provided to MongoDB,
|
156
|
-
# similar to the Javascript object that is used when performing a find()
|
157
|
-
# in the MongoDB console.
|
158
|
-
#
|
159
|
-
# Options:
|
160
|
-
#
|
161
|
-
# selectior: A +Hash+ that must match the attributes of the +Document+.
|
162
|
-
#
|
163
|
-
# Example:
|
164
|
-
#
|
165
|
-
# <tt>criteria.select(:field1 => "value1", :field2 => 15)</tt>
|
166
|
-
#
|
167
|
-
# Returns: <tt>self</tt>
|
168
|
-
def select(selector = {})
|
169
|
-
@selector = selector; self
|
170
|
-
end
|
171
|
-
|
172
153
|
# Adds a criterion to the +Criteria+ that specifies values where none
|
173
154
|
# should match in order to return results. This is similar to an SQL "NOT IN"
|
174
155
|
# clause. The MongoDB conditional operator that will be used is "$nin".
|
@@ -211,14 +192,14 @@ module Mongoid #:nodoc:
|
|
211
192
|
#
|
212
193
|
# Options:
|
213
194
|
#
|
214
|
-
#
|
195
|
+
# args: A list of field names to retrict the returned fields to.
|
215
196
|
#
|
216
197
|
# Example:
|
217
198
|
#
|
218
|
-
# <tt>criteria.
|
199
|
+
# <tt>criteria.select(:field1, :field2, :field3)</tt>
|
219
200
|
#
|
220
201
|
# Returns: <tt>self</tt>
|
221
|
-
def
|
202
|
+
def select(*args)
|
222
203
|
@options[:fields] = args.flatten; self
|
223
204
|
end
|
224
205
|
|
@@ -262,7 +243,26 @@ module Mongoid #:nodoc:
|
|
262
243
|
def self.translate(*args)
|
263
244
|
type, params = args[0], args[1] || {}
|
264
245
|
return new(:first).id(type.to_s) if type.is_a?(String)
|
265
|
-
return new(type).
|
246
|
+
return new(type).where(params.delete(:conditions)).extras(params)
|
247
|
+
end
|
248
|
+
|
249
|
+
# Adds a criterion to the +Criteria+ that specifies values that must
|
250
|
+
# be matched in order to return results. This is similar to a SQL "WHERE"
|
251
|
+
# clause. This is the actual selector that will be provided to MongoDB,
|
252
|
+
# similar to the Javascript object that is used when performing a find()
|
253
|
+
# in the MongoDB console.
|
254
|
+
#
|
255
|
+
# Options:
|
256
|
+
#
|
257
|
+
# selectior: A +Hash+ that must match the attributes of the +Document+.
|
258
|
+
#
|
259
|
+
# Example:
|
260
|
+
#
|
261
|
+
# <tt>criteria.where(:field1 => "value1", :field2 => 15)</tt>
|
262
|
+
#
|
263
|
+
# Returns: <tt>self</tt>
|
264
|
+
def where(selector = {})
|
265
|
+
@selector = selector; self
|
266
266
|
end
|
267
267
|
|
268
268
|
end
|
data/lib/mongoid/document.rb
CHANGED
@@ -26,7 +26,24 @@ module Mongoid #:nodoc:
|
|
26
26
|
collection.group(fields, selector, { :count => 0 }, AGGREGATE_REDUCE)
|
27
27
|
end
|
28
28
|
|
29
|
-
# Adds the association back to the parent document.
|
29
|
+
# Adds the association back to the parent document. This macro is
|
30
|
+
# necessary to set the references from the child back to the parent
|
31
|
+
# document. If a child does not define this association calling
|
32
|
+
# persistence methods on the child object will cause a save to fail.
|
33
|
+
#
|
34
|
+
# Options:
|
35
|
+
#
|
36
|
+
# association_name: A +Symbol+ that matches the name of the parent class.
|
37
|
+
#
|
38
|
+
# Example:
|
39
|
+
#
|
40
|
+
# class Person < Mongoid::Document
|
41
|
+
# has_many :addresses
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# class Address < Mongoid::Document
|
45
|
+
# belongs_to :person
|
46
|
+
# end
|
30
47
|
def belongs_to(association_name)
|
31
48
|
@embedded = true
|
32
49
|
add_association(:belongs_to, association_name.to_s.classify, association_name)
|
@@ -42,6 +59,15 @@ module Mongoid #:nodoc:
|
|
42
59
|
# Defines all the fields that are accessable on the Document
|
43
60
|
# For each field that is defined, a getter and setter will be
|
44
61
|
# added as an instance method to the Document.
|
62
|
+
#
|
63
|
+
# Options:
|
64
|
+
#
|
65
|
+
# name: The name of the field, as a +Symbol+.
|
66
|
+
# options: A +Hash+ of options to supply to the +Field+.
|
67
|
+
#
|
68
|
+
# Example:
|
69
|
+
#
|
70
|
+
# <tt>field :score, :default => 0</tt>
|
45
71
|
def field(name, options = {})
|
46
72
|
@fields ||= {}
|
47
73
|
@fields[name] = Field.new(name, options)
|
@@ -54,9 +80,19 @@ module Mongoid #:nodoc:
|
|
54
80
|
@fields
|
55
81
|
end
|
56
82
|
|
57
|
-
# Find
|
58
|
-
#
|
59
|
-
#
|
83
|
+
# Find a +Document+ in several different ways.
|
84
|
+
#
|
85
|
+
# If a +String+ is provided, it will be assumed that it is a
|
86
|
+
# representation of a Mongo::ObjectID and will attempt to find a single
|
87
|
+
# +Document+ based on that id. If a +Symbol+ and +Hash+ is provided then
|
88
|
+
# it will attempt to find either a single +Document+ or multiples based
|
89
|
+
# on the conditions provided and the first parameter.
|
90
|
+
#
|
91
|
+
# <tt>Person.find(:first, :conditions => { :attribute => "value" })</tt>
|
92
|
+
#
|
93
|
+
# <tt>Person.find(:all, :conditions => { :attribute => "value" })</tt>
|
94
|
+
#
|
95
|
+
# <tt>Person.find(Mongo::ObjectID.new.to_s)</tt>
|
60
96
|
def find(*args)
|
61
97
|
Criteria.translate(*args).execute(self)
|
62
98
|
end
|
@@ -82,7 +118,23 @@ module Mongoid #:nodoc:
|
|
82
118
|
end
|
83
119
|
end
|
84
120
|
|
85
|
-
#
|
121
|
+
# Adds the association from a parent document to its children. The name
|
122
|
+
# of the association needs to be a pluralized form of the child class
|
123
|
+
# name.
|
124
|
+
#
|
125
|
+
# Options:
|
126
|
+
#
|
127
|
+
# association_name: A +Symbol+ that is the plural child class name.
|
128
|
+
#
|
129
|
+
# Example:
|
130
|
+
#
|
131
|
+
# class Person < Mongoid::Document
|
132
|
+
# has_many :addresses
|
133
|
+
# end
|
134
|
+
#
|
135
|
+
# class Address < Mongoid::Document
|
136
|
+
# belongs_to :person
|
137
|
+
# end
|
86
138
|
def has_many(association_name)
|
87
139
|
add_association(:has_many, association_name.to_s.classify, association_name)
|
88
140
|
end
|
@@ -127,6 +179,23 @@ module Mongoid #:nodoc:
|
|
127
179
|
end
|
128
180
|
end
|
129
181
|
|
182
|
+
# Entry point for creating a new criteria from a Document. This will
|
183
|
+
# instantiate a new +Criteria+ object with the supplied select criterion
|
184
|
+
# already added to it.
|
185
|
+
#
|
186
|
+
# Options:
|
187
|
+
#
|
188
|
+
# args: A list of field names to retrict the returned fields to.
|
189
|
+
#
|
190
|
+
# Example:
|
191
|
+
#
|
192
|
+
# <tt>Person.select(:field1, :field2, :field3)</tt>
|
193
|
+
#
|
194
|
+
# Returns: <tt>Criteria</tt>
|
195
|
+
def select(*args)
|
196
|
+
Criteria.new(:all).select(*args)
|
197
|
+
end
|
198
|
+
|
130
199
|
end
|
131
200
|
|
132
201
|
# Get the Mongo::Collection associated with this Document.
|
@@ -200,7 +269,8 @@ module Mongoid #:nodoc:
|
|
200
269
|
|
201
270
|
# Write to the attributes hash.
|
202
271
|
def write_attribute(name, value)
|
203
|
-
|
272
|
+
symbol = name.to_sym
|
273
|
+
@attributes[name.to_sym] = fields[symbol].value(value)
|
204
274
|
end
|
205
275
|
|
206
276
|
end
|
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.5"
|
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-18}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.textile"
|
@@ -28,7 +28,7 @@ describe Mongoid::Document do
|
|
28
28
|
describe "#find" do
|
29
29
|
|
30
30
|
before do
|
31
|
-
@person = Person.create(:title => "Test"
|
31
|
+
@person = Person.create(:title => "Test")
|
32
32
|
end
|
33
33
|
|
34
34
|
context "finding all documents" do
|
@@ -64,7 +64,7 @@ describe Mongoid::Document do
|
|
64
64
|
|
65
65
|
before do
|
66
66
|
30.times do |num|
|
67
|
-
Person.create(:title => "Sir", :age => num
|
67
|
+
Person.create(:title => "Sir", :age => num)
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
@@ -82,7 +82,7 @@ describe Mongoid::Document do
|
|
82
82
|
|
83
83
|
before do
|
84
84
|
30.times do |num|
|
85
|
-
Person.create(:title => "Test-#{num}"
|
85
|
+
Person.create(:title => "Test-#{num}")
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
@@ -6,8 +6,8 @@ describe Mongoid::Associations::HasManyAssociation do
|
|
6
6
|
@first_id = Mongo::ObjectID.new
|
7
7
|
@second_id = Mongo::ObjectID.new
|
8
8
|
@attributes = { :addresses => [
|
9
|
-
{ :_id => @first_id, :street => "Street 1"
|
10
|
-
{ :_id => @second_id, :street => "Street 2"
|
9
|
+
{ :_id => @first_id, :street => "Street 1" },
|
10
|
+
{ :_id => @second_id, :street => "Street 2" } ] }
|
11
11
|
@document = stub(:attributes => @attributes)
|
12
12
|
end
|
13
13
|
|
@@ -88,14 +88,14 @@ describe Mongoid::Associations::HasManyAssociation do
|
|
88
88
|
end
|
89
89
|
|
90
90
|
it "adds a new document to the array with the suppied parameters" do
|
91
|
-
@association.build({ :street => "Street 1"
|
91
|
+
@association.build({ :street => "Street 1" })
|
92
92
|
@association.length.should == 3
|
93
93
|
@association[2].should be_a_kind_of(Address)
|
94
94
|
@association[2].street.should == "Street 1"
|
95
95
|
end
|
96
96
|
|
97
97
|
it "returns the newly built object in the association" do
|
98
|
-
address = @association.build({ :street => "Yet Another"
|
98
|
+
address = @association.build({ :street => "Yet Another" })
|
99
99
|
address.should be_a_kind_of(Address)
|
100
100
|
address.street.should == "Yet Another"
|
101
101
|
end
|
@@ -127,19 +127,6 @@ describe Mongoid::Criteria do
|
|
127
127
|
|
128
128
|
end
|
129
129
|
|
130
|
-
describe "#select" do
|
131
|
-
|
132
|
-
it "adds the clause to the selector" do
|
133
|
-
@criteria.select(:title => "Title", :text => "Text")
|
134
|
-
@criteria.selector.should == { :title => "Title", :text => "Text" }
|
135
|
-
end
|
136
|
-
|
137
|
-
it "returns self" do
|
138
|
-
@criteria.select.should == @criteria
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
142
|
-
|
143
130
|
describe "#not_in" do
|
144
131
|
|
145
132
|
it "adds the exclusion to the selector" do
|
@@ -170,15 +157,15 @@ describe Mongoid::Criteria do
|
|
170
157
|
|
171
158
|
end
|
172
159
|
|
173
|
-
describe "#
|
160
|
+
describe "#select" do
|
174
161
|
|
175
162
|
it "adds the options for limiting by fields" do
|
176
|
-
@criteria.
|
163
|
+
@criteria.select(:title, :text)
|
177
164
|
@criteria.options.should == { :fields => [ :title, :text ] }
|
178
165
|
end
|
179
166
|
|
180
167
|
it "returns self" do
|
181
|
-
@criteria.
|
168
|
+
@criteria.select.should == @criteria
|
182
169
|
end
|
183
170
|
|
184
171
|
end
|
@@ -288,4 +275,17 @@ describe Mongoid::Criteria do
|
|
288
275
|
|
289
276
|
end
|
290
277
|
|
278
|
+
describe "#where" do
|
279
|
+
|
280
|
+
it "adds the clause to the selector" do
|
281
|
+
@criteria.where(:title => "Title", :text => "Text")
|
282
|
+
@criteria.selector.should == { :title => "Title", :text => "Text" }
|
283
|
+
end
|
284
|
+
|
285
|
+
it "returns self" do
|
286
|
+
@criteria.where.should == @criteria
|
287
|
+
end
|
288
|
+
|
289
|
+
end
|
290
|
+
|
291
291
|
end
|
@@ -136,7 +136,7 @@ describe Mongoid::Document do
|
|
136
136
|
describe "#find" do
|
137
137
|
|
138
138
|
before do
|
139
|
-
@attributes = {
|
139
|
+
@attributes = {}
|
140
140
|
@criteria = mock
|
141
141
|
end
|
142
142
|
|
@@ -198,7 +198,7 @@ describe Mongoid::Document do
|
|
198
198
|
describe "#first" do
|
199
199
|
|
200
200
|
before do
|
201
|
-
@attributes = {
|
201
|
+
@attributes = {}
|
202
202
|
end
|
203
203
|
|
204
204
|
context "when a selector is provided" do
|
@@ -259,13 +259,13 @@ describe Mongoid::Document do
|
|
259
259
|
before do
|
260
260
|
@attributes = { :title => "Sir",
|
261
261
|
:addresses => [
|
262
|
-
{ :street => "Street 1"
|
263
|
-
{ :street => "Street 2"
|
262
|
+
{ :street => "Street 1" },
|
263
|
+
{ :street => "Street 2" } ] }
|
264
264
|
@person = Person.new(@attributes)
|
265
265
|
end
|
266
266
|
|
267
267
|
it "sets the attributes for the association" do
|
268
|
-
address = Address.new(:street => "New Street"
|
268
|
+
address = Address.new(:street => "New Street")
|
269
269
|
@person.addresses = [address]
|
270
270
|
@person.addresses.first.street.should == "New Street"
|
271
271
|
end
|
@@ -300,7 +300,7 @@ describe Mongoid::Document do
|
|
300
300
|
end
|
301
301
|
|
302
302
|
it "sets the attributes for the association" do
|
303
|
-
name = Name.new(:first_name => "New Name"
|
303
|
+
name = Name.new(:first_name => "New Name")
|
304
304
|
@person.name = name
|
305
305
|
@person.name.first_name.should == "New Name"
|
306
306
|
end
|
@@ -439,8 +439,8 @@ describe Mongoid::Document do
|
|
439
439
|
before do
|
440
440
|
@attributes = { :title => "Sir",
|
441
441
|
:addresses => [
|
442
|
-
{ :street => "Street 1"
|
443
|
-
{ :street => "Street 2"
|
442
|
+
{ :street => "Street 1" },
|
443
|
+
{ :street => "Street 2" } ] }
|
444
444
|
@person = Person.new(@attributes)
|
445
445
|
end
|
446
446
|
|
@@ -479,6 +479,15 @@ describe Mongoid::Document do
|
|
479
479
|
|
480
480
|
end
|
481
481
|
|
482
|
+
describe "#select" do
|
483
|
+
|
484
|
+
it "returns a new criteria with select conditions added" do
|
485
|
+
criteria = Person.select(:title, :age)
|
486
|
+
criteria.options.should == { :fields => [ :title, :age ] }
|
487
|
+
end
|
488
|
+
|
489
|
+
end
|
490
|
+
|
482
491
|
describe "#to_param" do
|
483
492
|
|
484
493
|
it "returns the id" do
|
@@ -488,6 +497,24 @@ describe Mongoid::Document do
|
|
488
497
|
|
489
498
|
end
|
490
499
|
|
500
|
+
describe "#write_attribute" do
|
501
|
+
|
502
|
+
context "when attribute does not exist" do
|
503
|
+
|
504
|
+
before do
|
505
|
+
Person.field :weight, :default => 100
|
506
|
+
@person = Person.new
|
507
|
+
end
|
508
|
+
|
509
|
+
it "returns the default value" do
|
510
|
+
@person.weight = nil
|
511
|
+
@person.weight.should == 100
|
512
|
+
end
|
513
|
+
|
514
|
+
end
|
515
|
+
|
516
|
+
end
|
517
|
+
|
491
518
|
context "validations" do
|
492
519
|
|
493
520
|
context "when defining using macros" do
|
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.5
|
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-18 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|