mongo_mapper-unstable 2010.3.8 → 2010.06.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. data/README.rdoc +4 -8
  2. data/bin/mmconsole +1 -1
  3. data/examples/keys.rb +37 -0
  4. data/examples/plugins.rb +41 -0
  5. data/examples/querying.rb +35 -0
  6. data/examples/scopes.rb +52 -0
  7. data/lib/mongo_mapper/connection.rb +83 -0
  8. data/lib/mongo_mapper/document.rb +11 -329
  9. data/lib/mongo_mapper/embedded_document.rb +9 -38
  10. data/lib/mongo_mapper/exceptions.rb +30 -0
  11. data/lib/mongo_mapper/extensions/array.rb +19 -0
  12. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  13. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  14. data/lib/mongo_mapper/extensions/date.rb +25 -0
  15. data/lib/mongo_mapper/extensions/float.rb +14 -0
  16. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  17. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  18. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  19. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  20. data/lib/mongo_mapper/extensions/object.rb +27 -0
  21. data/lib/mongo_mapper/extensions/object_id.rb +30 -0
  22. data/lib/mongo_mapper/extensions/set.rb +20 -0
  23. data/lib/mongo_mapper/extensions/string.rb +18 -0
  24. data/lib/mongo_mapper/extensions/time.rb +29 -0
  25. data/lib/mongo_mapper/plugins/accessible.rb +44 -0
  26. data/lib/mongo_mapper/plugins/associations/base.rb +7 -6
  27. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +5 -6
  28. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +5 -6
  29. data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
  30. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +2 -1
  31. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +25 -39
  32. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +4 -4
  33. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +36 -46
  34. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
  35. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +5 -4
  36. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
  37. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
  38. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +7 -7
  39. data/lib/mongo_mapper/plugins/associations/proxy.rb +16 -8
  40. data/lib/mongo_mapper/plugins/associations.rb +14 -22
  41. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  42. data/lib/mongo_mapper/plugins/callbacks.rb +17 -5
  43. data/lib/mongo_mapper/plugins/clone.rb +10 -4
  44. data/lib/mongo_mapper/plugins/descendants.rb +3 -2
  45. data/lib/mongo_mapper/plugins/dirty.rb +1 -0
  46. data/lib/mongo_mapper/plugins/document.rb +41 -0
  47. data/lib/mongo_mapper/{support/find.rb → plugins/dynamic_querying/dynamic_finder.rb} +3 -36
  48. data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
  49. data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
  50. data/lib/mongo_mapper/plugins/equality.rb +4 -10
  51. data/lib/mongo_mapper/plugins/identity_map.rb +29 -23
  52. data/lib/mongo_mapper/plugins/indexes.rb +12 -0
  53. data/lib/mongo_mapper/plugins/inspect.rb +1 -0
  54. data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
  55. data/lib/mongo_mapper/plugins/keys.rb +85 -110
  56. data/lib/mongo_mapper/plugins/logger.rb +1 -0
  57. data/lib/mongo_mapper/plugins/modifiers.rb +41 -16
  58. data/lib/mongo_mapper/plugins/pagination.rb +5 -15
  59. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  60. data/lib/mongo_mapper/plugins/protected.rb +9 -1
  61. data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
  62. data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
  63. data/lib/mongo_mapper/plugins/querying.rb +176 -0
  64. data/lib/mongo_mapper/plugins/rails.rb +6 -1
  65. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  66. data/lib/mongo_mapper/plugins/sci.rb +32 -0
  67. data/lib/mongo_mapper/plugins/scopes.rb +21 -0
  68. data/lib/mongo_mapper/plugins/serialization.rb +5 -4
  69. data/lib/mongo_mapper/plugins/timestamps.rb +2 -1
  70. data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
  71. data/lib/mongo_mapper/plugins/validations.rb +9 -5
  72. data/lib/mongo_mapper/plugins.rb +1 -20
  73. data/lib/mongo_mapper/support/descendant_appends.rb +5 -6
  74. data/lib/mongo_mapper/version.rb +4 -0
  75. data/lib/mongo_mapper.rb +71 -128
  76. data/test/{NOTE_ON_TESTING → _NOTE_ON_TESTING} +0 -0
  77. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +5 -5
  78. data/test/functional/associations/test_belongs_to_proxy.rb +13 -21
  79. data/test/functional/associations/test_in_array_proxy.rb +7 -9
  80. data/test/functional/associations/test_many_documents_as_proxy.rb +5 -5
  81. data/test/functional/associations/test_many_documents_proxy.rb +186 -64
  82. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +22 -22
  83. data/test/functional/associations/test_many_embedded_proxy.rb +32 -32
  84. data/test/functional/associations/test_many_polymorphic_proxy.rb +47 -47
  85. data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
  86. data/test/functional/associations/test_one_proxy.rb +70 -49
  87. data/test/functional/test_accessible.rb +168 -0
  88. data/test/functional/test_associations.rb +11 -11
  89. data/test/functional/test_binary.rb +5 -5
  90. data/test/functional/test_caching.rb +76 -0
  91. data/test/functional/test_callbacks.rb +104 -34
  92. data/test/functional/test_dirty.rb +16 -16
  93. data/test/functional/test_document.rb +12 -924
  94. data/test/functional/test_dynamic_querying.rb +75 -0
  95. data/test/functional/test_embedded_document.rb +88 -8
  96. data/test/functional/test_identity_map.rb +41 -43
  97. data/test/functional/{test_indexing.rb → test_indexes.rb} +3 -5
  98. data/test/functional/test_logger.rb +1 -1
  99. data/test/functional/test_modifiers.rb +275 -181
  100. data/test/functional/test_pagination.rb +13 -15
  101. data/test/functional/test_protected.rb +25 -11
  102. data/test/functional/test_querying.rb +873 -0
  103. data/test/functional/test_safe.rb +76 -0
  104. data/test/functional/test_sci.rb +230 -0
  105. data/test/functional/test_scopes.rb +171 -0
  106. data/test/functional/test_string_id_compatibility.rb +11 -11
  107. data/test/functional/test_timestamps.rb +0 -2
  108. data/test/functional/test_userstamps.rb +0 -1
  109. data/test/functional/test_validations.rb +44 -31
  110. data/test/models.rb +18 -17
  111. data/test/{active_model_lint_test.rb → test_active_model_lint.rb} +3 -1
  112. data/test/test_helper.rb +59 -16
  113. data/test/unit/associations/test_base.rb +47 -42
  114. data/test/unit/associations/test_proxy.rb +15 -15
  115. data/test/unit/serializers/test_json_serializer.rb +29 -29
  116. data/test/unit/test_clone.rb +69 -0
  117. data/test/unit/test_descendant_appends.rb +3 -3
  118. data/test/unit/test_document.rb +49 -67
  119. data/test/unit/test_dynamic_finder.rb +53 -51
  120. data/test/unit/test_embedded_document.rb +19 -38
  121. data/test/unit/{test_support.rb → test_extensions.rb} +136 -122
  122. data/test/unit/test_key.rb +185 -0
  123. data/test/unit/test_keys.rb +29 -147
  124. data/test/unit/test_mongo_mapper.rb +3 -48
  125. data/test/unit/test_pagination.rb +1 -150
  126. data/test/unit/test_rails.rb +77 -19
  127. data/test/unit/test_rails_compatibility.rb +12 -12
  128. data/test/unit/test_serialization.rb +5 -5
  129. data/test/unit/test_time_zones.rb +9 -9
  130. data/test/unit/test_validations.rb +46 -46
  131. metadata +157 -155
  132. data/.gitignore +0 -10
  133. data/Rakefile +0 -55
  134. data/VERSION +0 -1
  135. data/lib/mongo_mapper/plugins/pagination/proxy.rb +0 -72
  136. data/lib/mongo_mapper/query.rb +0 -130
  137. data/lib/mongo_mapper/support.rb +0 -215
  138. data/mongo_mapper.gemspec +0 -196
  139. data/performance/read_write.rb +0 -52
  140. data/specs.watchr +0 -51
  141. data/test/support/custom_matchers.rb +0 -55
  142. data/test/support/timing.rb +0 -16
  143. data/test/unit/test_query.rb +0 -340
