mongomodel 0.5.5 → 0.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +23 -11
  3. data/gemfiles/rails-3.2.gemfile +2 -2
  4. data/gemfiles/rails-4-observers.gemfile +1 -1
  5. data/gemfiles/rails-4.1.gemfile +2 -2
  6. data/gemfiles/{mongoid.gemfile → rails-4.2.gemfile} +2 -3
  7. data/gemfiles/{mongo_mapper.gemfile → rails-5.0.gemfile} +2 -3
  8. data/gemfiles/rails-5.1.gemfile +10 -0
  9. data/gemfiles/rails-5.2.gemfile +10 -0
  10. data/lib/mongomodel.rb +15 -15
  11. data/lib/mongomodel/attributes/mongo.rb +7 -7
  12. data/lib/mongomodel/attributes/store.rb +4 -4
  13. data/lib/mongomodel/attributes/typecasting.rb +7 -7
  14. data/lib/mongomodel/compatibility/mongo_mapper.rb +3 -3
  15. data/lib/mongomodel/compatibility/mongoid.rb +3 -3
  16. data/lib/mongomodel/concerns/abstract_class.rb +3 -3
  17. data/lib/mongomodel/concerns/activemodel.rb +4 -4
  18. data/lib/mongomodel/concerns/associations.rb +8 -8
  19. data/lib/mongomodel/concerns/associations/base/association.rb +5 -5
  20. data/lib/mongomodel/concerns/associations/base/definition.rb +4 -4
  21. data/lib/mongomodel/concerns/associations/base/proxy.rb +1 -1
  22. data/lib/mongomodel/concerns/associations/belongs_to.rb +19 -19
  23. data/lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb +36 -36
  24. data/lib/mongomodel/concerns/associations/has_many_by_ids.rb +34 -34
  25. data/lib/mongomodel/concerns/attribute_methods.rb +10 -10
  26. data/lib/mongomodel/concerns/attribute_methods/before_type_cast.rb +4 -4
  27. data/lib/mongomodel/concerns/attribute_methods/dirty.rb +95 -13
  28. data/lib/mongomodel/concerns/attribute_methods/forbidden.rb +1 -1
  29. data/lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb +6 -6
  30. data/lib/mongomodel/concerns/attribute_methods/nested.rb +18 -18
  31. data/lib/mongomodel/concerns/attribute_methods/protected.rb +3 -3
  32. data/lib/mongomodel/concerns/attribute_methods/query.rb +3 -3
  33. data/lib/mongomodel/concerns/attribute_methods/read.rb +4 -4
  34. data/lib/mongomodel/concerns/attribute_methods/write.rb +4 -4
  35. data/lib/mongomodel/concerns/attributes.rb +18 -18
  36. data/lib/mongomodel/concerns/callbacks.rb +7 -7
  37. data/lib/mongomodel/concerns/document_parent.rb +2 -2
  38. data/lib/mongomodel/concerns/logging.rb +2 -2
  39. data/lib/mongomodel/concerns/map_reduce.rb +11 -11
  40. data/lib/mongomodel/concerns/pretty_inspect.rb +3 -3
  41. data/lib/mongomodel/concerns/properties.rb +18 -18
  42. data/lib/mongomodel/concerns/record_status.rb +9 -13
  43. data/lib/mongomodel/concerns/serialization.rb +4 -4
  44. data/lib/mongomodel/concerns/timestamps.rb +4 -4
  45. data/lib/mongomodel/concerns/translation.rb +2 -2
  46. data/lib/mongomodel/concerns/validations.rb +5 -5
  47. data/lib/mongomodel/concerns/validations/associated.rb +1 -1
  48. data/lib/mongomodel/document.rb +6 -6
  49. data/lib/mongomodel/document/callbacks.rb +15 -21
  50. data/lib/mongomodel/document/collection_modifiers.rb +5 -5
  51. data/lib/mongomodel/document/dynamic_finders.rb +1 -1
  52. data/lib/mongomodel/document/indexes.rb +19 -19
  53. data/lib/mongomodel/document/optimistic_locking.rb +7 -7
  54. data/lib/mongomodel/document/persistence.rb +23 -23
  55. data/lib/mongomodel/document/scopes.rb +20 -20
  56. data/lib/mongomodel/document/validations.rb +6 -6
  57. data/lib/mongomodel/document/validations/uniqueness.rb +11 -11
  58. data/lib/mongomodel/embedded_document.rb +11 -11
  59. data/lib/mongomodel/locale/en.yml +0 -1
  60. data/lib/mongomodel/log_subscriber.rb +5 -5
  61. data/lib/mongomodel/railtie.rb +13 -13
  62. data/lib/mongomodel/support/collection.rb +31 -31
  63. data/lib/mongomodel/support/configuration.rb +11 -11
  64. data/lib/mongomodel/support/core_extensions.rb +1 -1
  65. data/lib/mongomodel/support/dynamic_finder.rb +12 -12
  66. data/lib/mongomodel/support/exceptions.rb +6 -6
  67. data/lib/mongomodel/support/instrumented_collection.rb +20 -20
  68. data/lib/mongomodel/support/map.rb +33 -33
  69. data/lib/mongomodel/support/mongo_operator.rb +6 -6
  70. data/lib/mongomodel/support/mongo_options.rb +18 -18
  71. data/lib/mongomodel/support/mongo_order.rb +16 -16
  72. data/lib/mongomodel/support/paginator.rb +8 -8
  73. data/lib/mongomodel/support/reference.rb +10 -10
  74. data/lib/mongomodel/support/scope.rb +37 -37
  75. data/lib/mongomodel/support/scope/array_methods.rb +1 -1
  76. data/lib/mongomodel/support/scope/batches.rb +1 -1
  77. data/lib/mongomodel/support/scope/dynamic_finders.rb +1 -1
  78. data/lib/mongomodel/support/scope/finder_methods.rb +7 -7
  79. data/lib/mongomodel/support/scope/load_methods.rb +1 -1
  80. data/lib/mongomodel/support/scope/pagination.rb +1 -1
  81. data/lib/mongomodel/support/scope/query_methods.rb +6 -6
  82. data/lib/mongomodel/support/scope/spawn_methods.rb +8 -8
  83. data/lib/mongomodel/support/types.rb +2 -2
  84. data/lib/mongomodel/support/types/array.rb +1 -1
  85. data/lib/mongomodel/support/types/boolean.rb +3 -3
  86. data/lib/mongomodel/support/types/custom.rb +3 -3
  87. data/lib/mongomodel/support/types/date.rb +2 -2
  88. data/lib/mongomodel/support/types/date_time.rb +6 -16
  89. data/lib/mongomodel/support/types/float.rb +1 -1
  90. data/lib/mongomodel/support/types/hash.rb +1 -1
  91. data/lib/mongomodel/support/types/integer.rb +13 -10
  92. data/lib/mongomodel/support/types/object.rb +5 -5
  93. data/lib/mongomodel/support/types/rational.rb +3 -3
  94. data/lib/mongomodel/support/types/time.rb +2 -2
  95. data/lib/mongomodel/version.rb +1 -1
  96. data/lib/rails/generators/mongo_model/config/templates/mongomodel.yml +3 -4
  97. data/lib/rails/generators/mongo_model/model/model_generator.rb +3 -3
  98. data/mongomodel.gemspec +5 -4
  99. data/spec/mongomodel/attributes/store_spec.rb +21 -21
  100. data/spec/mongomodel/concerns/activemodel_spec.rb +4 -4
  101. data/spec/mongomodel/concerns/associations/base/association_spec.rb +12 -12
  102. data/spec/mongomodel/concerns/associations/belongs_to_spec.rb +34 -21
  103. data/spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb +53 -53
  104. data/spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb +30 -30
  105. data/spec/mongomodel/concerns/attribute_methods/before_type_cast_spec.rb +7 -7
  106. data/spec/mongomodel/concerns/attribute_methods/dirty_spec.rb +45 -41
  107. data/spec/mongomodel/concerns/attribute_methods/multi_parameter_assignment_spec.rb +7 -7
  108. data/spec/mongomodel/concerns/attribute_methods/nested_spec.rb +31 -31
  109. data/spec/mongomodel/concerns/attribute_methods/protected_spec.rb +15 -15
  110. data/spec/mongomodel/concerns/attribute_methods/query_spec.rb +19 -19
  111. data/spec/mongomodel/concerns/attribute_methods/read_spec.rb +9 -9
  112. data/spec/mongomodel/concerns/attribute_methods/write_spec.rb +6 -6
  113. data/spec/mongomodel/concerns/attribute_methods_spec.rb +13 -13
  114. data/spec/mongomodel/concerns/attributes_spec.rb +34 -34
  115. data/spec/mongomodel/concerns/callbacks_spec.rb +25 -21
  116. data/spec/mongomodel/concerns/logging_spec.rb +5 -3
  117. data/spec/mongomodel/concerns/map_reduce_spec.rb +19 -19
  118. data/spec/mongomodel/concerns/observing_spec.rb +3 -3
  119. data/spec/mongomodel/concerns/pretty_inspect_spec.rb +10 -10
  120. data/spec/mongomodel/concerns/properties_spec.rb +11 -11
  121. data/spec/mongomodel/concerns/serialization/json_serialization_spec.rb +13 -13
  122. data/spec/mongomodel/concerns/timestamps_spec.rb +39 -39
  123. data/spec/mongomodel/concerns/validations_spec.rb +41 -38
  124. data/spec/mongomodel/document/callbacks_spec.rb +20 -16
  125. data/spec/mongomodel/document/collection_modifiers_spec.rb +16 -16
  126. data/spec/mongomodel/document/dynamic_finders_spec.rb +46 -46
  127. data/spec/mongomodel/document/finders_spec.rb +15 -15
  128. data/spec/mongomodel/document/indexes_spec.rb +29 -29
  129. data/spec/mongomodel/document/optimistic_locking_spec.rb +16 -16
  130. data/spec/mongomodel/document/persistence_spec.rb +39 -39
  131. data/spec/mongomodel/document/scopes_spec.rb +17 -17
  132. data/spec/mongomodel/document/validations/uniqueness_spec.rb +46 -46
  133. data/spec/mongomodel/document/validations_spec.rb +35 -35
  134. data/spec/mongomodel/document_spec.rb +19 -19
  135. data/spec/mongomodel/embedded_document_spec.rb +19 -19
  136. data/spec/mongomodel/mongomodel_spec.rb +7 -6
  137. data/spec/mongomodel/support/collection_spec.rb +54 -54
  138. data/spec/mongomodel/support/configuration_spec.rb +1 -1
  139. data/spec/mongomodel/support/map_spec.rb +66 -66
  140. data/spec/mongomodel/support/mongo_operator_spec.rb +5 -5
  141. data/spec/mongomodel/support/mongo_options_spec.rb +42 -42
  142. data/spec/mongomodel/support/mongo_order_spec.rb +24 -24
  143. data/spec/mongomodel/support/paginator_spec.rb +15 -15
  144. data/spec/mongomodel/support/property_spec.rb +29 -23
  145. data/spec/mongomodel/support/scope_spec.rb +205 -204
  146. data/spec/spec_helper.rb +13 -2
  147. data/spec/support/callbacks.rb +3 -8
  148. data/spec/support/helpers/define_class.rb +7 -7
  149. data/spec/support/helpers/document_finder_stubs.rb +6 -6
  150. data/spec/support/helpers/specs_for.rb +1 -1
  151. data/spec/support/helpers/validations.rb +1 -1
  152. data/spec/support/matchers/find_with.rb +8 -8
  153. data/spec/support/matchers/respond_to_boolean.rb +3 -3
  154. data/spec/support/matchers/run_callbacks.rb +6 -9
  155. data/spec/support/models.rb +5 -5
  156. metadata +23 -7
