mongomodel 0.1.6 → 0.2.0

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 (39) hide show
  1. data/README.md +6 -0
  2. data/bin/console +4 -4
  3. data/lib/mongomodel.rb +4 -4
  4. data/lib/mongomodel/concerns/associations/base/definition.rb +4 -0
  5. data/lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb +7 -16
  6. data/lib/mongomodel/concerns/associations/has_many_by_ids.rb +6 -12
  7. data/lib/mongomodel/concerns/attributes.rb +1 -7
  8. data/lib/mongomodel/document.rb +0 -1
  9. data/lib/mongomodel/document/dynamic_finders.rb +2 -69
  10. data/lib/mongomodel/document/indexes.rb +0 -6
  11. data/lib/mongomodel/document/persistence.rb +1 -21
  12. data/lib/mongomodel/document/scopes.rb +59 -135
  13. data/lib/mongomodel/document/validations/uniqueness.rb +7 -5
  14. data/lib/mongomodel/support/dynamic_finder.rb +68 -0
  15. data/lib/mongomodel/support/mongo_operator.rb +29 -0
  16. data/lib/mongomodel/support/mongo_options.rb +0 -101
  17. data/lib/mongomodel/support/mongo_order.rb +78 -0
  18. data/lib/mongomodel/support/scope.rb +186 -0
  19. data/lib/mongomodel/support/scope/dynamic_finders.rb +21 -0
  20. data/lib/mongomodel/support/scope/finder_methods.rb +61 -0
  21. data/lib/mongomodel/support/scope/query_methods.rb +43 -0
  22. data/lib/mongomodel/support/scope/spawn_methods.rb +35 -0
  23. data/lib/mongomodel/version.rb +1 -1
  24. data/mongomodel.gemspec +20 -3
  25. data/spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb +1 -1
  26. data/spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb +1 -1
  27. data/spec/mongomodel/document/dynamic_finders_spec.rb +0 -1
  28. data/spec/mongomodel/document/finders_spec.rb +0 -144
  29. data/spec/mongomodel/document/indexes_spec.rb +2 -2
  30. data/spec/mongomodel/document/persistence_spec.rb +1 -15
  31. data/spec/mongomodel/document/scopes_spec.rb +64 -167
  32. data/spec/mongomodel/support/mongo_operator_spec.rb +29 -0
  33. data/spec/mongomodel/support/mongo_options_spec.rb +0 -150
  34. data/spec/mongomodel/support/mongo_order_spec.rb +127 -0
  35. data/spec/mongomodel/support/scope_spec.rb +932 -0
  36. data/spec/support/helpers/document_finder_stubs.rb +40 -0
  37. data/spec/support/matchers/find_with.rb +36 -0
  38. metadata +22 -5
  39. data/lib/mongomodel/document/finders.rb +0 -82
