mongoid 1.0.6 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/HISTORY +39 -0
  2. data/Rakefile +1 -1
  3. data/VERSION +1 -1
  4. data/lib/mongoid.rb +3 -1
  5. data/lib/mongoid/associations.rb +5 -5
  6. data/lib/mongoid/associations/has_many.rb +8 -2
  7. data/lib/mongoid/associations/has_many_related.rb +10 -4
  8. data/lib/mongoid/attributes.rb +19 -13
  9. data/lib/mongoid/commands.rb +12 -6
  10. data/lib/mongoid/commands/create.rb +2 -2
  11. data/lib/mongoid/commands/delete_all.rb +1 -1
  12. data/lib/mongoid/commands/save.rb +2 -2
  13. data/lib/mongoid/components.rb +1 -0
  14. data/lib/mongoid/contexts.rb +4 -0
  15. data/lib/mongoid/contexts/enumerable.rb +105 -0
  16. data/lib/mongoid/contexts/mongo.rb +228 -0
  17. data/lib/mongoid/contexts/paging.rb +42 -0
  18. data/lib/mongoid/criteria.rb +42 -191
  19. data/lib/mongoid/document.rb +19 -13
  20. data/lib/mongoid/extensions.rb +1 -0
  21. data/lib/mongoid/extensions/array/accessors.rb +3 -1
  22. data/lib/mongoid/extensions/float/conversions.rb +1 -1
  23. data/lib/mongoid/extensions/hash/accessors.rb +1 -1
  24. data/lib/mongoid/extensions/integer/conversions.rb +1 -0
  25. data/lib/mongoid/fields.rb +6 -5
  26. data/lib/mongoid/matchers.rb +36 -0
  27. data/lib/mongoid/matchers/all.rb +11 -0
  28. data/lib/mongoid/matchers/default.rb +20 -0
  29. data/lib/mongoid/matchers/exists.rb +13 -0
  30. data/lib/mongoid/matchers/gt.rb +11 -0
  31. data/lib/mongoid/matchers/gte.rb +11 -0
  32. data/lib/mongoid/matchers/in.rb +11 -0
  33. data/lib/mongoid/matchers/lt.rb +11 -0
  34. data/lib/mongoid/matchers/lte.rb +11 -0
  35. data/lib/mongoid/matchers/ne.rb +11 -0
  36. data/lib/mongoid/matchers/nin.rb +11 -0
  37. data/lib/mongoid/matchers/size.rb +11 -0
  38. data/lib/mongoid/scope.rb +17 -1
  39. data/mongoid.gemspec +51 -5
  40. data/spec/integration/mongoid/associations_spec.rb +67 -5
  41. data/spec/integration/mongoid/attributes_spec.rb +22 -0
  42. data/spec/integration/mongoid/commands_spec.rb +51 -12
  43. data/spec/integration/mongoid/criteria_spec.rb +3 -3
  44. data/spec/integration/mongoid/document_spec.rb +8 -8
  45. data/spec/integration/mongoid/finders_spec.rb +1 -1
  46. data/spec/integration/mongoid/inheritance_spec.rb +6 -0
  47. data/spec/integration/mongoid/named_scope_spec.rb +1 -1
  48. data/spec/spec_helper.rb +47 -6
  49. data/spec/unit/mongoid/associations/has_many_related_spec.rb +42 -0
  50. data/spec/unit/mongoid/associations/has_many_spec.rb +40 -1
  51. data/spec/unit/mongoid/attributes_spec.rb +1 -1
  52. data/spec/unit/mongoid/commands/create_spec.rb +3 -3
  53. data/spec/unit/mongoid/commands/delete_all_spec.rb +2 -2
  54. data/spec/unit/mongoid/commands/save_spec.rb +2 -2
  55. data/spec/unit/mongoid/commands_spec.rb +12 -12
  56. data/spec/unit/mongoid/contexts/enumerable_spec.rb +208 -0
  57. data/spec/unit/mongoid/contexts/mongo_spec.rb +370 -0
  58. data/spec/unit/mongoid/criteria_spec.rb +182 -21
  59. data/spec/unit/mongoid/extensions/array/accessors_spec.rb +9 -9
  60. data/spec/unit/mongoid/extensions/date/conversions_spec.rb +2 -1
  61. data/spec/unit/mongoid/extensions/float/conversions_spec.rb +4 -4
  62. data/spec/unit/mongoid/extensions/integer/conversions_spec.rb +1 -1
  63. data/spec/unit/mongoid/fields_spec.rb +3 -3
  64. data/spec/unit/mongoid/identity_spec.rb +2 -2
  65. data/spec/unit/mongoid/matchers/all_spec.rb +27 -0
  66. data/spec/unit/mongoid/matchers/default_spec.rb +27 -0
  67. data/spec/unit/mongoid/matchers/exists_spec.rb +56 -0
  68. data/spec/unit/mongoid/matchers/gt_spec.rb +39 -0
  69. data/spec/unit/mongoid/matchers/gte_spec.rb +49 -0
  70. data/spec/unit/mongoid/matchers/in_spec.rb +27 -0
  71. data/spec/unit/mongoid/matchers/lt_spec.rb +39 -0
  72. data/spec/unit/mongoid/matchers/lte_spec.rb +49 -0
  73. data/spec/unit/mongoid/matchers/ne_spec.rb +27 -0
  74. data/spec/unit/mongoid/matchers/nin_spec.rb +27 -0
  75. data/spec/unit/mongoid/matchers/size_spec.rb +27 -0
  76. data/spec/unit/mongoid/matchers_spec.rb +329 -0
  77. data/spec/unit/mongoid/scope_spec.rb +70 -0
  78. data/spec/unit/mongoid/timestamps_spec.rb +2 -2
  79. metadata +50 -4