@@ -9,7 +9,7 @@ class SerializationTest < Test::Unit::TestCase
9
9
  key :preferences, Hash
10
10
  key :created_at, Time
11
11
  end
12
-
12
+
13
13
  @instance = @document.new(
14
14
  :name => 'John Doe',
15
15
  :age => 25,
@@ -18,7 +18,7 @@ class SerializationTest < Test::Unit::TestCase
18
18
  :created_at => Time.now.change(:usec => 0)
19
19
  )
20
20
  end
21
-
21
+
22
22
  [:json].each do |format|
23
23
  context format do
24
24
  should "be reversable" do
@@ -27,7 +27,7 @@ class SerializationTest < Test::Unit::TestCase
27
27
 
28
28
  assert_equal @instance, unserialized
29
29
  end
30
-
30
+
31
31
  should "allow attribute only filtering" do
32
32
  serialized = @instance.send("to_#{format}", :only => [ :age, :name ])
33
33
  unserialized = @document.send("from_#{format}", serialized)
@@ -37,11 +37,11 @@ class SerializationTest < Test::Unit::TestCase
37
37
  assert ! unserialized.awesome
38
38
  assert_nil unserialized.created_at
39
39
  end
40
-
40
+
41
41
  should "allow attribute except filtering" do
42
42
  serialized = @instance.send("to_#{format}", :except => [ :age, :name ])