@@ -0,0 +1,40 @@
1
+ require 'spec/mocks'
2
+
3
+ module DocumentFinderStubs
4
+ include Spec::Mocks::ExampleMethods
5
+
6
+ def stub_find(result)
7
+ find_result = mock('find result', :to_a => result.map { |doc| doc.to_mongo }, :count => result.size)
8
+ collection.stub!(:find).and_return(find_result)
9
+ end
10
+
11
+ def should_find(expected={}, result=[])
12
+ selector, options = MongoModel::MongoOptions.new(self, expected).to_a
13
+ find_result = mock('find result', :to_a => result.map { |doc| doc.to_mongo })
14
+ collection.should_receive(:find).once.with(selector, options).and_return(find_result)
15
+ yield if block_given?
16
+ end
17
+
18
+ def should_not_find
19
+ collection.should_not_receive(:find)
20
+ yield if block_given?
21
+ end
22
+
23
+ def should_count(expected={}, result=[])
24
+ selector, options = MongoModel::MongoOptions.new(self, expected).to_a
25
+ find_result = mock('find result', :count => result)
26
+ collection.should_receive(:find).once.with(selector, options).and_return(find_result)
27
+ yield if block_given?
28
+ end
29
+
30
+ def should_not_count
31
+ collection.should_not_receive(:find)
32
+ yield if block_given?
33
+ end
34
+
35
+ def should_delete(conditions={})
36
+ selector, options = MongoModel::MongoOptions.new(self, :conditions => conditions).to_a
37
+ collection.should_receive(:remove).once.with(selector)
38
+ yield if block_given?
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ Spec::Matchers.define(:find_with) do |find_options|
2
+ extend Spec::Mocks::ExampleMethods
3
+
4
+ match do |klass|
5
+ selector, options = MongoModel::MongoOptions.new(klass, find_options).to_a
6
+
7
+ result = mock('find result', :to_a => (@result || []).map { |d| d.to_mongo })
8
+ klass.collection.should_receive(:find).once.with(selector, options).and_return(result)
9
+
10
+ true
11
+ end
12
+
13
+ def and_return(result)
14
+ @result = result
15
+ self
16
+ end
17
+ end
18
+
19
+ Spec::Matchers.define(:count_with) do |find_options|
20
+ extend Spec::Mocks::ExampleMethods
21
+
22
+ match do |klass|
23
+ selector, options = MongoModel::MongoOptions.new(klass, find_options).to_a
24
+ result = mock('find result')
25
+
26
+ klass.collection.should_receive(:find).once.with(selector, options).and_return(result)
27
+ result.should_receive(:count).once.and_return(@count || 5)
28
+
29
+ true
30
+ end
31
+
32
+ def and_return(count)
33
+ @count = count
34
+ self
35
+ end
36
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 6
9
- version: 0.1.6
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Sam Pohlenz
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-16 00:00:00 +09:30
17
+ date: 2010-04-21 00:00:00 +09:30
18
18
  default_executable: console
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -125,7 +125,6 @@ files:
125
125
  - lib/mongomodel/document.rb
126
126
  - lib/mongomodel/document/callbacks.rb
127
127
  - lib/mongomodel/document/dynamic_finders.rb
128
- - lib/mongomodel/document/finders.rb
129
128
  - lib/mongomodel/document/indexes.rb
130
129
  - lib/mongomodel/document/optimistic_locking.rb
131
130
  - lib/mongomodel/document/persistence.rb
@@ -137,8 +136,16 @@ files:
137
136
  - lib/mongomodel/support/collection.rb
138
137
  - lib/mongomodel/support/configuration.rb
139
138
  - lib/mongomodel/support/core_extensions.rb
139
+ - lib/mongomodel/support/dynamic_finder.rb
140
140
  - lib/mongomodel/support/exceptions.rb
141
+ - lib/mongomodel/support/mongo_operator.rb
141
142
  - lib/mongomodel/support/mongo_options.rb
143
+ - lib/mongomodel/support/mongo_order.rb
144
+ - lib/mongomodel/support/scope.rb
145
+ - lib/mongomodel/support/scope/dynamic_finders.rb
146
+ - lib/mongomodel/support/scope/finder_methods.rb
147
+ - lib/mongomodel/support/scope/query_methods.rb
148
+ - lib/mongomodel/support/scope/spawn_methods.rb
142
149
  - lib/mongomodel/support/types.rb
143
150
  - lib/mongomodel/support/types/array.rb
144
151
  - lib/mongomodel/support/types/boolean.rb
@@ -186,16 +193,21 @@ files:
186
193
  - spec/mongomodel/embedded_document_spec.rb
187
194
  - spec/mongomodel/mongomodel_spec.rb
188
195
  - spec/mongomodel/support/collection_spec.rb
196
+ - spec/mongomodel/support/mongo_operator_spec.rb
189
197
  - spec/mongomodel/support/mongo_options_spec.rb
198
+ - spec/mongomodel/support/mongo_order_spec.rb
190
199
  - spec/mongomodel/support/property_spec.rb
200
+ - spec/mongomodel/support/scope_spec.rb
191
201
  - spec/spec.opts
192
202
  - spec/spec_helper.rb
193
203
  - spec/specdoc.opts
194
204
  - spec/support/callbacks.rb
195
205
  - spec/support/helpers/define_class.rb
206
+ - spec/support/helpers/document_finder_stubs.rb
196
207
  - spec/support/helpers/specs_for.rb