@@ -7,6 +7,111 @@ describe Mongoid::Criteria do
7
7
  @canvas_criteria = Mongoid::Criteria.new(Canvas)
8
8
  end
9
9
 
10
+ describe "#+" do
11
+
12
+ before do
13
+ @sir = Person.new(:title => "Sir")
14
+ @madam = Person.new(:title => "Madam")
15
+ @canvas = Canvas.new
16
+ end
17
+
18
+ context "when the criteria has not been executed" do
19
+
20
+ before do
21
+ @collection = mock
22
+ @cursor = stub(:count => 1, :collect => [ @sir, @madam ])
23
+ Person.expects(:collection).returns(@collection)
24
+ @collection.expects(:find).returns(@cursor)
25
+ end
26
+
27
+ it "executes the criteria and concats the results" do
28
+ results = @criteria + [ @canvas ]
29
+ results.should == [ @sir, @madam, @canvas ]
30
+ end
31
+
32
+ end
33
+
34
+ context "when the criteria has been executed" do
35
+
36
+ before do
37
+ @criteria.instance_variable_set(:@collection, [ @sir, @madam ])
38
+ end
39
+
40
+ it "concats the results" do
41
+ results = @criteria + [ @canvas ]
42
+ results.should == [ @sir, @madam, @canvas ]
43
+ end
44
+
45
+ end
46
+
47
+ context "when the other is a criteria" do
48
+
49
+ before do
50
+ @criteria.instance_variable_set(:@collection, [ @sir, @madam ])
51
+ @canvas_criteria.instance_variable_set(:@collection, [ @canvas ])
52
+ end
53
+
54
+ it "concats the results" do
55
+ results = @criteria + @canvas_criteria
56
+ results.should == [ @sir, @madam, @canvas ]
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ describe "#-" do
64
+
65
+ before do
66
+ @sir = Person.new(:title => "Sir")
67
+ @madam = Person.new(:title => "Madam")
68
+ end
69
+
70
+ context "when the criteria has not been executed" do
71
+
72
+ before do
73
+ @collection = mock
74
+ @cursor = stub(:count => 1, :collect => [ @sir, @sir, @madam ])
75
+ Person.expects(:collection).returns(@collection)
76
+ @collection.expects(:find).returns(@cursor)
77
+ end
78
+
79
+ it "executes the criteria and returns the difference" do
80
+ results = @criteria - [ @sir ]
81
+ results.should == [ @madam ]
82
+ end
83
+
84
+ end
85
+
86
+ context "when the criteria has been executed" do
87
+
88
+ before do
89
+ @criteria.instance_variable_set(:@collection, [@sir, @sir, @madam])
90
+ end
91
+
92
+ it "returns the difference" do
93
+ results = @criteria - [ @sir ]
94
+ results.should == [ @madam ]
95
+ end
96
+
97
+ end
98
+
99
+ context "when the other is a criteria" do
100
+
101
+ before do
102
+ @criteria.instance_variable_set(:@collection, [@sir, @sir, @madam])
103
+ @canvas_criteria.instance_variable_set(:@collection, [@sir])
104
+ end
105
+
106
+ it "returns the difference" do
107
+ results = @criteria - @canvas_criteria
108
+ results.should == [ @madam ]
109
+ end
110
+
111
+ end
112
+
113
+ end
114
+
10
115
  describe "#[]" do