43
43
  unserialized = @document.send("from_#{format}", serialized)
44
-
44
+
45
45
  assert_nil unserialized.name
46
46
  assert_nil unserialized.age
47
47
  assert_equal @instance.awesome, unserialized.awesome
@@ -2,37 +2,37 @@ require 'test_helper'
2
2
 
3
3
  class TimeZonesTest < Test::Unit::TestCase
4
4
  context "An instance of an embedded document" do
5
- setup do
5
+ setup do
6
6
  @document = EDoc do
7
7
  key :name, String
8
8
  key :created_at, Time
9
9
  end
10
10
  end
11
-
11
+
12
12
  should "work without Time.zone" do
13
13
  Time.zone = nil
14
-
14
+
15
15
  doc = @document.new(:created_at => "2009-08-15 14:00:00")
16
16
  doc.created_at.should == Time.local(2009, 8, 15, 14, 0, 0).utc
17
17
  end
18
-
18
+
19
19
  should "work with Time.zone set to the (default) UTC" do
20
20
  Time.zone = 'UTC'
21
-
21
+
22
22
  doc = @document.new(:created_at => "2009-08-15 14:00:00")
23
23
  doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
24
24
  doc.created_at.should == Time.utc(2009, 8, 15, 14)
25
-
25
+
26
26
  Time.zone = nil
27
27
  end
28
-
28
+
29
29
  should "work with timezones that are not UTC" do
30
30
  Time.zone = 'Hawaii'
31
-
31
+
32
32
  doc = @document.new(:created_at => "2009-08-15 14:00:00")
33
33
  doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
34
34
  doc.created_at.should == Time.utc(2009, 8, 16)
35
-
35
+
36
36
  Time.zone = nil
37
37
  end
38
38
  end
@@ -153,7 +153,7 @@ class ValidationsTest < Test::Unit::TestCase
153
153
  doc.should have_error_on(:name)
154
154
  end
155
155
  end
156
-
156
+
157
157
  context "validating exclusion of" do
158
158
  should "throw error if enumerator not provided" do
159
159
  @document.key :action, String
@@ -161,30 +161,30 @@ class ValidationsTest < Test::Unit::TestCase
161
161
  @document.validates_exclusion_of :action
162
162
  }.should raise_error(ArgumentError)
163
163
  end
164
-
164
+
165
165
  should "work with validates_exclusion_of macro" do
166
166
  @document.key :action, String
167
167
  @document.validates_exclusion_of :action, :within => %w(kick run)
168
-
168
+
169
169
  doc = @document.new
170
170
  doc.should_not have_error_on(:action)
171
-
171
+
172
172
  doc.action = 'fart'
173
173
  doc.should_not have_error_on(:action)
