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,284 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+
4
+ # This is the base module for all domain objects that need to be persisted to
5
+ # the database as documents.
6
+ module Document
7
+ extend ActiveSupport::Concern
8
+ include Mongoid::Components
9
+ include Mongoid::MultiDatabase
10
+
11
+ included do
12
+ attr_reader :new_record
13
+ end
14
+
15
+ # Default comparison is via the string version of the id.
16
+ #
17
+ # @example Compare two documents.
18
+ # person <=> other_person
19
+ #
20
+ # @param [ Document ] other The document to compare with.
21
+ #
22
+ # @return [ Integer ] -1, 0, 1.
23
+ def <=>(other)
24
+ attributes["_id"].to_s <=> other.attributes["_id"].to_s
25
+ end
26
+
27
+ # Performs equality checking on the document ids. For more robust
28
+ # equality checking please override this method.
29
+ #
30
+ # @example Compare for equality.
31
+ # document == other
32
+ #
33
+ # @param [ Document, Object ] other The other object to compare with.
34
+ #
35
+ # @return [ true, false ] True if the ids are equal, false if not.
36
+ def ==(other)
37
+ self.class == other.class &&
38
+ attributes["_id"] == other.attributes["_id"]
39
+ end
40
+
41
+ # Performs class equality checking.
42
+ #
43
+ # @example Compare the classes.
44
+ # document === other
45
+ #
46
+ # @param [ Document, Object ] other The other object to compare with.
47
+ #
48
+ # @return [ true, false ] True if the classes are equal, false if not.
49
+ def ===(other)
50
+ self.class == other.class
51
+ end
52
+
53
+ # Delegates to ==. Used when needing checks in hashes.
54
+ #
55
+ # @example Perform equality checking.
56
+ # document.eql?(other)
57
+ #
58
+ # @param [ Document, Object ] other The object to check against.
59
+ #
60
+ # @return [ true, false ] True if equal, false if not.
61
+ def eql?(other)
62
+ self == (other)
63
+ end
64
+
65
+ # Freezes the internal attributes of the document.
66
+ #
67
+ # @example Freeze the document
68
+ # document.freeze
69
+ #
70
+ # @return [ Document ] The document.
71
+ #
72
+ # @since 2.0.0
73
+ def freeze
74
+ attributes.freeze
75
+ self
76
+ end
77
+
78
+ # Checks if the document is frozen
79
+ #
80
+ # @example Check if frozen
81
+ # document.frozen?
82
+ #
83
+ # @return [ true, false ] True if frozen, else false.
84
+ #
85
+ # @since 2.0.0
86
+ def frozen?
87
+ raw_attributes.frozen?
88
+ end
89
+
90
+ # Delegates to id in order to allow two records of the same type and id to
91
+ # work with something like:
92
+ #
93
+ # [ Person.find(1), Person.find(2), Person.find(3) ] &
94
+ # [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
95
+ #
96
+ # @example Get the hash.
97
+ # document.hash
98
+ #
99
+ # @return [ Integer ] The hash of the document's id.
100
+ def hash
101
+ raw_attributes["_id"].hash
102
+ end
103
+
104
+ # Generate an id for this +Document+.
105
+ #
106
+ # @example Create the id.
107
+ # person.identify
108
+ #
109
+ # @return [ BSON::ObjectId, String ] A newly created id.
110
+ def identify
111
+ Identity.new(self).create
112
+ end
113
+
114
+ # Instantiate a new +Document+, setting the Document's attributes if
115
+ # given. If no attributes are provided, they will be initialized with
116
+ # an empty +Hash+.
117
+ #
118
+ # If a primary key is defined, the document's id will be set to that key,
119
+ # otherwise it will be set to a fresh +BSON::ObjectId+ string.
120
+ #
121
+ # @example Create a new document.
122
+ # Person.new(:title => "Sir")
123
+ #
124
+ # @param [ Hash ] attrs The attributes to set up the document with.
125
+ #
126
+ # @return [ Document ] A new document.
127
+ def initialize(attrs = nil)
128
+ @new_record = true
129
+ @attributes = apply_default_attributes
130
+ process(attrs) do
131
+ yield self if block_given?
132
+ identify
133
+ end
134
+ run_callbacks(:initialize) { self }
135
+ end
136
+
137
+ # Reloads the +Document+ attributes from the database. If the document has
138
+ # not been saved then an error will get raised if the configuration option
139
+ # was set.
140
+ #
141
+ # @example Reload the document.
142
+ # person.reload
143
+ #
144
+ # @raise [ Errors::DocumentNotFound ] If the document was deleted.
145
+ #
146
+ # @return [ Document ] The document, reloaded.
147
+ def reload
148
+ reloaded = collection.find_one(:_id => id)
149
+ if Mongoid.raise_not_found_error
150
+ raise Errors::DocumentNotFound.new(self.class, id) if reloaded.nil?
151
+ end
152
+ @attributes = {}.merge(reloaded || {})
153
+ apply_default_attributes
154
+ reset_modifications
155
+ tap do
156
+ relations.keys.each do |name|
157
+ if instance_variable_defined?("@#{name}")
158
+ remove_instance_variable("@#{name}")
159
+ end
160
+ end
161
+ end
162
+ end
163
+
164
+ # Remove a child document from this parent. If an embeds one then set to
165
+ # nil, otherwise remove from the embeds many.
166
+ #
167
+ # This is called from the +RemoveEmbedded+ persistence command.
168
+ #
169
+ # @example Remove the child.
170
+ # document.remove_child(child)
171
+ #
172
+ # @param [ Document ] child The child (embedded) document to remove.
173
+ def remove_child(child)
174
+ name = child.metadata.name
175
+ if child.embedded_one?
176
+ remove_instance_variable("@#{name}") if instance_variable_defined?("@#{name}")
177
+ else
178
+ send(name).delete(child)
179
+ end
180
+ end
181
+
182
+ # Return an array with this +Document+ only in it.
183
+ #
184
+ # @example Return the document in an array.
185
+ # document.to_a
186
+ #
187
+ # @return [ Array<Document> ] An array with the document as its only item.
188
+ def to_a
189
+ [ self ]
190
+ end
191
+
192
+ # Return a hash of the entire document hierarchy from this document and
193
+ # below. Used when the attributes are needed for everything and not just
194
+ # the current document.
195
+ #
196
+ # @example Get the full hierarchy.
197
+ # person.as_document
198
+ #
199
+ # @return [ Hash ] A hash of all attributes in the hierarchy.
200
+ def as_document
201
+ attribs = attributes
202
+ attribs.tap do |attrs|
203
+ relations.select { |name, meta| meta.embedded? }.each do |name, meta|
204
+ relation = send(name, false, :continue => false)
205
+ attrs[name] = relation.as_document unless relation.blank?
206
+ end
207
+ end
208
+ end
209
+
210
+ # Returns an instance of the specified class with the attributes
211
+ # and errors of the current document.
212
+ #
213
+ # @example Return a subclass document as a superclass instance.
214
+ # manager.becomes(Person)
215
+ #
216
+ # @raise [ ArgumentError ] If the class doesn't include Mongoid::Document
217
+ #
218
+ # @param [ Class ] klass The class to become.
219
+ #
220
+ # @return [ Document ] An instance of the specified class.
221
+ def becomes(klass)
222
+ unless klass.include?(Mongoid::Document)
223
+ raise ArgumentError, 'A class which includes Mongoid::Document is expected'
224
+ end
225
+ klass.new.tap do |became|
226
+ became.instance_variable_set('@attributes', @attributes)
227
+ became.instance_variable_set('@errors', @errors)
228
+ became.instance_variable_set('@new_record', new_record?)
229
+ became.instance_variable_set('@destroyed', destroyed?)
230
+ end
231
+ end
232
+
233
+ module ClassMethods #:nodoc:
234
+
235
+ # Performs class equality checking.
236
+ #
237
+ # @example Compare the classes.
238
+ # document === other
239
+ #
240
+ # @param [ Document, Object ] other The other object to compare with.
241
+ #
242
+ # @return [ true, false ] True if the classes are equal, false if not.
243
+ #
244
+ # @since 2.0.0.rc.4
245
+ def ===(other)
246
+ self == (other.is_a?(Class) ? other : other.class)
247
+ end
248
+
249
+ # Instantiate a new object, only when loaded from the database or when
250
+ # the attributes have already been typecast.
251
+ #
252
+ # @example Create the document.
253
+ # Person.instantiate(:title => "Sir", :age => 30)
254
+ #
255
+ # @param [ Hash ] attrs The hash of attributes to instantiate with.
256
+ #
257
+ # @return [ Document ] A new document.
258
+ def instantiate(attrs = nil)
259
+ attributes = attrs || {}
260
+ allocate.tap do |doc|
261
+ doc.instance_variable_set(:@attributes, attributes)
262
+ doc.send(:apply_default_attributes)
263
+ doc.setup_modifications
264
+ doc.run_callbacks(:initialize) { doc }
265
+ end
266
+ end
267
+
268
+ # Returns all types to query for when using this class as the base.
269
+ #
270
+ # @example Get the types.
271
+ # document._types
272
+ #
273
+ # @return [ Array<Class> ] All subclasses of the current document.
274
+ def _types
275
+ @_type ||= [descendants + [self]].flatten.uniq.map(&:to_s)
276
+ end
277
+
278
+ # Set the i18n scope to overwrite ActiveModel.
279
+ def i18n_scope
280
+ :mongoid
281
+ end
282
+ end
283
+ end
284
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+ require "mongoid/errors/mongoid_error"
3
+ require "mongoid/errors/document_not_found"
4
+ require "mongoid/errors/invalid_collection"
5
+ require "mongoid/errors/invalid_database"
6
+ require "mongoid/errors/invalid_field"
7
+ require "mongoid/errors/invalid_options"
8
+ require "mongoid/errors/invalid_type"
9
+ require "mongoid/errors/mixed_relations"
10
+ require "mongoid/errors/too_many_nested_attribute_records"
11
+ require "mongoid/errors/unsaved_document"
12
+ require "mongoid/errors/unsupported_version"
13
+ require "mongoid/errors/validations"
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Errors #:nodoc
4
+
5
+ # Raised when querying the database for a document by a specific id which
6
+ # does not exist. If multiple ids were passed then it will display all of
7
+ # those.
8
+ #
9
+ # Example:
10
+ #
11
+ # <tt>DocumentNotFound.new(Person, ["1", "2"])</tt>
12
+ class DocumentNotFound < MongoidError
13
+
14
+ attr_reader :klass, :identifiers
15
+
16
+ def initialize(klass, ids)
17
+ @klass = klass
18
+ @identifiers = ids.is_a?(Array) ? ids.join(", ") : ids
19
+
20
+ super(
21
+ translate(
22
+ "document_not_found",
23
+ { :klass => klass.name, :identifiers => @identifiers }
24
+ )
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Errors #:nodoc
4
+
5
+ # This error is raised when trying to access a Mongo::Collection from an
6
+ # embedded document.
7
+ #
8
+ # Example:
9
+ #
10
+ # <tt>InvalidCollection.new(Address)</tt>
11
+ class InvalidCollection < MongoidError
12
+ def initialize(klass)
13
+ super(
14
+ translate("invalid_collection", { :klass => klass.name })
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Errors #:nodoc
4
+
5
+ # Raised when the database connection has not been set up properly, either
6
+ # by attempting to set an object on the db that is not a +Mongo::DB+, or
7
+ # not setting anything at all.
8
+ #
9
+ # Example:
10
+ #
11
+ # <tt>InvalidDatabase.new("Not a DB")</tt>
12
+ class InvalidDatabase < MongoidError
13
+ def initialize(database)
14
+ super(
15
+ translate("invalid_database", { :name => database.class.name })
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Errors #:nodoc
4
+
5
+ # This error is raised when trying to create a field that conflicts with
6
+ # a Mongoid internal attribute or method.
7
+ #
8
+ # Example:
9
+ #
10
+ # <tt>InvalidField.new('collection')</tt>
11
+ class InvalidField < MongoidError
12
+ def initialize(name)
13
+ super(
14
+ translate("invalid_field", { :name => name })
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Errors #:nodoc
4
+
5
+ # Raised when invalid options are passed into a constructor or method.
6
+ #
7
+ # Example:
8
+ #
9
+ # <tt>InvalidOptions.new</tt>
10
+ class InvalidOptions < MongoidError
11
+ def initialize(key, options)
12
+ super(translate(key, options))
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Errors #:nodoc
4
+
5
+ # Raised when trying to get or set a value for a defined field, where the
6
+ # type of the object does not match the defined field type.
7
+ #
8
+ # Example:
9
+ #
10
+ # <tt>InvalidType.new(Array, "Not an Array")</tt>
11
+ class InvalidType < MongoidError
12
+ def initialize(klass, value)
13
+ super(
14
+ translate(
15
+ "invalid_type",
16
+ {
17
+ :klass => klass.name,
18
+ :other => value.class.name,
19
+ :value => value.inspect
20
+ }
21
+ )
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Errors #:nodoc
4
+
5
+ # This error is raised when trying to reference an embedded document from
6
+ # a document in another collection that is not it's parent.
7
+ #
8
+ # @example An illegal reference to an embedded document.
9
+ # class Post
10
+ # include Mongoid::Document
11
+ # references_many :addresses
12
+ # end
13
+ #
14
+ # class Address
15
+ # include Mongoid::Document
16
+ # embedded_in :person
17
+ # referenced_in :post
18
+ # end
19
+ #
20
+ # @since 2.0.0
21
+ class MixedRelations < MongoidError
22
+
23
+ attr_reader :root_klass, :embedded_klass
24
+
25
+ def initialize(root_klass, embedded_klass)
26
+ @root_klass, @embedded_klass = root_klass, embedded_klass
27
+
28
+ super(
29
+ translate(
30
+ "mixed_relations",
31
+ { :root => root_klass, :embedded => embedded_klass }
32
+ )
33
+ )
34
+ end
35
+ end
36
+ end
37
+ end