197
208
  - spec/support/matchers/be_a_subclass_of.rb
198
209
  - spec/support/matchers/be_truthy.rb
210
+ - spec/support/matchers/find_with.rb
199
211
  - spec/support/matchers/respond_to_boolean.rb
200
212
  - spec/support/matchers/run_callbacks.rb
201
213
  - spec/support/models.rb
@@ -264,14 +276,19 @@ test_files:
264
276
  - spec/mongomodel/embedded_document_spec.rb
265
277
  - spec/mongomodel/mongomodel_spec.rb
266
278
  - spec/mongomodel/support/collection_spec.rb
279
+ - spec/mongomodel/support/mongo_operator_spec.rb
267
280
  - spec/mongomodel/support/mongo_options_spec.rb
281
+ - spec/mongomodel/support/mongo_order_spec.rb
268
282
  - spec/mongomodel/support/property_spec.rb
283
+ - spec/mongomodel/support/scope_spec.rb
269
284
  - spec/spec_helper.rb
270
285
  - spec/support/callbacks.rb
271
286
  - spec/support/helpers/define_class.rb
287
+ - spec/support/helpers/document_finder_stubs.rb
272
288
  - spec/support/helpers/specs_for.rb
273
289
  - spec/support/matchers/be_a_subclass_of.rb
274
290
  - spec/support/matchers/be_truthy.rb
291
+ - spec/support/matchers/find_with.rb
275
292
  - spec/support/matchers/respond_to_boolean.rb
276
293
  - spec/support/matchers/run_callbacks.rb
277
294
  - spec/support/models.rb
@@ -1,82 +0,0 @@
1
- require 'active_support/core_ext/hash/keys'
2
-
3
- module MongoModel
4
- module DocumentExtensions
5
- module Finders
6
- def find(*args)
7
- options = args.extract_options!
8
-
9
- case args.first
10
- when :first then find_first(options)
11
- when :last then find_last(options)
12
- when :all then find_all(options)
13
- else find_by_ids(args, options)
14
- end
15
- end
16
-
17
- def first(options={})
18
- find(:first, options)
19
- end
20
-
21
- def last(options={})
22
- find(:last, options)
23
- end
24
-
25
- def all(options={})
26
- find(:all, options)
27
- end
28
-
29
- def count(conditions={})
30
- _find(:conditions => conditions).count
31
- end
32
-
33
- def exists?(id_or_conditions)
34
- case id_or_conditions
35
- when String
36
- exists?(:id => id_or_conditions)
37
- else
38
- count(id_or_conditions) > 0
39
- end
40
- end
41
-
42
- private
43
- def find_first(options={})
44
- _find_and_instantiate(options.merge(:limit => 1)).first
45
- end
46
-
47
- def find_last(options={})
48
- order = MongoOrder.parse(options[:order]) || :id.asc
49
- _find_and_instantiate(options.merge(:order => order.reverse, :limit => 1)).first
50
- end
51
-
52
- def find_all(options={})
53
- _find_and_instantiate(options)
54
- end
55
-
56
- def find_by_ids(ids, options={})
57
- ids.flatten!
58
-
59
- case ids.size
60
- when 0
61
- raise ArgumentError, "At least one id must be specified"
62
- when 1
63
- id = ids.first.to_s
64
- _find_and_instantiate(options.deep_merge(:conditions => { :id => id })).first || raise(DocumentNotFound, "Couldn't find document with id: #{id}")
65
- else
66
- docs = _find_and_instantiate(options.deep_merge(:conditions => { :id.in => ids.map { |id| id.to_s } }))
67
- raise DocumentNotFound if docs.size != ids.size
68
- docs.sort_by { |doc| ids.index(doc.id) }
69
- end
70
- end
71
-
72
- def _find(options={})
73
- selector, options = MongoOptions.new(self, options).to_a
74
- collection.find(selector, options)
75
- end
76
-
77
- def _find_and_instantiate(options={})
78
- _find(options).to_a.map { |doc| from_mongo(doc) }
79
- end
80
- end
81
- end
82
- end