174
-
174
+
175
175
  doc.action = 'kick'
176
176
  doc.should have_error_on(:action, 'is reserved')
177
177
  end
178
178
 
179
179
  should "work with :not_in shortcut on key definition" do
180
180
  @document.key :action, String, :not_in => %w(kick run)
181
-
181
+
182
182
  doc = @document.new
183
183
  doc.should_not have_error_on(:action)
184
-
184
+
185
185
  doc.action = 'fart'
186
186
  doc.should_not have_error_on(:action)
187
-
187
+
188
188
  doc.action = 'kick'
189
189
  doc.should have_error_on(:action, 'is reserved')
190
190
  end
@@ -192,7 +192,7 @@ class ValidationsTest < Test::Unit::TestCase
192
192
  should "not have error if allow nil is true and value is nil" do
193
193
  @document.key :action, String
194
194
  @document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
195
-
195
+
196
196
  doc = @document.new
197
197
  doc.should_not have_error_on(:action)
198
198
  end
@@ -200,7 +200,7 @@ class ValidationsTest < Test::Unit::TestCase
200
200
  should "not have error if allow blank is true and value is blank" do
201
201
  @document.key :action, String
202
202
  @document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
203
-
203
+
204
204
  doc = @document.new(:action => '')
205
205
  doc.should_not have_error_on(:action)
206
206
  end
@@ -213,53 +213,53 @@ class ValidationsTest < Test::Unit::TestCase
213
213
  @document.validates_inclusion_of :action
214
214
  }.should raise_error(ArgumentError)
215
215
  end
216
-
216
+
217
217
  should "work with validates_inclusion_of macro" do
218
218
  @document.key :action, String
219
219
  @document.validates_inclusion_of :action, :within => %w(kick run)
220
-
220
+
221
221
  doc = @document.new
222
222
  doc.should have_error_on(:action, 'is not in the list')
223
-
223
+
224
224
  doc.action = 'fart'
225
225
  doc.should have_error_on(:action, 'is not in the list')
226
-
226
+
227
227
  doc.action = 'kick'
228
228
  doc.should_not have_error_on(:action)
229
229
  end
230
230
 
231
231
  should "work with :in shortcut on key definition" do
232
232
  @document.key :action, String, :in => %w(kick run)
233
-
233
+
234
234
  doc = @document.new
235
235
  doc.should have_error_on(:action, 'is not in the list')
236
-
236
+
237
237
  doc.action = 'fart'
238
238
  doc.should have_error_on(:action, 'is not in the list')
239
-
239
+
240
240
  doc.action = 'kick'
241
241
  doc.should_not have_error_on(:action)
242
242
  end
243
-
243
+
244
244
  should "not have error if allow nil is true and value is nil" do
245
245
  @document.key :action, String
246
246
  @document.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
247
-
247
+
248
248
  doc = @document.new
249
249
  doc.should_not have_error_on(:action)
250
250
  end
251
-
251
+
252
252
  should "not have error if allow blank is true and value is blank" do
253
253
  @document.key :action, String
254
254
  @document.validates_inclusion_of :action, :within => %w(kick run), :allow_blank => true
255
-
255
+
256
256
  doc = @document.new(:action => '')
257
257
  doc.should_not have_error_on(:action)
258
258
  end
259
259
  end
260
-
260
+
261
261
  end # End on a Document
262
-
262
+
263
263
  context "On an EmbeddedDocument" do
264
264
  setup do
265
265
  @embedded_doc = EDoc()
@@ -411,7 +411,7 @@ class ValidationsTest < Test::Unit::TestCase
411
411
  doc.should have_error_on(:name)
412
412
  end
413
413
  end
414
-
414
+
415
415
  context "validating exclusion of" do
416
416
  should "throw error if enumerator not provided" do
417
417
  @embedded_doc.key :action, String
@@ -419,38 +419,38 @@ class ValidationsTest < Test::Unit::TestCase
419
419
  @embedded_doc.validates_exclusion_of :action
420
420
  }.should raise_error(ArgumentError)
421
421
  end
422
-
422
+
423
423
  should "work with validates_exclusion_of macro" do