@@ -5,7 +5,7 @@ module MongoModel
5
5
  it "uses a standard connection (Mongo::MongoClient) if no replicas are specified" do
6
6
  Configuration.new({}).connection.should be_an_instance_of(Mongo::MongoClient)
7
7
  end
8
-
8
+
9
9
  it "uses a replica set connection (Mongo::MongoReplicaSetClient) if replicas are specified" do
10
10
  Configuration.new({ :replicas => ['127.0.0.1:27017'], :connect => false }).connection.should be_an_instance_of(Mongo::MongoReplicaSetClient)
11
11
  end
@@ -3,31 +3,31 @@ require 'spec_helper'
3
3
  module MongoModel
4
4
  describe Map do
5
5
  subject { Map }
6
-
6
+
7
7
  define_class(:TestDocument, EmbeddedDocument) do
8
8
  property :name, String
9
-
9
+
10
10
  def self.cast(name)
11
11
  new(:name => name)
12
12
  end
13
13
  end
14
-
14
+
15
15
  let(:doc) { TestDocument.new(:name => "Foobar") }
16
-
16
+
17
17
  it { should be_a_subclass_of(Hash) }
18
-
18
+
19
19
  it "has from type String" do
20
20
  subject.from.should == String
21
21
  end
22
-
22
+
23
23
  it "has to type Object" do
