mongoid-braxton 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (226) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +50 -0
  3. data/Rakefile +51 -0
  4. data/lib/config/locales/bg.yml +41 -0
  5. data/lib/config/locales/de.yml +41 -0
  6. data/lib/config/locales/en.yml +45 -0
  7. data/lib/config/locales/es.yml +41 -0
  8. data/lib/config/locales/fr.yml +42 -0
  9. data/lib/config/locales/hu.yml +44 -0
  10. data/lib/config/locales/id.yml +46 -0
  11. data/lib/config/locales/it.yml +39 -0
  12. data/lib/config/locales/ja.yml +40 -0
  13. data/lib/config/locales/kr.yml +65 -0
  14. data/lib/config/locales/nl.yml +39 -0
  15. data/lib/config/locales/pl.yml +39 -0
  16. data/lib/config/locales/pt-BR.yml +40 -0
  17. data/lib/config/locales/pt.yml +40 -0
  18. data/lib/config/locales/ro.yml +46 -0
  19. data/lib/config/locales/ru.yml +41 -0
  20. data/lib/config/locales/sv.yml +40 -0
  21. data/lib/config/locales/vi.yml +45 -0
  22. data/lib/config/locales/zh-CN.yml +33 -0
  23. data/lib/mongoid.rb +140 -0
  24. data/lib/mongoid/atomicity.rb +111 -0
  25. data/lib/mongoid/attributes.rb +185 -0
  26. data/lib/mongoid/attributes/processing.rb +145 -0
  27. data/lib/mongoid/callbacks.rb +23 -0
  28. data/lib/mongoid/collection.rb +137 -0
  29. data/lib/mongoid/collections.rb +71 -0
  30. data/lib/mongoid/collections/master.rb +37 -0
  31. data/lib/mongoid/collections/operations.rb +42 -0
  32. data/lib/mongoid/collections/retry.rb +39 -0
  33. data/lib/mongoid/components.rb +45 -0
  34. data/lib/mongoid/config.rb +349 -0
  35. data/lib/mongoid/config/database.rb +167 -0
  36. data/lib/mongoid/config/replset_database.rb +78 -0
  37. data/lib/mongoid/contexts.rb +19 -0
  38. data/lib/mongoid/contexts/enumerable.rb +275 -0
  39. data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
  40. data/lib/mongoid/contexts/mongo.rb +345 -0
  41. data/lib/mongoid/copyable.rb +46 -0
  42. data/lib/mongoid/criteria.rb +357 -0
  43. data/lib/mongoid/criterion/builder.rb +34 -0
  44. data/lib/mongoid/criterion/complex.rb +34 -0
  45. data/lib/mongoid/criterion/creational.rb +34 -0
  46. data/lib/mongoid/criterion/exclusion.rb +108 -0
  47. data/lib/mongoid/criterion/inclusion.rb +198 -0
  48. data/lib/mongoid/criterion/inspection.rb +22 -0
  49. data/lib/mongoid/criterion/optional.rb +193 -0
  50. data/lib/mongoid/criterion/selector.rb +143 -0
  51. data/lib/mongoid/criterion/unconvertable.rb +20 -0
  52. data/lib/mongoid/cursor.rb +86 -0
  53. data/lib/mongoid/default_scope.rb +36 -0
  54. data/lib/mongoid/dirty.rb +253 -0
  55. data/lib/mongoid/document.rb +284 -0
  56. data/lib/mongoid/errors.rb +13 -0
  57. data/lib/mongoid/errors/document_not_found.rb +29 -0
  58. data/lib/mongoid/errors/invalid_collection.rb +19 -0
  59. data/lib/mongoid/errors/invalid_database.rb +20 -0
  60. data/lib/mongoid/errors/invalid_field.rb +19 -0
  61. data/lib/mongoid/errors/invalid_options.rb +16 -0
  62. data/lib/mongoid/errors/invalid_type.rb +26 -0
  63. data/lib/mongoid/errors/mixed_relations.rb +37 -0
  64. data/lib/mongoid/errors/mongoid_error.rb +27 -0
  65. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +21 -0
  66. data/lib/mongoid/errors/unsaved_document.rb +23 -0
  67. data/lib/mongoid/errors/unsupported_version.rb +21 -0
  68. data/lib/mongoid/errors/validations.rb +24 -0
  69. data/lib/mongoid/extensions.rb +123 -0
  70. data/lib/mongoid/extensions/array/conversions.rb +23 -0
  71. data/lib/mongoid/extensions/array/parentization.rb +13 -0
  72. data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
  73. data/lib/mongoid/extensions/binary/conversions.rb +17 -0
  74. data/lib/mongoid/extensions/boolean/conversions.rb +27 -0
  75. data/lib/mongoid/extensions/date/conversions.rb +25 -0
  76. data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
  77. data/lib/mongoid/extensions/false_class/equality.rb +13 -0
  78. data/lib/mongoid/extensions/float/conversions.rb +20 -0
  79. data/lib/mongoid/extensions/hash/conversions.rb +19 -0
  80. data/lib/mongoid/extensions/hash/criteria_helpers.rb +22 -0
  81. data/lib/mongoid/extensions/hash/scoping.rb +12 -0
  82. data/lib/mongoid/extensions/integer/conversions.rb +20 -0
  83. data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
  84. data/lib/mongoid/extensions/object/checks.rb +32 -0
  85. data/lib/mongoid/extensions/object/conversions.rb +25 -0
  86. data/lib/mongoid/extensions/object/reflections.rb +17 -0
  87. data/lib/mongoid/extensions/object/yoda.rb +27 -0
  88. data/lib/mongoid/extensions/object_id/conversions.rb +96 -0
  89. data/lib/mongoid/extensions/proc/scoping.rb +12 -0
  90. data/lib/mongoid/extensions/range/conversions.rb +25 -0
  91. data/lib/mongoid/extensions/set/conversions.rb +20 -0
  92. data/lib/mongoid/extensions/string/conversions.rb +34 -0
  93. data/lib/mongoid/extensions/string/inflections.rb +97 -0
  94. data/lib/mongoid/extensions/symbol/conversions.rb +21 -0
  95. data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
  96. data/lib/mongoid/extensions/time_conversions.rb +38 -0
  97. data/lib/mongoid/extensions/true_class/equality.rb +13 -0
  98. data/lib/mongoid/extras.rb +42 -0
  99. data/lib/mongoid/factory.rb +37 -0
  100. data/lib/mongoid/field.rb +162 -0
  101. data/lib/mongoid/fields.rb +183 -0
  102. data/lib/mongoid/finders.rb +127 -0
  103. data/lib/mongoid/hierarchy.rb +85 -0
  104. data/lib/mongoid/identity.rb +92 -0
  105. data/lib/mongoid/indexes.rb +38 -0
  106. data/lib/mongoid/inspection.rb +54 -0
  107. data/lib/mongoid/javascript.rb +21 -0
  108. data/lib/mongoid/javascript/functions.yml +37 -0
  109. data/lib/mongoid/json.rb +16 -0
  110. data/lib/mongoid/keys.rb +131 -0
  111. data/lib/mongoid/logger.rb +18 -0
  112. data/lib/mongoid/matchers.rb +32 -0
  113. data/lib/mongoid/matchers/all.rb +11 -0
  114. data/lib/mongoid/matchers/default.rb +70 -0
  115. data/lib/mongoid/matchers/exists.rb +13 -0
  116. data/lib/mongoid/matchers/gt.rb +11 -0
  117. data/lib/mongoid/matchers/gte.rb +11 -0
  118. data/lib/mongoid/matchers/in.rb +11 -0
  119. data/lib/mongoid/matchers/lt.rb +11 -0
  120. data/lib/mongoid/matchers/lte.rb +11 -0
  121. data/lib/mongoid/matchers/ne.rb +11 -0
  122. data/lib/mongoid/matchers/nin.rb +11 -0
  123. data/lib/mongoid/matchers/or.rb +30 -0
  124. data/lib/mongoid/matchers/size.rb +11 -0
  125. data/lib/mongoid/matchers/strategies.rb +63 -0
  126. data/lib/mongoid/multi_database.rb +11 -0
  127. data/lib/mongoid/multi_parameter_attributes.rb +82 -0
  128. data/lib/mongoid/named_scope.rb +137 -0
  129. data/lib/mongoid/nested_attributes.rb +51 -0
  130. data/lib/mongoid/observer.rb +67 -0
  131. data/lib/mongoid/paranoia.rb +103 -0
  132. data/lib/mongoid/paths.rb +61 -0
  133. data/lib/mongoid/persistence.rb +240 -0
  134. data/lib/mongoid/persistence/atomic.rb +88 -0
  135. data/lib/mongoid/persistence/atomic/add_to_set.rb +32 -0
  136. data/lib/mongoid/persistence/atomic/inc.rb +28 -0
  137. data/lib/mongoid/persistence/atomic/operation.rb +44 -0
  138. data/lib/mongoid/persistence/atomic/pull_all.rb +33 -0
  139. data/lib/mongoid/persistence/atomic/push.rb +28 -0
  140. data/lib/mongoid/persistence/command.rb +71 -0
  141. data/lib/mongoid/persistence/insert.rb +53 -0
  142. data/lib/mongoid/persistence/insert_embedded.rb +43 -0
  143. data/lib/mongoid/persistence/remove.rb +44 -0
  144. data/lib/mongoid/persistence/remove_all.rb +40 -0
  145. data/lib/mongoid/persistence/remove_embedded.rb +48 -0
  146. data/lib/mongoid/persistence/update.rb +77 -0
  147. data/lib/mongoid/railtie.rb +139 -0
  148. data/lib/mongoid/railties/database.rake +171 -0
  149. data/lib/mongoid/railties/document.rb +12 -0
  150. data/lib/mongoid/relations.rb +107 -0
  151. data/lib/mongoid/relations/accessors.rb +175 -0
  152. data/lib/mongoid/relations/auto_save.rb +34 -0
  153. data/lib/mongoid/relations/binding.rb +26 -0
  154. data/lib/mongoid/relations/bindings.rb +9 -0
  155. data/lib/mongoid/relations/bindings/embedded/in.rb +82 -0
  156. data/lib/mongoid/relations/bindings/embedded/many.rb +98 -0
  157. data/lib/mongoid/relations/bindings/embedded/one.rb +66 -0
  158. data/lib/mongoid/relations/bindings/referenced/in.rb +74 -0
  159. data/lib/mongoid/relations/bindings/referenced/many.rb +96 -0
  160. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +103 -0
  161. data/lib/mongoid/relations/bindings/referenced/one.rb +66 -0
  162. data/lib/mongoid/relations/builder.rb +42 -0
  163. data/lib/mongoid/relations/builders.rb +79 -0
  164. data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
  165. data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
  166. data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
  167. data/lib/mongoid/relations/builders/nested_attributes/many.rb +126 -0
  168. data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
  169. data/lib/mongoid/relations/builders/referenced/in.rb +29 -0
  170. data/lib/mongoid/relations/builders/referenced/many.rb +47 -0
  171. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
  172. data/lib/mongoid/relations/builders/referenced/one.rb +27 -0
  173. data/lib/mongoid/relations/cascading.rb +55 -0
  174. data/lib/mongoid/relations/cascading/delete.rb +19 -0
  175. data/lib/mongoid/relations/cascading/destroy.rb +19 -0
  176. data/lib/mongoid/relations/cascading/nullify.rb +18 -0
  177. data/lib/mongoid/relations/cascading/strategy.rb +26 -0
  178. data/lib/mongoid/relations/constraint.rb +42 -0
  179. data/lib/mongoid/relations/cyclic.rb +103 -0
  180. data/lib/mongoid/relations/embedded/atomic.rb +86 -0
  181. data/lib/mongoid/relations/embedded/atomic/operation.rb +63 -0
  182. data/lib/mongoid/relations/embedded/atomic/pull.rb +65 -0
  183. data/lib/mongoid/relations/embedded/atomic/push_all.rb +59 -0
  184. data/lib/mongoid/relations/embedded/atomic/set.rb +61 -0
  185. data/lib/mongoid/relations/embedded/atomic/unset.rb +41 -0
  186. data/lib/mongoid/relations/embedded/in.rb +173 -0
  187. data/lib/mongoid/relations/embedded/many.rb +499 -0
  188. data/lib/mongoid/relations/embedded/one.rb +170 -0
  189. data/lib/mongoid/relations/macros.rb +310 -0
  190. data/lib/mongoid/relations/many.rb +215 -0
  191. data/lib/mongoid/relations/metadata.rb +539 -0
  192. data/lib/mongoid/relations/nested_builder.rb +68 -0
  193. data/lib/mongoid/relations/one.rb +47 -0
  194. data/lib/mongoid/relations/polymorphic.rb +54 -0
  195. data/lib/mongoid/relations/proxy.rb +143 -0
  196. data/lib/mongoid/relations/referenced/batch.rb +71 -0
  197. data/lib/mongoid/relations/referenced/batch/insert.rb +57 -0
  198. data/lib/mongoid/relations/referenced/in.rb +216 -0
  199. data/lib/mongoid/relations/referenced/many.rb +516 -0
  200. data/lib/mongoid/relations/referenced/many_to_many.rb +396 -0
  201. data/lib/mongoid/relations/referenced/one.rb +222 -0
  202. data/lib/mongoid/relations/reflections.rb +45 -0
  203. data/lib/mongoid/safe.rb +23 -0
  204. data/lib/mongoid/safety.rb +207 -0
  205. data/lib/mongoid/scope.rb +31 -0
  206. data/lib/mongoid/serialization.rb +99 -0
  207. data/lib/mongoid/sharding.rb +51 -0
  208. data/lib/mongoid/state.rb +67 -0
  209. data/lib/mongoid/timestamps.rb +14 -0
  210. data/lib/mongoid/timestamps/created.rb +31 -0
  211. data/lib/mongoid/timestamps/updated.rb +33 -0
  212. data/lib/mongoid/validations.rb +124 -0
  213. data/lib/mongoid/validations/associated.rb +44 -0
  214. data/lib/mongoid/validations/referenced.rb +58 -0
  215. data/lib/mongoid/validations/uniqueness.rb +85 -0
  216. data/lib/mongoid/version.rb +4 -0
  217. data/lib/mongoid/versioning.rb +113 -0
  218. data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
  219. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +20 -0
  220. data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
  221. data/lib/rails/generators/mongoid/model/templates/model.rb +19 -0
  222. data/lib/rails/generators/mongoid/observer/observer_generator.rb +17 -0
  223. data/lib/rails/generators/mongoid/observer/templates/observer.rb +4 -0
  224. data/lib/rails/generators/mongoid_generator.rb +70 -0
  225. data/lib/rails/mongoid.rb +58 -0
  226. metadata +406 -0
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Persistence #:nodoc:
4
+ module Atomic #:nodoc:
5
+
6
+ # This is the superclass for all atomic operation objects.
7
+ class Operation
8
+ include Mongoid::Safe
9
+
10
+ attr_reader :document, :field, :value, :options
11
+
12
+ # Initialize the new pullAll operation.
13
+ #
14
+ # @example Create a new pullAll operation.
15
+ # PullAll.new(document, :aliases, [ "Bond" ])
16
+ #
17
+ # @param [ Document ] document The document to pullAll onto.
18
+ # @param [ Symbol ] field The name of the array field.
19
+ # @param [ Object ] value The value to pullAll.
20
+ # @param [ Hash ] options The persistence options.
21
+ #
22
+ # @since 2.0.0
23
+ def initialize(document, field, value, options = {})
24
+ @document, @field, @value = document, field, value
25
+ @options = { :safe => safe_mode?(options) }
26
+ end
27
+
28
+ # Get the atomic operation to perform.
29
+ #
30
+ # @example Get the operation.
31
+ # inc.operation
32
+ #
33
+ # @param [ String ] modifier The modifier to use.
34
+ #
35
+ # @return [ Hash ] The atomic operation for the field and addition.
36
+ #
37
+ # @since 2.0.0
38
+ def operation(modifier)
39
+ { modifier => { field => value } }
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Persistence #:nodoc:
4
+ module Atomic #:nodoc:
5
+
6
+ # This class provides the ability to perform an explicit $pullAll
7
+ # modification on a specific field.
8
+ class PullAll < Operation
9
+
10
+ # Sends the atomic $pullAll operation to the database.
11
+ #
12
+ # @example Persist the new values.
13
+ # pull_all.persist
14
+ #
15
+ # @return [ Object ] The new array value.
16
+ #
17
+ # @since 2.0.0
18
+ def persist
19
+ if document[field]
20
+ values = document.send(field)
21
+ values.delete_if { |val| value.include?(val) }
22
+ values.tap do
23
+ document.collection.update(document._selector, operation("$pullAll"), options)
24
+ document.changes.delete(field.to_s) if document.persisted?
25
+ end
26
+ else
27
+ return nil
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Persistence #:nodoc:
4
+ module Atomic #:nodoc:
5
+
6
+ # This class provides the ability to perform an explicit $push modification
7
+ # on a specific field.
8
+ class Push < Operation
9
+
10
+ # Sends the atomic $push operation to the database.
11
+ #
12
+ # @example Persist the new values.
13
+ # push.persist
14
+ #
15
+ # @return [ Object ] The new array value.
16
+ #
17
+ # @since 2.0.0
18
+ def persist
19
+ document[field] = [] unless document[field]
20
+ document.send(field).push(value).tap do |value|
21
+ document.collection.update(document._selector, operation("$push"), options)
22
+ document.changes.delete(field.to_s)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,71 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Persistence #:nodoc:
4
+ # Persistence commands extend from this class to get basic functionality on
5
+ # initialization.
6
+ class Command
7
+ include Mongoid::Safe
8
+
9
+ attr_reader \
10
+ :collection,
11
+ :document,
12
+ :klass,
13
+ :options,
14
+ :selector,
15
+ :suppress,
16
+ :validate
17
+
18
+ # Initialize the persistence +Command+.
19
+ #
20
+ # Options:
21
+ #
22
+ # document_or_class: The +Document+ or +Class+ to get the collection.
23
+ # options: Options like validation or safe mode.
24
+ # selector: Optional selector to use in query.
25
+ #
26
+ # Example:
27
+ #
28
+ # <tt>DeleteAll.new(Person, { :validate => true }, {})</tt>
29
+ def initialize(document_or_class, options = {}, selector = {})
30
+ init(document_or_class)
31
+ validate = options[:validate]
32
+ @suppress = options[:suppress]
33
+ @validate = (validate.nil? ? true : validate)
34
+ @selector = selector
35
+ @options = { :safe => safe_mode?(options) }
36
+ end
37
+
38
+ private
39
+
40
+ # Setup the proper instance variables based on if the supplied argument
41
+ # was a document object or a class object.
42
+ #
43
+ # Example:
44
+ #
45
+ # <tt>init(document_or_class)</tt>
46
+ #
47
+ # Options:
48
+ #
49
+ # document_or_class: A document or a class.
50
+ def init(document_or_class)
51
+ if document_or_class.is_a?(Mongoid::Document)
52
+ @document = document_or_class
53
+ @collection = @document.embedded? ? @document._root.collection : @document.collection
54
+ else
55
+ @klass = document_or_class
56
+ @collection = @klass.collection
57
+ end
58
+ end
59
+
60
+ # Should we suppress parent notifications?
61
+ #
62
+ # @example Suppress notifications?
63
+ # command.suppress?
64
+ #
65
+ # @return [ true, false ] Should the parent notifcations be suppressed.
66
+ def suppress?
67
+ !!@suppress
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Persistence #:nodoc:
4
+
5
+ # Insert is a persistence command responsible for taking a document that
6
+ # has not been saved to the database and saving it.
7
+ #
8
+ # The underlying query resembles the following MongoDB query:
9
+ #
10
+ # collection.insert(
11
+ # { "_id" : 1, "field" : "value" },
12
+ # false
13
+ # );
14
+ class Insert < Command
15
+
16
+ # Insert the new document in the database. This delegates to the standard
17
+ # MongoDB collection's insert command.
18
+ #
19
+ # Example:
20
+ #
21
+ # <tt>Insert.persist</tt>
22
+ #
23
+ # Returns:
24
+ #
25
+ # The +Document+, whether the insert succeeded or not.
26
+ def persist
27
+ return document if validate && document.invalid?(:create)
28
+ document.run_callbacks(:save) do
29
+ document.run_callbacks(:create) do
30
+ if insert
31
+ document.new_record = false
32
+ document._children.each { |child| child.new_record = false }
33
+ document.move_changes
34
+ end
35
+ end
36
+ end; document
37
+ end
38
+
39
+ protected
40
+ # Insert the document into the database.
41
+ def insert
42
+ if document.embedded?
43
+ Persistence::InsertEmbedded.new(
44
+ document,
45
+ options.merge(:validate => validate)
46
+ ).persist
47
+ else
48
+ collection.insert(document.as_document, options)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Persistence #:nodoc:
4
+
5
+ # Insert is a persistence command responsible for taking a document that
6
+ # has not been saved to the database and saving it. This specific class
7
+ # handles the case when the document is embedded in another.
8
+ #
9
+ # The underlying query resembles the following MongoDB query:
10
+ #
11
+ # collection.insert(
12
+ # { "_id" : 1, "field" : "value" },
13
+ # false
14
+ # );
15
+ class InsertEmbedded < Command
16
+
17
+ # Insert the new document in the database. If the document's parent is a
18
+ # new record, we will call save on the parent, otherwise we will $push
19
+ # the document onto the parent.
20
+ #
21
+ # Example:
22
+ #
23
+ # <tt>Insert.persist</tt>
24
+ #
25
+ # Returns:
26
+ #
27
+ # The +Document+, whether the insert succeeded or not.
28
+ def persist
29
+ return document if validate && document.invalid?(:create)
30
+ parent = document._parent
31
+ if parent.new_record?
32
+ parent.insert
33
+ else
34
+ update = { document._inserter => { document._position => document.as_document } }
35
+ collection.update(parent._selector, update, options.merge(:multi => false))
36
+ document.new_record = false
37
+ document.move_changes
38
+ end
39
+ document
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Persistence #:nodoc:
4
+
5
+ # Remove is a persistence command responsible for deleting a document from
6
+ # the database.
7
+ #
8
+ # The underlying query resembles the following MongoDB query:
9
+ #
10
+ # collection.remove(
11
+ # { "_id" : 1 },
12
+ # false
13
+ # );
14
+ class Remove < Command
15
+
16
+ # Remove the document from the database: delegates to the MongoDB
17
+ # collection remove method.
18
+ #
19
+ # Example:
20
+ #
21
+ # <tt>Remove.persist</tt>
22
+ #
23
+ # Returns:
24
+ #
25
+ # +true+ if success, +false+ if not.
26
+ def persist
27
+ remove
28
+ end
29
+
30
+ protected
31
+ # Remove the document from the database.
32
+ def remove
33
+ if document.embedded?
34
+ Persistence::RemoveEmbedded.new(
35
+ document,
36
+ options.merge(:validate => validate, :suppress => suppress)
37
+ ).persist
38
+ else
39
+ collection.remove({ :_id => document.id }, options)
40
+ end; true
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Persistence #:nodoc:
4
+
5
+ # Remove is a persistence command responsible for deleting a document from
6
+ # the database.
7
+ #
8
+ # The underlying query resembles the following MongoDB query:
9
+ #
10
+ # collection.remove(
11
+ # { "field" : value },
12
+ # false
13
+ # );
14
+ class RemoveAll < Command
15
+
16
+ # Remove the document from the database: delegates to the MongoDB
17
+ # collection remove method.
18
+ #
19
+ # Example:
20
+ #
21
+ # <tt>Remove.persist</tt>
22
+ #
23
+ # Returns:
24
+ #
25
+ # +true+ if success, +false+ if not.
26
+ def persist
27
+ remove
28
+ end
29
+
30
+ protected
31
+ # Remove the document from the database.
32
+ def remove
33
+ select = (klass.hereditary? ? selector.merge(:_type => klass.name) : selector)
34
+ collection.find(select).count.tap do
35
+ collection.remove(select, options)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Persistence #:nodoc:
4
+
5
+ # Remove is a persistence command responsible for deleting a document from
6
+ # the database.
7
+ #
8
+ # The underlying query resembles the following MongoDB query:
9
+ #
10
+ # collection.remove(
11
+ # { "_id" : 1 },
12
+ # false
13
+ # );
14
+ class RemoveEmbedded < Command
15
+
16
+ # Remove the document from the database. If the parent is a new record,
17
+ # it will get removed in Ruby only. If the parent is not a new record
18
+ # then either an $unset or $set will occur, depending if it's an
19
+ # embeds_one or embeds_many.
20
+ #
21
+ # Example:
22
+ #
23
+ # <tt>RemoveEmbedded.persist</tt>
24
+ #
25
+ # Returns:
26
+ #
27
+ # +true+ or +false+, depending on if the removal passed.
28
+ def persist
29
+ parent = document._parent
30
+ parent.remove_child(document) unless suppress?
31
+ unless parent.new_record?
32
+ update = { document._remover => removal_selector }
33
+ collection.update(parent._selector, update, options.merge(:multi => false))
34
+ end; true
35
+ end
36
+
37
+ protected
38
+ # Get the value to pass to the removal modifier.
39
+ def setter
40
+ document._index ? document.id : true
41
+ end
42
+
43
+ def removal_selector
44
+ document._index ? { document._pull => { "_id" => document.id } } : { document._path => setter }
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,77 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Persistence #:nodoc:
4
+
5
+ # Update is a persistence command responsible for taking a document that
6
+ # has already been saved to the database and saving it, depending on
7
+ # whether or not the document has been modified.
8
+ #
9
+ # Before persisting the command will check via dirty attributes if the
10
+ # document has changed, if not, it will simply return true. If it has it
11
+ # will go through the validation steps, run callbacks, and set the changed
12
+ # fields atomically on the document. The underlying query resembles the
13
+ # following MongoDB query:
14
+ #
15
+ # collection.update(
16
+ # { "_id" : 1,
17
+ # { "$set" : { "field" : "value" },
18
+ # false,
19
+ # false
20
+ # );
21
+ #
22
+ # For embedded documents it will use the positional locator:
23
+ #
24
+ # collection.update(
25
+ # { "_id" : 1, "addresses._id" : 2 },
26
+ # { "$set" : { "addresses.$.field" : "value" },
27
+ # false,
28
+ # false
29
+ # );
30
+ #
31
+ class Update < Command
32
+
33
+ # Persist the document that is to be updated to the database. This will
34
+ # only write changed fields via MongoDB's $set modifier operation.
35
+ #
36
+ # Example:
37
+ #
38
+ # <tt>Update.persist</tt>
39
+ #
40
+ # Returns:
41
+ #
42
+ # +true+ or +false+, depending on validation.
43
+ def persist
44
+ return false if validate && document.invalid?(:update)
45
+ document.run_callbacks(:save) do
46
+ document.run_callbacks(:update) do
47
+ if update
48
+ document.move_changes
49
+ document._children.each do |child|
50
+ child.move_changes
51
+ child.new_record = false if child.new_record?
52
+ end
53
+ true
54
+ else
55
+ return false
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ protected
62
+ # Update the document in the database atomically.
63
+ def update
64
+ updates = document._updates
65
+ unless updates.empty?
66
+ other_pushes = updates.delete(:other)
67
+ collection.update(document._selector, updates, options.merge(:multi => false))
68
+ collection.update(
69
+ document._selector,
70
+ { "$pushAll" => other_pushes },
71
+ options.merge(:multi => false)
72
+ ) if other_pushes
73
+ end; true
74
+ end
75
+ end
76
+ end
77
+ end