424
424
  @embedded_doc.key :action, String
425
425
  @embedded_doc.validates_exclusion_of :action, :within => %w(kick run)
426
-
426
+
427
427
  doc = @embedded_doc.new
428
428
  doc.should_not have_error_on(:action)
429
-
429
+
430
430
  doc.action = 'fart'
431
431
  doc.should_not have_error_on(:action)
432
-
432
+
433
433
  doc.action = 'kick'
434
434
  doc.should have_error_on(:action, 'is reserved')
435
435
  end
436
436
 
437
437
  should "work with :not_in shortcut on key definition" do
438
438
  @embedded_doc.key :action, String, :not_in => %w(kick run)
439
-
439
+
440
440
  doc = @embedded_doc.new
441
441
  doc.should_not have_error_on(:action)
442
-
442
+
443
443
  doc.action = 'fart'
444
444
  doc.should_not have_error_on(:action)
445
-
445
+
446
446
  doc.action = 'kick'
447
447
  doc.should have_error_on(:action, 'is reserved')
448
448
  end
449
-
449
+
450
450
  should "not have error if allow nil is true and value is nil" do
451
451
  @embedded_doc.key :action, String
452
452
  @embedded_doc.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
453
-
453
+
454
454
  doc = @embedded_doc.new
455
455
  doc.should_not have_error_on(:action)
456
456
  end
@@ -458,7 +458,7 @@ class ValidationsTest < Test::Unit::TestCase
458
458
  should "not have error if allow blank is true and value is blank" do
459
459
  @embedded_doc.key :action, String
460
460
  @embedded_doc.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
461
-
461
+
462
462
  doc = @embedded_doc.new(:action => '')
463
463
  doc.should_not have_error_on(:action)
464
464
  end
@@ -471,30 +471,30 @@ class ValidationsTest < Test::Unit::TestCase
471
471
  @embedded_doc.validates_inclusion_of :action
472
472
  }.should raise_error(ArgumentError)
473
473
  end
474
-
474
+
475
475
  should "work with validates_inclusion_of macro" do
476
476
  @embedded_doc.key :action, String
477
477
  @embedded_doc.validates_inclusion_of :action, :within => %w(kick run)
478
-
478
+
479
479
  doc = @embedded_doc.new
480
480
  doc.should have_error_on(:action, 'is not in the list')
481
-
481
+
482
482
  doc.action = 'fart'
483
483
  doc.should have_error_on(:action, 'is not in the list')
484
-
484
+
485
485
  doc.action = 'kick'
486
486
  doc.should_not have_error_on(:action)
487
487
  end
488
488
 
489
489
  should "work with :in shortcut on key definition" do
490
490
  @embedded_doc.key :action, String, :in => %w(kick run)
491
-
491
+
492
492
  doc = @embedded_doc.new
493
493
  doc.should have_error_on(:action, 'is not in the list')
494
-
494
+
495
495
  doc.action = 'fart'
496
496
  doc.should have_error_on(:action, 'is not in the list')
497
-
497
+
498
498
  doc.action = 'kick'
499
499
  doc.should_not have_error_on(:action)
500
500
  end
@@ -502,24 +502,24 @@ class ValidationsTest < Test::Unit::TestCase
502
502
  should "not have error if allow nil is true and value is nil" do
503
503
  @embedded_doc.key :action, String
504
504
  @embedded_doc.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
505
-
505
+
506
506
  doc = @embedded_doc.new
507
507
  doc.should_not have_error_on(:action)
508
508
  end
509
-
509
+
510
510
  should "not have error if allow blank is true and value is blank" do
511
511
  @embedded_doc.key :action, String
512
512
  @embedded_doc.validates_inclusion_of :action, :within => %w(kick run), :allow_blank => true
513
-
513
+
514
514
  doc = @embedded_doc.new(:action => '')
515
515
  doc.should_not have_error_on(:action)
516
516
  end
517
517
  end
518
518
 
519
519
  end # End on an EmbeddedDocument
520
-
520
+
521
521
  end # Validations
522
-
522
+
523
523
  context "Adding validation errors" do
524
524
  setup do
525
525
  @document = Doc do