24
24
  subject.to.should == Object
25
25
  end
26
-
26
+
27
27
  it "does not show its type when inspecting" do
28
28
  subject.inspect.should == "Map"
29
29
  end
30
-
30
+
31
31
  it "allows any string->object mappings to be added" do
32
32
  map = subject.new
33
33
  map["hello"] = 123
@@ -35,72 +35,72 @@ module MongoModel
35
35
  map["mydoc"] = doc
36
36
  map.should == { "hello" => 123, "foo" => "Bonjour", "mydoc" => doc }
37
37
  end
38
-
38
+
39
39
  it "converts to mongo representation" do
40
40
  map = subject.new({ "hello" => 123, "mydoc" => doc })
41
41
  map.to_mongo.should == { "hello" => 123, "mydoc" => { "_type" => 'TestDocument', "name" => "Foobar" } }
42
42
  end
43
-
43
+
44
44
  it "loads from mongo representation" do
45
45
  map = subject.from_mongo({ "hello" => 123, "mydoc" => { "_type" => 'TestDocument', "name" => "Foobar" } })
46
46
  map.should be_a(subject)
47
47
  map.should == { "hello" => 123, "mydoc" => doc }
48
48
  end
49
-
49
+
50
50
  it "caches map types" do
51
51
  Map[Symbol => String].should equal(Map[Symbol => String])