11
116
 
12
117
  before do
@@ -89,7 +194,59 @@ describe Mongoid::Criteria do
89
194
 
90
195
  end
91
196
 
92
- describe "#collect" do
197
+ describe "#context" do
198
+
199
+ context "when the context has been set" do
200
+
201
+ before do
202
+ @context = stub
203
+ @criteria.instance_variable_set(:@context, @context)
204
+ end
205
+
206
+ it "returns the memoized context" do
207
+ @criteria.context.should == @context
208
+ end
209
+
210
+ end
211
+
212
+ context "when the context has not been set" do
213
+
214
+ before do
215
+ @context = stub
216
+ end
217
+
218
+ it "creates a new context" do
219
+ Mongoid::Contexts::Mongo.expects(:new).with(
220
+ @criteria.selector, @criteria.options, @criteria.klass
221
+ ).returns(@context)
222
+ @criteria.context.should == @context
223
+ end
224
+
225
+ end
226
+
227
+ context "when the class is embedded" do
228
+
229
+ before do
230
+ @criteria = Mongoid::Criteria.new(Address)
231
+ end
232
+
233
+ it "returns an enumerable context" do
234
+ @criteria.context.should be_a_kind_of(Mongoid::Contexts::Enumerable)
235
+ end
236
+
237
+ end
238
+
239
+ context "when the class is not embedded" do
240
+
241
+ it "returns a mongo context" do
242
+ @criteria.context.should be_a_kind_of(Mongoid::Contexts::Mongo)
243
+ end
244
+
245
+ end
246
+
247
+ end
248
+
249
+ describe "#entries" do
93
250
 
94
251
  context "filtering" do
95
252
 
@@ -101,7 +258,7 @@ describe Mongoid::Criteria do
101
258
  end
102
259
 
103
260
  it "filters out unused params" do
104
- @criteria.collect
261
+ @criteria.entries
105
262
  @criteria.options[:page].should be_nil
106
263
  @criteria.options[:per_page].should be_nil
107
264
  end
@@ -119,7 +276,7 @@ describe Mongoid::Criteria do
119
276
  end
120
277
 
121
278
  it "adds the count instance variable" do
122
- @criteria.collect.should == []
279
+ @criteria.entries.should == []
123
280
  @criteria.count.should == 44
124
281
  end
125
282
 
@@ -132,7 +289,7 @@ describe Mongoid::Criteria do
132
289
  collection = mock
133
290
  Person.expects(:collection).returns(collection)
134
291
  collection.expects(:find).with(@criteria.selector, @criteria.options).returns([])
135
- criteria.collect.should == []
292
+ criteria.entries.should == []
136
293
  end
137
294
 
138
295
  end
@@ -141,18 +298,6 @@ describe Mongoid::Criteria do
141
298
 
142
299
  describe "#count" do
143
300
 
144
- context "when criteria has not been executed" do
145
-
146
- before do
147
- @criteria.instance_variable_set(:@count, 34)
148
- end
149
-
150
- it "returns a count from the cursor" do
151
- @criteria.count.should == 34
152
- end
153
-
154
- end
155
-
156
301
  context "when criteria has been executed" do
157
302
 
158
303
  before do
@@ -508,7 +653,7 @@ describe Mongoid::Criteria do
508
653
  describe "#max" do
509
654
 
510
655
  before do
511
- @reduce = Mongoid::Criteria::MAX_REDUCE.gsub("[field]", "age")
656
+ @reduce = Mongoid::Contexts::Mongo::MAX_REDUCE.gsub("[field]", "age")
512
657
  @collection = mock
513
658
  Person.expects(:collection).returns(@collection)
514
659
  end
@@ -564,6 +709,22 @@ describe Mongoid::Criteria do
564
709
  @criteria.selector.should == @selector
565
710
  @criteria.options.should == @options
566
711
  end
