mongoid 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +24 -0
- data/Rakefile +8 -3
- data/VERSION +1 -1
- data/lib/mongoid/associations.rb +15 -0
- data/lib/mongoid/associations/belongs_to.rb +5 -0
- data/lib/mongoid/associations/has_many.rb +5 -0
- data/lib/mongoid/associations/has_one.rb +5 -0
- data/lib/mongoid/criteria.rb +6 -13
- data/mongoid.gemspec +7 -1
- data/spec/unit/mongoid/associations/belongs_to_spec.rb +9 -1
- data/spec/unit/mongoid/associations/has_many_spec.rb +16 -16
- data/spec/unit/mongoid/associations/has_one_spec.rb +9 -1
- data/spec/unit/mongoid/associations_spec.rb +8 -0
- data/spec/unit/mongoid/criteria_spec.rb +0 -39
- metadata +21 -1
data/README.textile
CHANGED
@@ -116,6 +116,30 @@ New School:
|
|
116
116
|
Criteria.translate(:conditions => { :title => "Sir" }).execute
|
117
117
|
</pre>
|
118
118
|
|
119
|
+
Chaining Scopes:
|
120
|
+
|
121
|
+
Class methods that return criteria, either through the Criteria API or the traditional Finder API, can
|
122
|
+
be chained, similar to DataMapper.
|
123
|
+
|
124
|
+
<pre>
|
125
|
+
class Person < Mongoid::Document
|
126
|
+
field :title
|
127
|
+
field :age, :type => Integer
|
128
|
+
|
129
|
+
class << self
|
130
|
+
def knighted
|
131
|
+
criteria.where(:title => "Sir")
|
132
|
+
end
|
133
|
+
def old
|
134
|
+
criteria.where(:age => { "$gt" > 70 })
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
Person.old.knighted
|
140
|
+
Person.old.paginate
|
141
|
+
</pre>
|
142
|
+
|
119
143
|
Paginate Document search results:
|
120
144
|
|
121
145
|
<pre>
|
data/Rakefile
CHANGED
@@ -12,11 +12,15 @@ begin
|
|
12
12
|
gem.email = "durran@gmail.com"
|
13
13
|
gem.homepage = "http://github.com/durran/mongoid"
|
14
14
|
gem.authors = ["Durran Jordan"]
|
15
|
+
|
15
16
|
gem.add_dependency("durran-validatable", "1.8.2")
|
16
17
|
gem.add_dependency("leshill-will_paginate", "2.3.11")
|
17
18
|
gem.add_dependency("activesupport", "2.3.4")
|
18
19
|
gem.add_dependency("mongo", "0.17.1")
|
19
20
|
gem.add_dependency("mongo_ext", "0.17.1")
|
21
|
+
|
22
|
+
gem.add_development_dependency("rspec", "1.2.9")
|
23
|
+
gem.add_development_dependency("mocha", "0.9.8")
|
20
24
|
end
|
21
25
|
Jeweler::GemcutterTasks.new
|
22
26
|
rescue LoadError
|
@@ -25,13 +29,13 @@ end
|
|
25
29
|
|
26
30
|
Spec::Rake::SpecTask.new(:spec) do |spec|
|
27
31
|
spec.pattern = "spec/**/*_spec.rb"
|
28
|
-
spec.spec_opts = [
|
32
|
+
spec.spec_opts = ["--options", "spec/spec.opts"]
|
29
33
|
end
|
30
34
|
|
31
35
|
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
32
36
|
spec.libs << "lib" << "spec"
|
33
37
|
spec.pattern = "spec/**/*_spec.rb"
|
34
|
-
spec.spec_opts = [
|
38
|
+
spec.spec_opts = ["--options", "spec/spec.opts"]
|
35
39
|
spec.rcov = true
|
36
40
|
end
|
37
41
|
|
@@ -48,4 +52,5 @@ Rake::RDocTask.new do |rdoc|
|
|
48
52
|
rdoc.rdoc_files.include("lib/**/*.rb")
|
49
53
|
end
|
50
54
|
|
51
|
-
task :default => ["
|
55
|
+
task :default => ["spec"]
|
56
|
+
task :metrics => ["rcov", "metrics:all"]
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.1
|
data/lib/mongoid/associations.rb
CHANGED
@@ -91,6 +91,21 @@ module Mongoid # :nodoc:
|
|
91
91
|
add_association(Associations::HasOne, Associations::Options.new(options.merge(:name => name)))
|
92
92
|
end
|
93
93
|
|
94
|
+
# Returns the macro associated with the supplied association name. This
|
95
|
+
# will return has_one, has_many, belongs_to or nil.
|
96
|
+
#
|
97
|
+
# Options:
|
98
|
+
#
|
99
|
+
# name: The association name.
|
100
|
+
#
|
101
|
+
# Example:
|
102
|
+
#
|
103
|
+
# <tt>Person.reflect_on_association(:addresses)</tt>
|
104
|
+
def reflect_on_association(name)
|
105
|
+
association = associations[name]
|
106
|
+
association ? association.macro : nil
|
107
|
+
end
|
108
|
+
|
94
109
|
private
|
95
110
|
# Adds the association to the associations hash with the type as the key,
|
96
111
|
# then adds the accessors for the association.
|
@@ -22,6 +22,11 @@ module Mongoid #:nodoc:
|
|
22
22
|
end
|
23
23
|
|
24
24
|
class << self
|
25
|
+
# Returns the macro used to create the association.
|
26
|
+
def macro
|
27
|
+
:belongs_to
|
28
|
+
end
|
29
|
+
|
25
30
|
# Perform an update of the relationship of the parent and child. This
|
26
31
|
# is initialized by setting a parent object as the association on the
|
27
32
|
# +Document+. Will properly set a has_one or a has_many.
|
@@ -82,6 +82,11 @@ module Mongoid #:nodoc:
|
|
82
82
|
end
|
83
83
|
|
84
84
|
class << self
|
85
|
+
# Returns the macro used to create the association.
|
86
|
+
def macro
|
87
|
+
:has_many
|
88
|
+
end
|
89
|
+
|
85
90
|
# Perform an update of the relationship of the parent and child. This
|
86
91
|
# is initialized by setting the has_many to the supplied +Enumerable+
|
87
92
|
# and setting up the parentization.
|
@@ -35,6 +35,11 @@ module Mongoid #:nodoc:
|
|
35
35
|
end
|
36
36
|
|
37
37
|
class << self
|
38
|
+
# Returns the macro used to create the association.
|
39
|
+
def macro
|
40
|
+
:has_one
|
41
|
+
end
|
42
|
+
|
38
43
|
# Perform an update of the relationship of the parent and child. This
|
39
44
|
# will assimilate the child +Document+ into the parent's object graph.
|
40
45
|
#
|
data/lib/mongoid/criteria.rb
CHANGED
@@ -31,7 +31,7 @@ module Mongoid #:nodoc:
|
|
31
31
|
when Criteria
|
32
32
|
self.selector == other.selector && self.options == other.options
|
33
33
|
when Enumerable
|
34
|
-
execute
|
34
|
+
@collection ||= execute
|
35
35
|
return (@collection == other)
|
36
36
|
else
|
37
37
|
return false
|
@@ -81,9 +81,7 @@ module Mongoid #:nodoc:
|
|
81
81
|
#
|
82
82
|
# Returns: <tt>Integer</tt>
|
83
83
|
def count
|
84
|
-
|
85
|
-
@klass = klass if klass
|
86
|
-
return @klass.collection.find(@selector, @options.dup).count
|
84
|
+
@count ||= @klass.collection.find(@selector, @options.dup).count
|
87
85
|
end
|
88
86
|
|
89
87
|
# Iterate over each +Document+ in the results. This can take an optional
|
@@ -261,13 +259,8 @@ module Mongoid #:nodoc:
|
|
261
259
|
#
|
262
260
|
# <tt>criteria.merge({ :conditions => { :title => "Sir" } })</tt>
|
263
261
|
def merge(other)
|
264
|
-
|
265
|
-
|
266
|
-
merge(self.class.translate(@klass, other))
|
267
|
-
else
|
268
|
-
@selector.update(other.selector)
|
269
|
-
@options.update(other.options)
|
270
|
-
end
|
262
|
+
@selector.update(other.selector)
|
263
|
+
@options.update(other.options)
|
271
264
|
end
|
272
265
|
|
273
266
|
# Used for chaining +Criteria+ scopes together in the for of class methods
|
@@ -469,9 +462,9 @@ module Mongoid #:nodoc:
|
|
469
462
|
attributes = @klass.collection.find(@selector, @options.dup)
|
470
463
|
if attributes
|
471
464
|
@count = attributes.count
|
472
|
-
|
465
|
+
attributes.collect { |doc| @klass.instantiate(doc) }
|
473
466
|
else
|
474
|
-
|
467
|
+
[]
|
475
468
|
end
|
476
469
|
end
|
477
470
|
|
data/mongoid.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "0.9.
|
8
|
+
s.version = "0.9.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"]
|
@@ -167,12 +167,16 @@ Gem::Specification.new do |s|
|
|
167
167
|
s.add_runtime_dependency(%q<activesupport>, ["= 2.3.4"])
|
168
168
|
s.add_runtime_dependency(%q<mongo>, ["= 0.17.1"])
|
169
169
|
s.add_runtime_dependency(%q<mongo_ext>, ["= 0.17.1"])
|
170
|
+
s.add_development_dependency(%q<rspec>, ["= 1.2.9"])
|
171
|
+
s.add_development_dependency(%q<mocha>, ["= 0.9.8"])
|
170
172
|
else
|
171
173
|
s.add_dependency(%q<durran-validatable>, ["= 1.8.2"])
|
172
174
|
s.add_dependency(%q<leshill-will_paginate>, ["= 2.3.11"])
|
173
175
|
s.add_dependency(%q<activesupport>, ["= 2.3.4"])
|
174
176
|
s.add_dependency(%q<mongo>, ["= 0.17.1"])
|
175
177
|
s.add_dependency(%q<mongo_ext>, ["= 0.17.1"])
|
178
|
+
s.add_dependency(%q<rspec>, ["= 1.2.9"])
|
179
|
+
s.add_dependency(%q<mocha>, ["= 0.9.8"])
|
176
180
|
end
|
177
181
|
else
|
178
182
|
s.add_dependency(%q<durran-validatable>, ["= 1.8.2"])
|
@@ -180,6 +184,8 @@ Gem::Specification.new do |s|
|
|
180
184
|
s.add_dependency(%q<activesupport>, ["= 2.3.4"])
|
181
185
|
s.add_dependency(%q<mongo>, ["= 0.17.1"])
|
182
186
|
s.add_dependency(%q<mongo_ext>, ["= 0.17.1"])
|
187
|
+
s.add_dependency(%q<rspec>, ["= 1.2.9"])
|
188
|
+
s.add_dependency(%q<mocha>, ["= 0.9.8"])
|
183
189
|
end
|
184
190
|
end
|
185
191
|
|
@@ -50,7 +50,15 @@ describe Mongoid::Associations::BelongsTo do
|
|
50
50
|
|
51
51
|
end
|
52
52
|
|
53
|
-
describe "
|
53
|
+
describe ".macro" do
|
54
|
+
|
55
|
+
it "returns :belongs_to" do
|
56
|
+
Mongoid::Associations::BelongsTo.macro.should == :belongs_to
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
describe ".update" do
|
54
62
|
|
55
63
|
context "when child is a has one" do
|
56
64
|
|
@@ -116,21 +116,6 @@ describe Mongoid::Associations::HasMany do
|
|
116
116
|
|
117
117
|
end
|
118
118
|
|
119
|
-
describe "#push" do
|
120
|
-
|
121
|
-
before do
|
122
|
-
@association = Mongoid::Associations::HasMany.new(@document, Mongoid::Associations::Options.new(:name => :addresses))
|
123
|
-
@address = Address.new
|
124
|
-
end
|
125
|
-
|
126
|
-
it "adds the parent document before appending to the array" do
|
127
|
-
@association.push @address
|
128
|
-
@association.length.should == 3
|
129
|
-
@address.parent.should == @document
|
130
|
-
end
|
131
|
-
|
132
|
-
end
|
133
|
-
|
134
119
|
describe "#find" do
|
135
120
|
|
136
121
|
before do
|
@@ -207,6 +192,14 @@ describe Mongoid::Associations::HasMany do
|
|
207
192
|
|
208
193
|
end
|
209
194
|
|
195
|
+
describe ".macro" do
|
196
|
+
|
197
|
+
it "returns :has_many" do
|
198
|
+
Mongoid::Associations::HasMany.macro.should == :has_many
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
202
|
+
|
210
203
|
describe "#push" do
|
211
204
|
|
212
205
|
before do
|
@@ -214,6 +207,13 @@ describe Mongoid::Associations::HasMany do
|
|
214
207
|
@document,
|
215
208
|
Mongoid::Associations::Options.new(:name => :addresses)
|
216
209
|
)
|
210
|
+
@address = Address.new
|
211
|
+
end
|
212
|
+
|
213
|
+
it "adds the parent document before appending to the array" do
|
214
|
+
@association.push @address
|
215
|
+
@association.length.should == 3
|
216
|
+
@address.parent.should == @document
|
217
217
|
end
|
218
218
|
|
219
219
|
it "appends the document to the end of the array" do
|
@@ -223,7 +223,7 @@ describe Mongoid::Associations::HasMany do
|
|
223
223
|
|
224
224
|
end
|
225
225
|
|
226
|
-
describe "
|
226
|
+
describe ".update" do
|
227
227
|
|
228
228
|
before do
|
229
229
|
@address = Address.new(:street => "Madison Ave")
|
@@ -77,7 +77,15 @@ describe Mongoid::Associations::HasOne do
|
|
77
77
|
|
78
78
|
end
|
79
79
|
|
80
|
-
describe "
|
80
|
+
describe ".macro" do
|
81
|
+
|
82
|
+
it "returns :has_many" do
|
83
|
+
Mongoid::Associations::HasOne.macro.should == :has_one
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
describe ".update" do
|
81
89
|
|
82
90
|
before do
|
83
91
|
@name = Name.new(:first_name => "Donald")
|
@@ -232,4 +232,12 @@ describe Mongoid::Associations do
|
|
232
232
|
|
233
233
|
end
|
234
234
|
|
235
|
+
describe ".reflect_on_association" do
|
236
|
+
|
237
|
+
it "returns the association class for the name" do
|
238
|
+
Person.reflect_on_association(:addresses).should == :has_many
|
239
|
+
end
|
240
|
+
|
241
|
+
end
|
242
|
+
|
235
243
|
end
|
@@ -430,45 +430,6 @@ describe Mongoid::Criteria do
|
|
430
430
|
|
431
431
|
end
|
432
432
|
|
433
|
-
context "with a hash" do
|
434
|
-
|
435
|
-
context "when hash has values" do
|
436
|
-
|
437
|
-
before do
|
438
|
-
@hash = { :conditions => { :name => "Rebecca" }, :sort => [[:name, :desc]] }
|
439
|
-
@selector = { :title => "Sir", :age => 30, :name => "Rebecca" }
|
440
|
-
@options = { :skip => 40, :limit => 20, :sort => [[:name, :desc]] }
|
441
|
-
@criteria.merge(@hash)
|
442
|
-
end
|
443
|
-
|
444
|
-
it "merges the conditions with the selector" do
|
445
|
-
@criteria.selector.should == @selector
|
446
|
-
end
|
447
|
-
|
448
|
-
it "merges all valid other values into the options" do
|
449
|
-
@criteria.options.should == @options
|
450
|
-
end
|
451
|
-
|
452
|
-
end
|
453
|
-
|
454
|
-
context "when hash is empty" do
|
455
|
-
|
456
|
-
before do
|
457
|
-
@hash = {}
|
458
|
-
@selector = { :title => "Sir", :age => 30 }
|
459
|
-
@options = { :skip => 40, :limit => 20 }
|
460
|
-
end
|
461
|
-
|
462
|
-
it "merges nothing" do
|
463
|
-
@criteria.merge(@hash)
|
464
|
-
@criteria.selector.should == @selector
|
465
|
-
@criteria.options.should == @options
|
466
|
-
end
|
467
|
-
|
468
|
-
end
|
469
|
-
|
470
|
-
end
|
471
|
-
|
472
433
|
end
|
473
434
|
|
474
435
|
describe "#method_missing" 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.9.
|
4
|
+
version: 0.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -62,6 +62,26 @@ dependencies:
|
|
62
62
|
- !ruby/object:Gem::Version
|
63
63
|
version: 0.17.1
|
64
64
|
version:
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: rspec
|
67
|
+
type: :development
|
68
|
+
version_requirement:
|
69
|
+
version_requirements: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - "="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 1.2.9
|
74
|
+
version:
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: mocha
|
77
|
+
type: :development
|
78
|
+
version_requirement:
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 0.9.8
|
84
|
+
version:
|
65
85
|
description:
|
66
86
|
email: durran@gmail.com
|
67
87
|
executables: []
|