52
52
  Map[String => TestDocument].should equal(Map[String => TestDocument])
53
53
  end
54
-
54
+
55
55
  describe "map from String to String" do
56
56
  let(:klass) { Map[String => String] }
57
57
  subject { klass.new("123" => "456", "12.5" => "foobar") }
58
-
58
+
59
59
  it "shows its types when inspecting" do
60
60
  klass.inspect.should == "Map[String => String]"
61
61
  end
62
-
62
+
63
63
  it "casts key/values when instantiating" do
64
64
  map = klass.new(123 => 456, 12.5 => :foobar)
65
65
  map.should == { "123" => "456", "12.5" => "foobar" }
66
66
  end
67
-
67
+
68
68
  it "casts keys on []" do
69
69
  subject[123].should == "456"
70
70
  end
71
-
71
+
72
72
  it "casts key/values on []=" do
73
73
  subject[12.5] = 456
74
74
  subject["12.5"].should == "456"
75
75
  end
76
-
76
+
77
77
  it "casts key/values on #store" do
78
78
  subject.store(12.5, 456)
79
79
  subject["12.5"].should == "456"
80
80
  end
81
-
81
+
82
82
  it "casts keys on #delete" do
83
83
  subject.delete(123)
84
84
  subject["123"].should be_nil
85
85
  end
86
-
86
+
87
87
  it "casts keys on #fetch" do