712
+
713
+ end
714
+
715
+ context "when the other has a document collection" do
716
+
717
+ before do
718
+ @documents = [ stub ]
719
+ @other = Mongoid::Criteria.new(Person)
720
+ @other.documents = @documents
721
+ end
722
+
723
+ it "merges the documents collection in" do
724
+ @criteria.merge(@other)
725
+ @criteria.documents.should == @documents
726
+ end
727
+
567
728
  end
568
729
 
569
730
  end
@@ -605,7 +766,7 @@ describe Mongoid::Criteria do
605
766
  describe "#[]" do
606
767
 
607
768
  it "collects the criteria and calls []" do
608
- @criteria.expects(:collect).returns([@document])
769
+ @criteria.expects(:entries).returns([@document])
609
770
  @criteria[0].should == @document
610
771
  end
611
772
 
@@ -614,7 +775,7 @@ describe Mongoid::Criteria do
614
775
  describe "#rand" do
615
776
 
616
777
  it "collects the criteria and call rand" do
617
- @criteria.expects(:collect).returns(@array)
778
+ @criteria.expects(:entries).returns(@array)
618
779
  @array.expects(:send).with(:rand).returns(@document)
619
780
  @criteria.rand
620
781
  end
@@ -628,7 +789,7 @@ describe Mongoid::Criteria do
628
789
  describe "#min" do
629
790
 
630
791
  before do
631
- @reduce = Mongoid::Criteria::MIN_REDUCE.gsub("[field]", "age")
792
+ @reduce = Mongoid::Contexts::Mongo::MIN_REDUCE.gsub("[field]", "age")
632
793
  @collection = mock
633
794
  Person.expects(:collection).returns(@collection)
634
795
  end
@@ -917,7 +1078,7 @@ describe Mongoid::Criteria do
917
1078
  context "when klass not provided" do
918
1079
 
919
1080
  before do
920
- @reduce = Mongoid::Criteria::SUM_REDUCE.gsub("[field]", "age")
1081
+ @reduce = Mongoid::Contexts::Mongo::SUM_REDUCE.gsub("[field]", "age")
921
1082
  @collection = mock
922
1083
  Person.expects(:collection).returns(@collection)
923
1084
  end
@@ -7,12 +7,12 @@ describe Mongoid::Extensions::Array::Accessors do
7
7
  context "when the attributes exist" do
8
8
 
9
9
  before do
10
- @array = [{ :_id => 1, :name => "James T. Kirk" }]
10
+ @array = [{ "_id" => 1, "name" => "James T. Kirk" }]
11
11
  end
12
12
 
13
13
  it "overwrites with the new attributes" do
14
- @array.update({ :_id => 1, :name => "Spock" })
15
- @array.first[:name].should == "Spock"
14
+ @array.update({ "_id" => 1, "name" => "Spock" })
15
+ @array.first["name"].should == "Spock"
16
16
  end
17
17
 
18
18
  end
@@ -20,13 +20,13 @@ describe Mongoid::Extensions::Array::Accessors do
20
20
  context "when the attributes do not exist" do
21
21
 
22
22
  before do
23
- @array = [{ :_id => 1, :name => "James T. Kirk" }]
23
+ @array = [{ "_id" => 1, "name" => "James T. Kirk" }]
24
24
  end
25
25
 
26
26
  it "appends the new attributes" do
27
- @array.update({ :_id => 2, :name => "Scotty" })
27
+ @array.update({ "_id" => 2, "name" => "Scotty" })
28
28
  @array.size.should == 2
29
- @array.last[:name].should == "Scotty"
29
+ @array.last["name"].should == "Scotty"
30
30
  end
31
31
 
32
32
  end
@@ -34,13 +34,13 @@ describe Mongoid::Extensions::Array::Accessors do
34
34
  context "when the new attribtues have no id" do
35
35
 
36
36
  before do
37
- @array = [{ :_id => 1, :name => "James T. Kirk" }]
37
+ @array = [{ "_id" => 1, :name => "James T. Kirk" }]
38
38
  end
39
39
 
40
40
  it "appends the new attributes" do
41
- @array.update({:name => "Scotty" })
41
+ @array.update({"name" => "Scotty" })
42
42
  @array.size.should == 2
43
- @array.last[:name].should == "Scotty"
43
+ @array.last["name"].should == "Scotty"
44
44
  end
45
45
 
46
46
  end
@@ -21,7 +21,8 @@ describe Mongoid::Extensions::Date::Conversions do
21
21
  context "when string is a date" do
22
22
 
23
23
  it "returns a time from the string" do
