mongoid-multi-db 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (276) hide show
  1. data/CHANGELOG.md +615 -0
  2. data/LICENSE +20 -0
  3. data/README.md +62 -0
  4. data/Rakefile +49 -0
  5. data/lib/config/locales/bg.yml +54 -0
  6. data/lib/config/locales/de.yml +54 -0
  7. data/lib/config/locales/en-GB.yml +55 -0
  8. data/lib/config/locales/en.yml +55 -0
  9. data/lib/config/locales/es.yml +52 -0
  10. data/lib/config/locales/fr.yml +55 -0
  11. data/lib/config/locales/hi.yml +46 -0
  12. data/lib/config/locales/hu.yml +57 -0
  13. data/lib/config/locales/id.yml +55 -0
  14. data/lib/config/locales/it.yml +52 -0
  15. data/lib/config/locales/ja.yml +50 -0
  16. data/lib/config/locales/kr.yml +47 -0
  17. data/lib/config/locales/nl.yml +52 -0
  18. data/lib/config/locales/pl.yml +52 -0
  19. data/lib/config/locales/pt-BR.yml +53 -0
  20. data/lib/config/locales/pt.yml +53 -0
  21. data/lib/config/locales/ro.yml +59 -0
  22. data/lib/config/locales/ru.yml +54 -0
  23. data/lib/config/locales/sv.yml +53 -0
  24. data/lib/config/locales/vi.yml +55 -0
  25. data/lib/config/locales/zh-CN.yml +46 -0
  26. data/lib/mongoid.rb +148 -0
  27. data/lib/mongoid/atomic.rb +230 -0
  28. data/lib/mongoid/atomic/modifiers.rb +243 -0
  29. data/lib/mongoid/atomic/paths.rb +3 -0
  30. data/lib/mongoid/atomic/paths/embedded.rb +43 -0
  31. data/lib/mongoid/atomic/paths/embedded/many.rb +44 -0
  32. data/lib/mongoid/atomic/paths/embedded/one.rb +43 -0
  33. data/lib/mongoid/atomic/paths/root.rb +40 -0
  34. data/lib/mongoid/attributes.rb +234 -0
  35. data/lib/mongoid/attributes/processing.rb +146 -0
  36. data/lib/mongoid/callbacks.rb +135 -0
  37. data/lib/mongoid/collection.rb +153 -0
  38. data/lib/mongoid/collection_proxy.rb +59 -0
  39. data/lib/mongoid/collections.rb +120 -0
  40. data/lib/mongoid/collections/master.rb +45 -0
  41. data/lib/mongoid/collections/operations.rb +44 -0
  42. data/lib/mongoid/collections/retry.rb +46 -0
  43. data/lib/mongoid/components.rb +96 -0
  44. data/lib/mongoid/config.rb +347 -0
  45. data/lib/mongoid/config/database.rb +186 -0
  46. data/lib/mongoid/config/replset_database.rb +82 -0
  47. data/lib/mongoid/connection_proxy.rb +30 -0
  48. data/lib/mongoid/contexts.rb +25 -0
  49. data/lib/mongoid/contexts/enumerable.rb +288 -0
  50. data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
  51. data/lib/mongoid/contexts/mongo.rb +409 -0
  52. data/lib/mongoid/copyable.rb +48 -0
  53. data/lib/mongoid/criteria.rb +418 -0
  54. data/lib/mongoid/criterion/builder.rb +34 -0
  55. data/lib/mongoid/criterion/complex.rb +84 -0
  56. data/lib/mongoid/criterion/creational.rb +34 -0
  57. data/lib/mongoid/criterion/exclusion.rb +108 -0
  58. data/lib/mongoid/criterion/inclusion.rb +305 -0
  59. data/lib/mongoid/criterion/inspection.rb +22 -0
  60. data/lib/mongoid/criterion/optional.rb +232 -0
  61. data/lib/mongoid/criterion/selector.rb +153 -0
  62. data/lib/mongoid/cursor.rb +86 -0
  63. data/lib/mongoid/database_proxy.rb +97 -0
  64. data/lib/mongoid/default_scope.rb +36 -0
  65. data/lib/mongoid/dirty.rb +110 -0
  66. data/lib/mongoid/document.rb +280 -0
  67. data/lib/mongoid/errors.rb +17 -0
  68. data/lib/mongoid/errors/callback.rb +26 -0
  69. data/lib/mongoid/errors/document_not_found.rb +28 -0
  70. data/lib/mongoid/errors/eager_load.rb +25 -0
  71. data/lib/mongoid/errors/invalid_collection.rb +18 -0
  72. data/lib/mongoid/errors/invalid_database.rb +19 -0
  73. data/lib/mongoid/errors/invalid_field.rb +18 -0
  74. data/lib/mongoid/errors/invalid_find.rb +19 -0
  75. data/lib/mongoid/errors/invalid_options.rb +28 -0
  76. data/lib/mongoid/errors/invalid_time.rb +25 -0
  77. data/lib/mongoid/errors/invalid_type.rb +25 -0
  78. data/lib/mongoid/errors/mixed_relations.rb +37 -0
  79. data/lib/mongoid/errors/mongoid_error.rb +26 -0
  80. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +20 -0
  81. data/lib/mongoid/errors/unsaved_document.rb +23 -0
  82. data/lib/mongoid/errors/unsupported_version.rb +20 -0
  83. data/lib/mongoid/errors/validations.rb +23 -0
  84. data/lib/mongoid/extensions.rb +82 -0
  85. data/lib/mongoid/extensions/array/deletion.rb +29 -0
  86. data/lib/mongoid/extensions/false_class/equality.rb +26 -0
  87. data/lib/mongoid/extensions/hash/criteria_helpers.rb +45 -0
  88. data/lib/mongoid/extensions/hash/scoping.rb +25 -0
  89. data/lib/mongoid/extensions/integer/checks.rb +23 -0
  90. data/lib/mongoid/extensions/nil/collectionization.rb +23 -0
  91. data/lib/mongoid/extensions/object/checks.rb +29 -0
  92. data/lib/mongoid/extensions/object/reflections.rb +48 -0
  93. data/lib/mongoid/extensions/object/substitutable.rb +15 -0
  94. data/lib/mongoid/extensions/object/yoda.rb +44 -0
  95. data/lib/mongoid/extensions/object_id/conversions.rb +60 -0
  96. data/lib/mongoid/extensions/proc/scoping.rb +25 -0
  97. data/lib/mongoid/extensions/string/checks.rb +36 -0
  98. data/lib/mongoid/extensions/string/conversions.rb +22 -0
  99. data/lib/mongoid/extensions/string/inflections.rb +118 -0
  100. data/lib/mongoid/extensions/symbol/checks.rb +23 -0
  101. data/lib/mongoid/extensions/symbol/inflections.rb +66 -0
  102. data/lib/mongoid/extensions/true_class/equality.rb +26 -0
  103. data/lib/mongoid/extras.rb +31 -0
  104. data/lib/mongoid/factory.rb +46 -0
  105. data/lib/mongoid/fields.rb +332 -0
  106. data/lib/mongoid/fields/mappings.rb +41 -0
  107. data/lib/mongoid/fields/serializable.rb +201 -0
  108. data/lib/mongoid/fields/serializable/array.rb +49 -0
  109. data/lib/mongoid/fields/serializable/big_decimal.rb +42 -0
  110. data/lib/mongoid/fields/serializable/bignum.rb +10 -0
  111. data/lib/mongoid/fields/serializable/binary.rb +11 -0
  112. data/lib/mongoid/fields/serializable/boolean.rb +43 -0
  113. data/lib/mongoid/fields/serializable/date.rb +51 -0
  114. data/lib/mongoid/fields/serializable/date_time.rb +28 -0
  115. data/lib/mongoid/fields/serializable/fixnum.rb +10 -0
  116. data/lib/mongoid/fields/serializable/float.rb +32 -0
  117. data/lib/mongoid/fields/serializable/foreign_keys/array.rb +42 -0
  118. data/lib/mongoid/fields/serializable/foreign_keys/object.rb +47 -0
  119. data/lib/mongoid/fields/serializable/hash.rb +11 -0
  120. data/lib/mongoid/fields/serializable/integer.rb +44 -0
  121. data/lib/mongoid/fields/serializable/localized.rb +41 -0
  122. data/lib/mongoid/fields/serializable/nil_class.rb +38 -0
  123. data/lib/mongoid/fields/serializable/object.rb +11 -0
  124. data/lib/mongoid/fields/serializable/object_id.rb +31 -0
  125. data/lib/mongoid/fields/serializable/range.rb +42 -0
  126. data/lib/mongoid/fields/serializable/set.rb +42 -0
  127. data/lib/mongoid/fields/serializable/string.rb +27 -0
  128. data/lib/mongoid/fields/serializable/symbol.rb +27 -0
  129. data/lib/mongoid/fields/serializable/time.rb +23 -0
  130. data/lib/mongoid/fields/serializable/time_with_zone.rb +23 -0
  131. data/lib/mongoid/fields/serializable/timekeeping.rb +106 -0
  132. data/lib/mongoid/finders.rb +152 -0
  133. data/lib/mongoid/hierarchy.rb +120 -0
  134. data/lib/mongoid/identity.rb +92 -0
  135. data/lib/mongoid/identity_map.rb +119 -0
  136. data/lib/mongoid/indexes.rb +54 -0
  137. data/lib/mongoid/inspection.rb +54 -0
  138. data/lib/mongoid/javascript.rb +20 -0
  139. data/lib/mongoid/javascript/functions.yml +63 -0
  140. data/lib/mongoid/json.rb +16 -0
  141. data/lib/mongoid/keys.rb +144 -0
  142. data/lib/mongoid/logger.rb +39 -0
  143. data/lib/mongoid/matchers.rb +32 -0
  144. data/lib/mongoid/matchers/all.rb +21 -0
  145. data/lib/mongoid/matchers/and.rb +30 -0
  146. data/lib/mongoid/matchers/default.rb +70 -0
  147. data/lib/mongoid/matchers/exists.rb +23 -0
  148. data/lib/mongoid/matchers/gt.rb +21 -0
  149. data/lib/mongoid/matchers/gte.rb +21 -0
  150. data/lib/mongoid/matchers/in.rb +21 -0
  151. data/lib/mongoid/matchers/lt.rb +21 -0
  152. data/lib/mongoid/matchers/lte.rb +21 -0
  153. data/lib/mongoid/matchers/ne.rb +21 -0
  154. data/lib/mongoid/matchers/nin.rb +21 -0
  155. data/lib/mongoid/matchers/or.rb +33 -0
  156. data/lib/mongoid/matchers/size.rb +21 -0
  157. data/lib/mongoid/matchers/strategies.rb +93 -0
  158. data/lib/mongoid/multi_database.rb +31 -0
  159. data/lib/mongoid/multi_parameter_attributes.rb +106 -0
  160. data/lib/mongoid/named_scope.rb +146 -0
  161. data/lib/mongoid/nested_attributes.rb +54 -0
  162. data/lib/mongoid/observer.rb +170 -0
  163. data/lib/mongoid/paranoia.rb +158 -0
  164. data/lib/mongoid/persistence.rb +264 -0
  165. data/lib/mongoid/persistence/atomic.rb +223 -0
  166. data/lib/mongoid/persistence/atomic/add_to_set.rb +35 -0
  167. data/lib/mongoid/persistence/atomic/bit.rb +37 -0
  168. data/lib/mongoid/persistence/atomic/inc.rb +31 -0
  169. data/lib/mongoid/persistence/atomic/operation.rb +85 -0
  170. data/lib/mongoid/persistence/atomic/pop.rb +34 -0
  171. data/lib/mongoid/persistence/atomic/pull.rb +34 -0
  172. data/lib/mongoid/persistence/atomic/pull_all.rb +34 -0
  173. data/lib/mongoid/persistence/atomic/push.rb +31 -0
  174. data/lib/mongoid/persistence/atomic/push_all.rb +31 -0
  175. data/lib/mongoid/persistence/atomic/rename.rb +31 -0
  176. data/lib/mongoid/persistence/atomic/sets.rb +30 -0
  177. data/lib/mongoid/persistence/atomic/unset.rb +28 -0
  178. data/lib/mongoid/persistence/deletion.rb +32 -0
  179. data/lib/mongoid/persistence/insertion.rb +41 -0
  180. data/lib/mongoid/persistence/modification.rb +37 -0
  181. data/lib/mongoid/persistence/operations.rb +211 -0
  182. data/lib/mongoid/persistence/operations/embedded/insert.rb +42 -0
  183. data/lib/mongoid/persistence/operations/embedded/remove.rb +40 -0
  184. data/lib/mongoid/persistence/operations/insert.rb +34 -0
  185. data/lib/mongoid/persistence/operations/remove.rb +33 -0
  186. data/lib/mongoid/persistence/operations/update.rb +64 -0
  187. data/lib/mongoid/railtie.rb +126 -0
  188. data/lib/mongoid/railties/database.rake +182 -0
  189. data/lib/mongoid/railties/document.rb +12 -0
  190. data/lib/mongoid/relations.rb +144 -0
  191. data/lib/mongoid/relations/accessors.rb +138 -0
  192. data/lib/mongoid/relations/auto_save.rb +38 -0
  193. data/lib/mongoid/relations/binding.rb +26 -0
  194. data/lib/mongoid/relations/bindings.rb +9 -0
  195. data/lib/mongoid/relations/bindings/embedded/in.rb +69 -0
  196. data/lib/mongoid/relations/bindings/embedded/many.rb +93 -0
  197. data/lib/mongoid/relations/bindings/embedded/one.rb +61 -0
  198. data/lib/mongoid/relations/bindings/referenced/in.rb +76 -0
  199. data/lib/mongoid/relations/bindings/referenced/many.rb +54 -0
  200. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +51 -0
  201. data/lib/mongoid/relations/bindings/referenced/one.rb +58 -0
  202. data/lib/mongoid/relations/builder.rb +57 -0
  203. data/lib/mongoid/relations/builders.rb +83 -0
  204. data/lib/mongoid/relations/builders/embedded/in.rb +29 -0
  205. data/lib/mongoid/relations/builders/embedded/many.rb +40 -0
  206. data/lib/mongoid/relations/builders/embedded/one.rb +30 -0
  207. data/lib/mongoid/relations/builders/nested_attributes/many.rb +110 -0
  208. data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
  209. data/lib/mongoid/relations/builders/referenced/in.rb +26 -0
  210. data/lib/mongoid/relations/builders/referenced/many.rb +27 -0
  211. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +38 -0
  212. data/lib/mongoid/relations/builders/referenced/one.rb +26 -0
  213. data/lib/mongoid/relations/cascading.rb +56 -0
  214. data/lib/mongoid/relations/cascading/delete.rb +19 -0
  215. data/lib/mongoid/relations/cascading/destroy.rb +26 -0
  216. data/lib/mongoid/relations/cascading/nullify.rb +18 -0
  217. data/lib/mongoid/relations/cascading/strategy.rb +26 -0
  218. data/lib/mongoid/relations/constraint.rb +42 -0
  219. data/lib/mongoid/relations/conversions.rb +35 -0
  220. data/lib/mongoid/relations/cyclic.rb +103 -0
  221. data/lib/mongoid/relations/embedded/atomic.rb +89 -0
  222. data/lib/mongoid/relations/embedded/atomic/operation.rb +63 -0
  223. data/lib/mongoid/relations/embedded/atomic/pull.rb +65 -0
  224. data/lib/mongoid/relations/embedded/atomic/push_all.rb +59 -0
  225. data/lib/mongoid/relations/embedded/atomic/set.rb +61 -0
  226. data/lib/mongoid/relations/embedded/atomic/unset.rb +41 -0
  227. data/lib/mongoid/relations/embedded/in.rb +220 -0
  228. data/lib/mongoid/relations/embedded/many.rb +560 -0
  229. data/lib/mongoid/relations/embedded/one.rb +206 -0
  230. data/lib/mongoid/relations/embedded/sort.rb +31 -0
  231. data/lib/mongoid/relations/macros.rb +310 -0
  232. data/lib/mongoid/relations/many.rb +135 -0
  233. data/lib/mongoid/relations/metadata.rb +919 -0
  234. data/lib/mongoid/relations/nested_builder.rb +75 -0
  235. data/lib/mongoid/relations/one.rb +36 -0
  236. data/lib/mongoid/relations/options.rb +47 -0
  237. data/lib/mongoid/relations/polymorphic.rb +40 -0
  238. data/lib/mongoid/relations/proxy.rb +145 -0
  239. data/lib/mongoid/relations/referenced/batch.rb +72 -0
  240. data/lib/mongoid/relations/referenced/batch/insert.rb +57 -0
  241. data/lib/mongoid/relations/referenced/in.rb +262 -0
  242. data/lib/mongoid/relations/referenced/many.rb +623 -0
  243. data/lib/mongoid/relations/referenced/many_to_many.rb +396 -0
  244. data/lib/mongoid/relations/referenced/one.rb +272 -0
  245. data/lib/mongoid/relations/reflections.rb +62 -0
  246. data/lib/mongoid/relations/synchronization.rb +153 -0
  247. data/lib/mongoid/relations/targets.rb +2 -0
  248. data/lib/mongoid/relations/targets/enumerable.rb +372 -0
  249. data/lib/mongoid/reloading.rb +91 -0
  250. data/lib/mongoid/safety.rb +105 -0
  251. data/lib/mongoid/scope.rb +31 -0
  252. data/lib/mongoid/serialization.rb +134 -0
  253. data/lib/mongoid/sharding.rb +61 -0
  254. data/lib/mongoid/state.rb +97 -0
  255. data/lib/mongoid/threaded.rb +530 -0
  256. data/lib/mongoid/threaded/lifecycle.rb +192 -0
  257. data/lib/mongoid/timestamps.rb +15 -0
  258. data/lib/mongoid/timestamps/created.rb +24 -0
  259. data/lib/mongoid/timestamps/timeless.rb +50 -0
  260. data/lib/mongoid/timestamps/updated.rb +26 -0
  261. data/lib/mongoid/validations.rb +140 -0
  262. data/lib/mongoid/validations/associated.rb +46 -0
  263. data/lib/mongoid/validations/uniqueness.rb +145 -0
  264. data/lib/mongoid/version.rb +4 -0
  265. data/lib/mongoid/versioning.rb +185 -0
  266. data/lib/rack/mongoid.rb +2 -0
  267. data/lib/rack/mongoid/middleware/identity_map.rb +38 -0
  268. data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
  269. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +20 -0
  270. data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
  271. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +19 -0
  272. data/lib/rails/generators/mongoid/observer/observer_generator.rb +17 -0
  273. data/lib/rails/generators/mongoid/observer/templates/observer.rb.tt +4 -0
  274. data/lib/rails/generators/mongoid_generator.rb +70 -0
  275. data/lib/rails/mongoid.rb +91 -0
  276. metadata +465 -0
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Contexts #:nodoc:
4
+ class Enumerable
5
+ class Sort
6
+ attr_reader :value, :direction
7
+
8
+ # Create a new sorting object. This requires a value and a sort
9
+ # direction of +:asc+ or +:desc+.
10
+ def initialize(value, direction)
11
+ @value = value
12
+ @direction = direction
13
+ end
14
+
15
+ # Return +true+ if the direction is +:asc+, otherwise false.
16
+ def ascending?
17
+ direction == :asc
18
+ end
19
+
20
+ # Compare two +Sort+ objects against each other, taking into
21
+ # consideration the direction of the sorting.
22
+ def <=>(other)
23
+ cmp = compare(value, other.value)
24
+ ascending? ? cmp : cmp * -1
25
+ end
26
+
27
+ private
28
+
29
+ # Compare two values allowing for nil values.
30
+ def compare(a, b)
31
+ case
32
+ when a.nil?
33
+ b.nil? ? 0 : 1
34
+ when b.nil?
35
+ -1
36
+ else
37
+ a <=> b
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,409 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Contexts #:nodoc:
4
+ class Mongo
5
+ attr_accessor :criteria
6
+
7
+ delegate :cached?, :klass, :options, :field_list, :selector, :to => :criteria
8
+ delegate :collection, :to => :klass
9
+
10
+ # Perform an add to set on the matching documents.
11
+ #
12
+ # @example Add to set on all matching.
13
+ # Person.where(:name => "Alex").add_to_set(:aliases, "value")
14
+ #
15
+ # @param [ String ] field The field to add to.
16
+ # @param [ Object ] value The value to add.
17
+ #
18
+ # @return [ Object ] The update value.
19
+ #
20
+ # @since 2.1.0
21
+ def add_to_set(field, value)
22
+ klass.collection.update(
23
+ selector,
24
+ { "$addToSet" => { field => value } },
25
+ :multi => true
26
+ )
27
+ end
28
+
29
+ # Aggregate the context. This will take the internally built selector and options
30
+ # and pass them on to the Ruby driver's +group()+ method on the collection. The
31
+ # collection itself will be retrieved from the class provided, and once the
32
+ # query has returned it will provided a grouping of keys with counts.
33
+ #
34
+ # @example Aggreate the context.
35
+ # context.aggregate
36
+ #
37
+ # @return [ Hash ] A +Hash+ with field values as keys, counts as values
38
+ def aggregate
39
+ klass.collection.group(
40
+ :key => field_list,
41
+ :cond => selector,
42
+ :initial => { :count => 0 },
43
+ :reduce => Javascript.aggregate
44
+ )
45
+ end
46
+
47
+ # Get the average value for the supplied field.
48
+ #
49
+ # This will take the internally built selector and options
50
+ # and pass them on to the Ruby driver's +group()+ method on the collection. The
51
+ # collection itself will be retrieved from the class provided, and once the
52
+ # query has returned it will provided a grouping of keys with averages.
53
+ #
54
+ # @example Get the average for a field.
55
+ # context.avg(:age)
56
+ #
57
+ # @param [ Symbol ] field The field to get the average for.
58
+ #
59
+ # @return [ Numeric ] A numeric value that is the average.
60
+ def avg(field)
61
+ total = sum(field)
62
+ total ? (total / count) : nil
63
+ end
64
+
65
+ # Determine if the context is empty or blank given the criteria. Will
66
+ # perform a quick has_one asking only for the id.
67
+ #
68
+ # @example Is the context empty?
69
+ # context.blank?a
70
+ #
71
+ # @return [ true, false ] True if blank.
72
+ def blank?
73
+ klass.collection.find_one(selector, { :fields => [ :_id ] }).nil?
74
+ end
75
+ alias :empty? :blank?
76
+
77
+ # Get the count of matching documents in the database for the context.
78
+ #
79
+ # @example Get the count without skip and limit taken into consideration.
80
+ # context.count
81
+ #
82
+ # @example Get the count with skip and limit applied.
83
+ # context.count(true)
84
+ #
85
+ # @param [Boolean] extras True to inclued previous skip/limit
86
+ # statements in the count; false to ignore them. Defaults to `false`.
87
+ #
88
+ # @return [ Integer ] The count of documents.
89
+ def count(extras = false)
90
+ if cached?
91
+ @count ||= collection.find(selector, process_options).count(extras)
92
+ else
93
+ collection.find(selector, process_options).count(extras)
94
+ end
95
+ end
96
+ alias :size :count
97
+ alias :length :count
98
+
99
+ # Delete all the documents in the database matching the selector.
100
+ #
101
+ # @example Delete the documents.
102
+ # context.delete_all
103
+ #
104
+ # @return [ Integer ] The number of documents deleted.
105
+ #
106
+ # @since 2.0.0.rc.1
107
+ def delete_all
108
+ klass.delete_all(:conditions => selector)
109
+ end
110
+ alias :delete :delete_all
111
+
112
+ # Destroy all the documents in the database matching the selector.
113
+ #
114
+ # @example Destroy the documents.
115
+ # context.destroy_all
116
+ #
117
+ # @return [ Integer ] The number of documents destroyed.
118
+ #
119
+ # @since 2.0.0.rc.1
120
+ def destroy_all
121
+ klass.destroy_all(:conditions => selector)
122
+ end
123
+ alias :destroy :destroy_all
124
+
125
+ # Gets an array of distinct values for the supplied field across the
126
+ # entire collection or the susbset given the criteria.
127
+ #
128
+ # @example Get the distinct values.
129
+ # context.distinct(:title)
130
+ #
131
+ # @param [ Symbol ] field The field to get the values for.
132
+ #
133
+ # @return [ Array<Object> ] The distinct values for the field.
134
+ def distinct(field)
135
+ klass.collection.distinct(field, selector)
136
+ end
137
+
138
+ # Execute the context. This will take the selector and options
139
+ # and pass them on to the Ruby driver's +find()+ method on the collection. The
140
+ # collection itself will be retrieved from the class provided, and once the
141
+ # query has returned new documents of the type of class provided will be instantiated.
142
+ #
143
+ # @example Execute the criteria on the context.
144
+ # context.execute
145
+ #
146
+ # @return [ Cursor ] An enumerable +Cursor+ of results.
147
+ def execute
148
+ criteria.inclusions.reject! do |metadata|
149
+ metadata.eager_load(criteria)
150
+ end
151
+ klass.collection.find(selector, process_options) || []
152
+ end
153
+
154
+ # Return the first result for the +Context+.
155
+ #
156
+ # @example Get the first document.
157
+ # context.one
158
+ #
159
+ # @return [ Document ] The first document in the collection.
160
+ def first
161
+ attributes = klass.collection.find_one(selector, options_with_default_sorting)
162
+ attributes ? Mongoid::Factory.from_db(klass, attributes) : nil
163
+ end
164
+ alias :one :first
165
+
166
+ # Groups the context. This will take the internally built selector and options
167
+ # and pass them on to the Ruby driver's +group()+ method on the collection. The
168
+ # collection itself will be retrieved from the class provided, and once the
169
+ # query has returned it will provided a grouping of keys with objects.
170
+ #
171
+ # @example Get the criteria as a group.
172
+ # context.group
173
+ #
174
+ # @return [ Hash ] Hash with field values as keys, arrays of documents as values.
175
+ def group
176
+ klass.collection.group(
177
+ :key => field_list,
178
+ :cond => selector,
179
+ :initial => { :group => [] },
180
+ :reduce => Javascript.group
181
+ ).collect do |docs|
182
+ docs["group"] = docs["group"].collect do |attrs|
183
+ Mongoid::Factory.from_db(klass, attrs)
184
+ end
185
+ docs
186
+ end
187
+ end
188
+
189
+ # Create the new mongo context. This will execute the queries given the
190
+ # selector and options against the database.
191
+ #
192
+ # @example Create a new context.
193
+ # Mongoid::Contexts::Mongo.new(criteria)
194
+ #
195
+ # @param [ Criteria ] criteria The criteria to create with.
196
+ def initialize(criteria)
197
+ @criteria = criteria
198
+ if klass.hereditary? && !criteria.selector.keys.include?(:_type)
199
+ @criteria = criteria.in(:_type => criteria.klass._types)
200
+ end
201
+ @criteria.cache if klass.cached?
202
+ end
203
+
204
+ # Iterate over each +Document+ in the results. This can take an optional
205
+ # block to pass to each argument in the results.
206
+ #
207
+ # @example Iterate over the results.
208
+ # context.iterate { |doc| p doc }
209
+ def iterate(&block)
210
+ return caching(&block) if cached?
211
+ if block_given?
212
+ execute.each { |doc| yield doc }
213
+ end
214
+ end
215
+
216
+ # Return the last result for the +Context+. Essentially does a find_one on
217
+ # the collection with the sorting reversed. If no sorting parameters have
218
+ # been provided it will default to ids.
219
+ #
220
+ # @example Get the last document.
221
+ # context.last
222
+ #
223
+ # @return [ Document ] The last document in the collection.
224
+ def last
225
+ opts = options_with_default_sorting
226
+ opts[:sort] = opts[:sort].map{ |option| [ option[0], option[1].invert ] }.uniq
227
+ attributes = klass.collection.find_one(selector, opts)
228
+ attributes ? Mongoid::Factory.from_db(klass, attributes) : nil
229
+ end
230
+
231
+ # Return the max value for a field.
232
+ #
233
+ # This will take the internally built selector and options
234
+ # and pass them on to the Ruby driver's +group()+ method on the collection. The
235
+ # collection itself will be retrieved from the class provided, and once the
236
+ # query has returned it will provided a grouping of keys with sums.
237
+ #
238
+ # @example Get the max value.
239
+ # context.max(:age)
240
+ #
241
+ # @param [ Symbol ] field The field to get the max for.
242
+ #
243
+ # @return [ Numeric ] A numeric max value.
244
+ def max(field)
245
+ grouped(:max, field.to_s, Javascript.max, Javascript.max_finalize)
246
+ end
247
+
248
+ # Return the min value for a field.
249
+ #
250
+ # This will take the internally built selector and options
251
+ # and pass them on to the Ruby driver's +group()+ method on the collection. The
252
+ # collection itself will be retrieved from the class provided, and once the
253
+ # query has returned it will provided a grouping of keys with sums.
254
+ #
255
+ # @example Get the min value.
256
+ # context.min(:age)
257
+ #
258
+ # @param [ Symbol ] field The field to get the min for.
259
+ #
260
+ # @return [ Numeric ] A numeric minimum value.
261
+ def min(field)
262
+ grouped(:min, field.to_s, Javascript.min, Javascript.min_finalize)
263
+ end
264
+
265
+ # Perform a pull on the matching documents.
266
+ #
267
+ # @example Pull on all matching.
268
+ # Person.where(:name => "Alex").pull(:aliases, "value")
269
+ #
270
+ # @param [ String ] field The field to pull from.
271
+ # @param [ Object ] value The value to pull.
272
+ #
273
+ # @return [ Object ] The update value.
274
+ #
275
+ # @since 2.1.0
276
+ def pull(field, value)
277
+ klass.collection.update(
278
+ selector,
279
+ { "$pull" => { field => value } },
280
+ :multi => true
281
+ )
282
+ end
283
+
284
+ # Return the first result for the +Context+ and skip it
285
+ # for successive calls.
286
+ #
287
+ # @example Get the first document and shift.
288
+ # context.shift
289
+ #
290
+ # @return [ Document ] The first document in the collection.
291
+ def shift
292
+ first.tap { criteria.skip((options[:skip] || 0) + 1) }
293
+ end
294
+
295
+ # Sum the context.
296
+ #
297
+ # This will take the internally built selector and options
298
+ # and pass them on to the Ruby driver's +group()+ method on the collection. The
299
+ # collection itself will be retrieved from the class provided, and once the
300
+ # query has returned it will provided a grouping of keys with sums.
301
+ #
302
+ # @example Get the sum for a field.
303
+ # context.sum(:age)
304
+ #
305
+ # @param [ Symbol ] field The field who's values to sum.
306
+ #
307
+ # @return [ Numeric ] A numeric value that is the sum.
308
+ def sum(field)
309
+ grouped(:sum, field.to_s, Javascript.sum, Javascript.sum_finalize)
310
+ end
311
+
312
+ # Very basic update that will perform a simple atomic $set of the
313
+ # attributes provided in the hash. Can be expanded to later for more
314
+ # robust functionality.
315
+ #
316
+ # @example Update all matching documents.
317
+ # context.update_all(:title => "Sir")
318
+ #
319
+ # @param [ Hash ] attributes The sets to perform.
320
+ #
321
+ # @since 2.0.0.rc.4
322
+ def update_all(attributes = {})
323
+ klass.collection.update(
324
+ selector,
325
+ { "$set" => attributes },
326
+ Safety.merge_safety_options(:multi => true)
327
+ ).tap do
328
+ Threaded.clear_options!
329
+ end
330
+ end
331
+ alias :update :update_all
332
+
333
+ protected
334
+
335
+ # Iterate over each +Document+ in the results and cache the collection.
336
+ #
337
+ # @example Execute with caching.
338
+ # context.caching
339
+ def caching(&block)
340
+ if defined? @collection
341
+ @collection.each(&block)
342
+ else
343
+ @collection = []
344
+ execute.each do |doc|
345
+ @collection << doc
346
+ yield doc if block_given?
347
+ end
348
+ end
349
+ end
350
+
351
+ # Common functionality for grouping operations. Currently used by min, max
352
+ # and sum. Will gsub the field name in the supplied reduce function.
353
+ #
354
+ # @example Execute the group function.
355
+ # context.group(0, :avg, "")
356
+ #
357
+ # @param [ Object ] start The value to start the map/reduce with.
358
+ # @param [ String ] field The field to aggregate.
359
+ # @param [ String ] reduce The reduce JS function.
360
+ #
361
+ # @return [ Numeric ] A numeric result.
362
+ def grouped(start, field, reduce, finalize)
363
+ collection = klass.collection.group(
364
+ :cond => selector,
365
+ :initial => { start => "start" },
366
+ :finalize => finalize,
367
+ :reduce => reduce.gsub("[field]", field)
368
+ )
369
+ collection.empty? ? nil : collection.first[start.to_s]
370
+ end
371
+
372
+ # Get the options hash with the default sorting options provided.
373
+ #
374
+ # @example Get the options.
375
+ # criteria.options_with_default_sorting
376
+ #
377
+ # @return [ Hash ] The options.
378
+ #
379
+ # @since 2.3.2
380
+ def options_with_default_sorting
381
+ process_options.tap do |opts|
382
+ if opts[:sort].blank?
383
+ opts[:sort] = [[ :_id, :asc ]]
384
+ end
385
+ end
386
+ end
387
+
388
+ # Filters the field list. If no fields have been supplied, then it will be
389
+ # empty. If fields have been defined then _type will be included as well.
390
+ #
391
+ # @example Process the field list.
392
+ # context.process_options
393
+ #
394
+ # @return [ Hash ] The options.
395
+ def process_options
396
+ fields = options[:fields]
397
+ if fields && fields.size > 0 && !fields.include?(:_type)
398
+ if fields.kind_of?(Hash)
399
+ fields[:_type] = 1 if fields.first.last != 0 # Not excluding
400
+ else
401
+ fields << :type
402
+ end
403
+ options[:fields] = fields
404
+ end
405
+ options.dup
406
+ end
407
+ end
408
+ end
409
+ end