88
88
  subject.fetch(123).should == "456"
89
89
  subject.fetch(999, "default").should == "default"
90
90
  end
91
-
91
+
92
92
  it "casts keys on #has_key?, #include?, #key?, #member?" do
93
- subject.has_key?(123).should be_true
94
- subject.include?(12.5).should be_true
95
- subject.key?(123).should be_true
96
- subject.member?(12.5).should be_true
93
+ subject.has_key?(123).should be true
94
+ subject.include?(12.5).should be true
95
+ subject.key?(123).should be true
96
+ subject.member?(12.5).should be true
97
97
  end
98
-
98
+
99
99
  it "casts values on #has_value?, #value?" do
100
- subject.has_value?(456).should be_true
101
- subject.value?(456).should be_true
100
+ subject.has_value?(456).should be true
101
+ subject.value?(456).should be true
102
102
  end
103
-
103
+
104
104
  if Hash.method_defined?(:key)
105
105
  it "casts values on #key" do
106
106
  subject.key(456).should == "123"
@@ -110,79 +110,79 @@ module MongoModel
110
110
  subject.index(456).should == "123"
111
111
  end
112
112
  end
113
-
113
+
114
114
  it "casts key/values on #replace" do
115
115
  subject.replace(321 => 654, 5.12 => :barbaz)
116
116
  subject.should == { "321" => "654", "5.12" => "barbaz" }
117
117
  end
118
-
118
+
119
119
  it "casts key/values on #merge" do
120
120
  map = subject.merge(321 => 654, 5.12 => :barbaz)
121
121
  map.should == { "123" => "456", "12.5" => "foobar", "321" => "654", "5.12" => "barbaz" }
122
122
  end
123
-
123
+
124
124
  it "casts key/values on #merge!" do
125
125
  subject.merge!(321 => 654, 5.12 => :barbaz)
126
126
  subject.should == { "123" => "456", "12.5" => "foobar", "321" => "654", "5.12" => "barbaz" }
127
127
  end
128
-
128
+
129
129
  it "casts keys on #values_at" do
130
130
  subject.values_at(12.5, 123).should == ["foobar", "456"]
131
131
  end
132
132
  end
133
-
133
+
134
134
  describe "map from Symbol to TestDocument" do
135
135
  let(:doc1) { TestDocument.new(:name => "First") }
136
136
  let(:doc2) { TestDocument.new(:name => "Another") }
137
-
137
+
138
138
  let(:klass) { Map[Symbol => TestDocument] }
139
139
  subject { klass.new(:abc => doc1, :another => doc2) }
140
-
140
+
141
141
  it "shows its types when inspecting" do
142
142
  klass.inspect.should == "Map[Symbol => TestDocument]"
143
143
  end
144
-
144
+
145
145
  it "casts key/values when instantiating" do
146
146
  map = klass.new("foo" => "First", "123" => "Another")
147
147
  map.should == { :foo => doc1, :"123" => doc2 }
148
148
  end
149
-
149
+
150
150
  it "casts keys on []" do
151
151
  subject["abc"].should == doc1
152
152
  end
153
-
153
+
154
154
  it "casts key/values on []=" do
155
155
  subject["def"] = "Another"
156
156
  subject[:def].should == doc2
157
157
  end
158
-
158
+
159
159
  it "casts key/values on #store" do
160
160
  subject.store("def", "Another")
161
161
  subject[:def].should == doc2
162
162
  end
163
-
163
+
164
164
  it "casts keys on #delete" do
165
165
  subject.delete("abc")
166
166
  subject[:abc].should be_nil
167
167
  end
168
-
168
+
169
169
  it "casts keys on #fetch" do
170
170
  subject.fetch("abc").should == doc1
171
171
  subject.fetch("999", "default").should == "default"
172
172
  end
173
-
173
+
174
174
  it "casts keys on #has_key?, #include?, #key?, #member?" do