24
- Date.set("01/15/2007").should == Date.new(2007, 1, 15).at_midnight.utc
24
+ date = RUBY_VERSION.start_with?("1.9") ? "15/01/2007" : "01/15/2007"
25
+ Date.set(date).should == Date.new(2007, 1, 15).at_midnight.utc
25
26
  end
26
27
 
27
28
  end
@@ -12,9 +12,9 @@ describe Mongoid::Extensions::Float::Conversions do
12
12
 
13
13
  end
14
14
 
15
- context "when the string is not a number" do
15
+ context "when the value is not a number" do
16
16
 
17
- context "when the string is non numerical" do
17
+ context "when the value is non numerical" do
18
18
 
19
19
  it "returns the string" do
20
20
  Float.set("foo").should == "foo"
@@ -33,7 +33,7 @@ describe Mongoid::Extensions::Float::Conversions do
33
33
  context "when the string is empty" do
34
34
 
35
35
  it "returns 0.0" do
36
- Float.set("").should == 0.0
36
+ Float.set("").should be_nil
37
37
  end
38
38
 
39
39
  end
@@ -41,7 +41,7 @@ describe Mongoid::Extensions::Float::Conversions do
41
41
  context "when the string is nil" do
42
42
 
43
43
  it "returns 0.0" do
44
- Float.set(nil).should == 0.0
44
+ Float.set(nil).should be_nil
45
45
  end
46
46
 
47
47
  end
@@ -33,7 +33,7 @@ describe Mongoid::Extensions::Integer::Conversions do
33
33
  context "when the string is empty" do
34
34
 
35
35
  it "returns an empty string" do
36
- Integer.set("").should be_blank
36
+ Integer.set("").should be_nil
37
37
  end
38
38
 
39
39
  end
@@ -95,17 +95,17 @@ describe Mongoid::Fields do
95
95
  end
96
96
 
97
97
  it "uses the alias to write the attribute" do
98
- @person.expects(:write_attribute).with(:aliased, true)
98
+ @person.expects(:write_attribute).with("aliased", true)
99
99
  @person.alias = true
100
100
  end
101
101
 
102
102
  it "uses the alias to read the attribute" do
103
- @person.expects(:read_attribute).with(:aliased)
103
+ @person.expects(:read_attribute).with("aliased")
104
104
  @person.alias
105
105
  end
106
106
 
107
107
  it "uses the alias for the query method" do
108
- @person.expects(:read_attribute).with(:aliased)
108
+ @person.expects(:read_attribute).with("aliased")
109
109
  @person.alias?
110
110
  end
111
111
 
@@ -19,7 +19,7 @@ describe Mongoid::Identity do
19
19
 
20
20
  before do
21
21
  @address = Address.allocate
22
- @address.instance_variable_set(:@attributes, { :street => "Market St"})
22
+ @address.instance_variable_set(:@attributes, { "street" => "Market St"})
23
23
  end
24
24
 
25
25
  it "sets the id to the composite key" do
@@ -51,7 +51,7 @@ describe Mongoid::Identity do
51
51
 
52
52
  before do
53
53
  @person = Person.allocate
54
- @person.instance_variable_set(:@attributes, { :_id => "5" })
54
+ @person.instance_variable_set(:@attributes, { "_id" => "5" })
55
55
  end
56
56
 
57
57
  it "returns the existing id" do
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Matchers::All do
4
+
5
+ let(:matcher) { Mongoid::Matchers::All.new(["first", "second"]) }
6
+
7
+ describe "#matches?" do
8
+
9
+ context "when the values are equal" do
10
+
11
+ it "returns true" do
12
+ matcher.matches?("$all" => ["first", "second"]).should be_true
13
+ end
14
+
15
+ end
16
+
17
+ context "when the values are not equal" do
18
+
19
+ it "returns false" do
20
+ matcher.matches?("$all" => ["first"]).should be_false
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Matchers::Default do
4
+
5
+ let(:matcher) { Mongoid::Matchers::Default.new("Testing") }
6
+
7
+ describe "#matches?" do
8
+
9
+ context "when the values are equal" do
10
+
11
+ it "returns true" do
12
+ matcher.matches?("Testing").should be_true
13
+ end
14
+
15
+ end
16
+
17
+ context "when the values are not equal" do
18
+
19
+ it "returns false" do
20
+ matcher.matches?("Other").should be_false
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+ end