175
- subject.has_key?("abc").should be_true
176
- subject.include?("another").should be_true
177
- subject.key?("abc").should be_true
178
- subject.member?("another").should be_true
175
+ subject.has_key?("abc").should be true
176
+ subject.include?("another").should be true
177
+ subject.key?("abc").should be true
178
+ subject.member?("another").should be true
179
179
  end
180
-
180
+
181
181
  it "casts values on #has_value?, #value?" do
182
- subject.has_value?("First").should be_true
183
- subject.value?("Another").should be_true
182
+ subject.has_value?("First").should be true
183
+ subject.value?("Another").should be true
184
184
  end
185
-
185
+
186
186
  if Hash.method_defined?(:key)
187
187
  it "casts values on #key" do
188
188
  subject.key("First").should == :abc
@@ -192,81 +192,81 @@ module MongoModel
192
192
  subject.index("First").should == :abc
193
193
  end
194
194
  end
195
-
195
+
196
196
  it "casts key/values on #replace" do
197
197
  subject.replace("321" => "Bonus", "hello" => "Another")
198
198
  subject.should == { :"321" => TestDocument.new(:name => "Bonus"), :hello => doc2 }
199
199
  end
200
-
200
+
201
201
  it "casts key/values on #merge" do
202
202
  map = subject.merge("321" => "Bonus", "hello" => "Another")
203
203
  map.should == { :abc => doc1, :another => doc2, :"321" => TestDocument.new(:name => "Bonus"), :hello => doc2 }
204
204
  end
205
-
205
+
206
206
  it "casts key/values on #merge!" do
207
207
  subject.merge!("321" => "Bonus", "hello" => "Another")
208
208
  subject.should == { :abc => doc1, :another => doc2, :"321" => TestDocument.new(:name => "Bonus"), :hello => doc2 }
209
209
  end
210
-
210
+
211
211
  it "casts keys on #values_at" do
212
212
  subject.values_at(:another, :abc).should == [doc2, doc1]
213
213
  end
214
214
  end
215
-
215
+
216
216
  describe "map from Date to String" do
217
217
  let(:klass) { Map[Date => String] }
218
218
  subject { klass.new(Date.civil(2009, 11, 15) => "Hello world") }
219
-
219
+
220
220
  it "casts key to String on #to_mongo" do
221
221
  subject.to_mongo.should == { "2009/11/15" => "Hello world" }
222
222
  end
223
223
  end
224
224
  end
225
-
225
+
226
226
  specs_for(Document, EmbeddedDocument) do
227
227
  define_class(:ChildDocument, EmbeddedDocument) do
228
228
  property :name, String
229
-
229
+
230
230
  def self.cast(name)
231
231
  new(:name => name)
232
232
  end
233
233
  end
234
-
234
+
235
235
  describe "defining a Map property containing EmbeddedDocument values" do
236
236
  define_class(:TestDocument, described_class) do
237
237
  property :test_map, Map[String => ChildDocument]
238
238
  end
239
-
239
+
240
240
  let(:child1) { ChildDocument.new(:name => "Child 1") }
241
241
  let(:child2) { ChildDocument.new(:name => "Child 2") }
242
-
242
+
243
243
  subject { TestDocument.new(:test_map => { "1" => child1, "2" => child2 }) }
244
-
244
+
245
245
  it "includes the map values in the embedded documents list" do
246
246
  subject.embedded_documents.should include(child1, child2)
247
247
  end
248
248
  end
249
-
249
+
250
250
  describe "defining a Map property with no default value" do
251
251
  define_class(:TestDocument, described_class) do
252
252
  property :test_map, Map[Symbol => ChildDocument]
253
253
  end
254
254
 
255
255
  subject { TestDocument.new }
256
-
256
+
257
257
  it "defaults to an empty map" do
258
258
  subject.test_map.should be_an_instance_of(Map[Symbol => ChildDocument])
259
259
  subject.test_map.should be_empty
260
260
  end
261
261
  end
262
-
262
+
263
263
  describe "defining a Map property with a default value" do
264
264
  define_class(:TestDocument, described_class) do
265
265
  property :test_map, Map[Symbol => ChildDocument], :default => { :abc => 'abc', 'def' => 'def' }
266
266
  end
267
267
 
268
268
  subject { TestDocument.new }
269
-
269
+
270
270
  it "casts key/values to map type" do
271
271
  subject.test_map[:abc].should == ChildDocument.new(:name => 'abc')
272
272
  subject.test_map[:def].should == ChildDocument.new(:name => 'def')
@@ -3,20 +3,20 @@ require 'spec_helper'
3
3
  module MongoModel
4
4
  describe MongoOperator do
5
5
  subject { MongoOperator.new(:age, :gt) }
6
-
6
+
7
7
  it "converts to mongo selector" do
8
8
  subject.to_mongo_selector(14).should == { '$gt' => 14 }
9
9
  end
10
-
10
+
11
11
  it "is equal to a MongoOperator with the same field and operator" do
12
12
  subject.should == MongoOperator.new(:age, :gt)
13
13
  end
14
-
14
+
15
15
  it "is not equal to a MongoOperator with a different field/operator" do
16
16
  subject.should_not == MongoOperator.new(:age, :lte)
17
17
  subject.should_not == MongoOperator.new(:date, :gt)
18
18
  end
19
-
19
+
20
20
  unless defined?(Origin) || defined?(SymbolOperator)
21
21
  it "is created from symbol methods" do
22
22
  :age.gt.should == MongoOperator.new(:age, :gt)
@@ -24,7 +24,7 @@ module MongoModel
24
24
  :position.near.should == MongoOperator.new(:position, :near)
25
25
  end
26
26
  end
27
-
27
+
28
28
  it "is equal within a hash" do
29
29
  { :age.gt => 10 }.should == { :age.gt => 10 }
30
30
  end
@@ -3,150 +3,150 @@ require 'spec_helper'
3
3
  module MongoModel
4
4
  describe MongoOptions do
5
5
  define_class(:TestDocument, Document)
6
-
6
+
7
7
  shared_examples_for "options without conditions" do
8
8
  it "has an empty selector hash" do
9
9
  subject.selector.should == {}
10
10
  end
11
11
  end
12
-
12
+
13
13
  shared_examples_for "options with conditions only" do
14
14
  it "has an empty options hash" do
15
15
  subject.options.should == {}
16
16
  end
17
17
  end
18
-
18
+
19
19
  context "with blank options" do
20
20
  subject { MongoOptions.new(TestDocument) }
21
-
21
+
22
22
  it_should_behave_like "options without conditions"
23
-
23
+
24
24
  it "has an empty options hash" do
25
25
  subject.options.should == {}
26
26
  end
27
27
  end
28
-
28
+
29
29
  context "with basic conditions" do
30
30
  subject { MongoOptions.new(TestDocument, :conditions => { :foo => 'bar' }) }
31
-
31
+
32
32
  it_should_behave_like "options with conditions only"
33
-
33
+
34
34
  it "includes the conditions in the selector" do
35
35
  subject.selector.should == { :foo => 'bar' }
36
36
  end
37
37
  end
38
-
38
+
39
39
  context "with conditions using an operator" do
40
40
  subject { MongoOptions.new(TestDocument, :conditions => { :age.gt => 10 }) }
41
-
41
+
42
42
  it_should_behave_like "options with conditions only"
43
-
43
+
44
44
  it "includes the expanded conditions in the selector" do
45
45
  subject.selector.should == { :age => { '$gt' => 10 } }
46
46
  end
47
47
  end
48
-
48
+
49
49
  context "with multiple operator conditions" do
50
50
  subject { MongoOptions.new(TestDocument, :conditions => { :age.gt => 10, :age.lte => 18 }) }
51
-
51
+
52
52
  it_should_behave_like "options with conditions only"
53
-
53
+
54
54
  it "merges conditions in the selector" do
55
55
  subject.selector.should == { :age => { '$gt' => 10, '$lte' => 18 } }
56
56
  end
57
57
  end
58
-
58
+
59
59
  context "with conditions using a property" do
60
60
  subject { MongoOptions.new(TestDocument, :conditions => { :id => '123' }) }
61
-
61
+
62
62
  it_should_behave_like "options with conditions only"
63
-
63
+
64
64
  it "uses the property as value in the selector" do
65
65
  subject.selector.should == { '_id' => '123' }
66
66
  end
67
67
  end
68
-
68
+
69
69
  context "with basic options (no conditions or order)" do
70
70
  subject { MongoOptions.new(TestDocument, :offset => 20, :limit => 10, :select => [ :foo, :bar ]) }
71
-
71
+
72
72
  it_should_behave_like "options without conditions"
73
-
73
+
74
74
  it "includes converted options in options hash" do
75
75
  subject.options.should == { :skip => 20, :limit => 10, :fields => [ :foo, :bar ]}
76
76
  end
77
77
  end
78
-
78
+
79
79
  context "with string order" do
80
80
  subject { MongoOptions.new(TestDocument, :order => 'foo DESC') }
81
-
81
+
82
82
  it_should_behave_like "options without conditions"
83
-
83
+
84
84
  it "converts order to sort in options hash" do
85
85
  subject.options.should == { :sort => [ ['foo', :descending] ] }
86
86
  end
87
87
  end
88
-
88
+
89
89
  context "with symbol order" do
90
90
  subject { MongoOptions.new(TestDocument, :order => :bar) }
91
-
91
+
92
92
  it_should_behave_like "options without conditions"
93
-
93
+
94
94
  it "converts order to sort in options hash" do
95
95
  subject.options.should == { :sort => [ ['bar', :ascending] ]}
96
96
  end
97
97
  end
98
-
98
+
99
99
  context "with symbol(asc) order" do
100
100
  subject { MongoOptions.new(TestDocument, :order => :bar.asc) }
101
-
101
+
102
102
  it_should_behave_like "options without conditions"
103
-
103
+
104
104
  it "converts order to sort in options hash" do
105
105
  subject.options.should == { :sort => [ ['bar', :ascending] ]}
106
106
  end
107
107
  end
108
-
108
+
109
109
  context "with multiple orders in array" do
110
110
  subject { MongoOptions.new(TestDocument, :order => ['foo ASC', :bar.desc]) }
111
-
111
+
112
112
  it_should_behave_like "options without conditions"
113
-
113
+
114
114
  it "converts order to sort in options hash" do
115
115
  subject.options.should == { :sort => [ ['foo', :ascending], ['bar', :descending]] }
116
116
  end
117
117
  end
118
-
118
+
119
119
  context "with multiple orders in string" do
120
120
  subject { MongoOptions.new(TestDocument, :order => 'foo DESC, baz') }
121
-
121
+
122
122
  it_should_behave_like "options without conditions"
123
-
123
+
124
124
  it "converts order to sort in options hash" do
125
125
  subject.options.should == { :sort => [ ['foo', :descending], ['baz', :ascending] ] }
126
126
  end
127
127
  end
128
-
128
+
129
129
  context "with an order using a property" do
130
130
  subject { MongoOptions.new(TestDocument, :order => :id.desc) }
131
-
131
+
132
132
  it_should_behave_like "options without conditions"
133
-
133
+
134
134
  it "uses property as value as sort column" do
135
135
  subject.options.should == { :sort => [ ['_id', :descending] ] }
136
136
  end
137
137
  end
138
-
138
+
139
139
  context "with conditions and options" do
140
140
  subject { MongoOptions.new(TestDocument, :conditions => { :age => 18 }, :order => :id.desc, :limit => 5) }
141
-
141
+
142
142
  it "uses conditions for selector" do
143
143
  subject.selector.should == { :age => 18 }
144
144
  end
145
-
145
+
146
146
  it "converts options" do
147
147
  subject.options.should == { :sort => [ ['_id', :descending] ], :limit => 5 }
148
148
  end
149
-
149
+
150
150
  it "converts to array" do
151
151
  subject.to_a.should == [ subject.selector, subject.options ]
152
152
  end