stonegao-mongoid 2.0.0.rc.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.
- data/LICENSE +20 -0
- data/README.rdoc +50 -0
- data/Rakefile +51 -0
- data/lib/config/locales/bg.yml +44 -0
- data/lib/config/locales/de.yml +44 -0
- data/lib/config/locales/en.yml +45 -0
- data/lib/config/locales/es.yml +44 -0
- data/lib/config/locales/fr.yml +45 -0
- data/lib/config/locales/hu.yml +47 -0
- data/lib/config/locales/it.yml +42 -0
- data/lib/config/locales/kr.yml +68 -0
- data/lib/config/locales/nl.yml +42 -0
- data/lib/config/locales/pl.yml +42 -0
- data/lib/config/locales/pt-br.yml +43 -0
- data/lib/config/locales/pt.yml +43 -0
- data/lib/config/locales/ro.yml +49 -0
- data/lib/config/locales/sv.yml +43 -0
- data/lib/config/locales/zh-CN.yml +34 -0
- data/lib/mongoid/atomicity.rb +111 -0
- data/lib/mongoid/attributes.rb +251 -0
- data/lib/mongoid/callbacks.rb +13 -0
- data/lib/mongoid/collection.rb +137 -0
- data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
- data/lib/mongoid/collections/master.rb +29 -0
- data/lib/mongoid/collections/operations.rb +42 -0
- data/lib/mongoid/collections/slaves.rb +45 -0
- data/lib/mongoid/collections.rb +70 -0
- data/lib/mongoid/components.rb +45 -0
- data/lib/mongoid/config/database.rb +167 -0
- data/lib/mongoid/config/replset_database.rb +48 -0
- data/lib/mongoid/config.rb +343 -0
- data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
- data/lib/mongoid/contexts/enumerable.rb +226 -0
- data/lib/mongoid/contexts/ids.rb +25 -0
- data/lib/mongoid/contexts/mongo.rb +345 -0
- data/lib/mongoid/contexts/paging.rb +50 -0
- data/lib/mongoid/contexts.rb +21 -0
- data/lib/mongoid/copyable.rb +44 -0
- data/lib/mongoid/criteria.rb +325 -0
- data/lib/mongoid/criterion/complex.rb +34 -0
- data/lib/mongoid/criterion/creational.rb +34 -0
- data/lib/mongoid/criterion/exclusion.rb +67 -0
- data/lib/mongoid/criterion/inclusion.rb +134 -0
- data/lib/mongoid/criterion/inspection.rb +20 -0
- data/lib/mongoid/criterion/optional.rb +213 -0
- data/lib/mongoid/criterion/selector.rb +74 -0
- data/lib/mongoid/cursor.rb +81 -0
- data/lib/mongoid/default_scope.rb +28 -0
- data/lib/mongoid/dirty.rb +251 -0
- data/lib/mongoid/document.rb +256 -0
- data/lib/mongoid/errors/document_not_found.rb +29 -0
- data/lib/mongoid/errors/invalid_collection.rb +19 -0
- data/lib/mongoid/errors/invalid_database.rb +20 -0
- data/lib/mongoid/errors/invalid_field.rb +19 -0
- data/lib/mongoid/errors/invalid_options.rb +16 -0
- data/lib/mongoid/errors/invalid_type.rb +26 -0
- data/lib/mongoid/errors/mongoid_error.rb +27 -0
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +21 -0
- data/lib/mongoid/errors/unsaved_document.rb +23 -0
- data/lib/mongoid/errors/unsupported_version.rb +21 -0
- data/lib/mongoid/errors/validations.rb +24 -0
- data/lib/mongoid/errors.rb +12 -0
- data/lib/mongoid/extensions/array/conversions.rb +23 -0
- data/lib/mongoid/extensions/array/parentization.rb +13 -0
- data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
- data/lib/mongoid/extensions/binary/conversions.rb +17 -0
- data/lib/mongoid/extensions/boolean/conversions.rb +27 -0
- data/lib/mongoid/extensions/date/conversions.rb +25 -0
- data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
- data/lib/mongoid/extensions/false_class/equality.rb +13 -0
- data/lib/mongoid/extensions/float/conversions.rb +20 -0
- data/lib/mongoid/extensions/hash/conversions.rb +19 -0
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +22 -0
- data/lib/mongoid/extensions/hash/scoping.rb +12 -0
- data/lib/mongoid/extensions/integer/conversions.rb +20 -0
- data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
- data/lib/mongoid/extensions/object/conversions.rb +25 -0
- data/lib/mongoid/extensions/object/reflections.rb +17 -0
- data/lib/mongoid/extensions/object/yoda.rb +27 -0
- data/lib/mongoid/extensions/object_id/conversions.rb +57 -0
- data/lib/mongoid/extensions/proc/scoping.rb +12 -0
- data/lib/mongoid/extensions/set/conversions.rb +20 -0
- data/lib/mongoid/extensions/string/conversions.rb +34 -0
- data/lib/mongoid/extensions/string/inflections.rb +97 -0
- data/lib/mongoid/extensions/symbol/conversions.rb +21 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
- data/lib/mongoid/extensions/time_conversions.rb +38 -0
- data/lib/mongoid/extensions/true_class/equality.rb +13 -0
- data/lib/mongoid/extensions.rb +116 -0
- data/lib/mongoid/extras.rb +61 -0
- data/lib/mongoid/factory.rb +20 -0
- data/lib/mongoid/field.rb +95 -0
- data/lib/mongoid/fields.rb +138 -0
- data/lib/mongoid/finders.rb +173 -0
- data/lib/mongoid/hierarchy.rb +85 -0
- data/lib/mongoid/identity.rb +89 -0
- data/lib/mongoid/indexes.rb +38 -0
- data/lib/mongoid/inspection.rb +58 -0
- data/lib/mongoid/javascript/functions.yml +37 -0
- data/lib/mongoid/javascript.rb +21 -0
- data/lib/mongoid/json.rb +16 -0
- data/lib/mongoid/keys.rb +77 -0
- data/lib/mongoid/logger.rb +18 -0
- data/lib/mongoid/matchers/all.rb +11 -0
- data/lib/mongoid/matchers/default.rb +27 -0
- data/lib/mongoid/matchers/exists.rb +13 -0
- data/lib/mongoid/matchers/gt.rb +11 -0
- data/lib/mongoid/matchers/gte.rb +11 -0
- data/lib/mongoid/matchers/in.rb +11 -0
- data/lib/mongoid/matchers/lt.rb +11 -0
- data/lib/mongoid/matchers/lte.rb +11 -0
- data/lib/mongoid/matchers/ne.rb +11 -0
- data/lib/mongoid/matchers/nin.rb +11 -0
- data/lib/mongoid/matchers/size.rb +11 -0
- data/lib/mongoid/matchers.rb +55 -0
- data/lib/mongoid/modifiers/command.rb +18 -0
- data/lib/mongoid/modifiers/inc.rb +24 -0
- data/lib/mongoid/modifiers.rb +24 -0
- data/lib/mongoid/multi_database.rb +11 -0
- data/lib/mongoid/multi_parameter_attributes.rb +80 -0
- data/lib/mongoid/named_scope.rb +36 -0
- data/lib/mongoid/nested_attributes.rb +43 -0
- data/lib/mongoid/paranoia.rb +103 -0
- data/lib/mongoid/paths.rb +61 -0
- data/lib/mongoid/persistence/command.rb +59 -0
- data/lib/mongoid/persistence/insert.rb +53 -0
- data/lib/mongoid/persistence/insert_embedded.rb +42 -0
- data/lib/mongoid/persistence/remove.rb +44 -0
- data/lib/mongoid/persistence/remove_all.rb +40 -0
- data/lib/mongoid/persistence/remove_embedded.rb +48 -0
- data/lib/mongoid/persistence/update.rb +76 -0
- data/lib/mongoid/persistence.rb +237 -0
- data/lib/mongoid/railtie.rb +129 -0
- data/lib/mongoid/railties/database.rake +171 -0
- data/lib/mongoid/railties/document.rb +12 -0
- data/lib/mongoid/relations/accessors.rb +157 -0
- data/lib/mongoid/relations/auto_save.rb +34 -0
- data/lib/mongoid/relations/binding.rb +26 -0
- data/lib/mongoid/relations/bindings/embedded/in.rb +82 -0
- data/lib/mongoid/relations/bindings/embedded/many.rb +98 -0
- data/lib/mongoid/relations/bindings/embedded/one.rb +66 -0
- data/lib/mongoid/relations/bindings/referenced/in.rb +74 -0
- data/lib/mongoid/relations/bindings/referenced/many.rb +96 -0
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +99 -0
- data/lib/mongoid/relations/bindings/referenced/one.rb +66 -0
- data/lib/mongoid/relations/bindings.rb +9 -0
- data/lib/mongoid/relations/builder.rb +42 -0
- data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
- data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
- data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +116 -0
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
- data/lib/mongoid/relations/builders/referenced/in.rb +32 -0
- data/lib/mongoid/relations/builders/referenced/many.rb +26 -0
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
- data/lib/mongoid/relations/builders/referenced/one.rb +30 -0
- data/lib/mongoid/relations/builders.rb +79 -0
- data/lib/mongoid/relations/cascading/delete.rb +19 -0
- data/lib/mongoid/relations/cascading/destroy.rb +19 -0
- data/lib/mongoid/relations/cascading/nullify.rb +18 -0
- data/lib/mongoid/relations/cascading/strategy.rb +26 -0
- data/lib/mongoid/relations/cascading.rb +55 -0
- data/lib/mongoid/relations/constraint.rb +45 -0
- data/lib/mongoid/relations/cyclic.rb +97 -0
- data/lib/mongoid/relations/embedded/in.rb +173 -0
- data/lib/mongoid/relations/embedded/many.rb +483 -0
- data/lib/mongoid/relations/embedded/one.rb +170 -0
- data/lib/mongoid/relations/macros.rb +306 -0
- data/lib/mongoid/relations/many.rb +171 -0
- data/lib/mongoid/relations/metadata.rb +533 -0
- data/lib/mongoid/relations/nested_builder.rb +68 -0
- data/lib/mongoid/relations/one.rb +47 -0
- data/lib/mongoid/relations/polymorphic.rb +54 -0
- data/lib/mongoid/relations/proxy.rb +128 -0
- data/lib/mongoid/relations/referenced/in.rb +216 -0
- data/lib/mongoid/relations/referenced/many.rb +443 -0
- data/lib/mongoid/relations/referenced/many_to_many.rb +344 -0
- data/lib/mongoid/relations/referenced/one.rb +206 -0
- data/lib/mongoid/relations/reflections.rb +45 -0
- data/lib/mongoid/relations.rb +105 -0
- data/lib/mongoid/safe.rb +23 -0
- data/lib/mongoid/safety.rb +207 -0
- data/lib/mongoid/scope.rb +31 -0
- data/lib/mongoid/serialization.rb +99 -0
- data/lib/mongoid/state.rb +66 -0
- data/lib/mongoid/timestamps.rb +38 -0
- data/lib/mongoid/validations/associated.rb +42 -0
- data/lib/mongoid/validations/uniqueness.rb +85 -0
- data/lib/mongoid/validations.rb +117 -0
- data/lib/mongoid/version.rb +4 -0
- data/lib/mongoid/versioning.rb +51 -0
- data/lib/mongoid.rb +139 -0
- data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +23 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
- data/lib/rails/generators/mongoid/model/templates/model.rb +17 -0
- data/lib/rails/generators/mongoid_generator.rb +61 -0
- data/lib/rails/mongoid.rb +57 -0
- metadata +380 -0
|
@@ -0,0 +1,256 @@
|
|
|
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
|
+
id.to_s <=> other.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
|
+
return false unless other.is_a?(Document)
|
|
38
|
+
id == other.id || equal?(other)
|
|
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
|
+
# Delegates to id in order to allow two records of the same type and id to
|
|
66
|
+
# work with something like:
|
|
67
|
+
#
|
|
68
|
+
# [ Person.find(1), Person.find(2), Person.find(3) ] &
|
|
69
|
+
# [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
|
|
70
|
+
#
|
|
71
|
+
# @example Get the hash.
|
|
72
|
+
# document.hash
|
|
73
|
+
#
|
|
74
|
+
# @return [ Integer ] The hash of the document's id.
|
|
75
|
+
def hash
|
|
76
|
+
id.hash
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Return the attributes hash with indifferent access. Used mostly for
|
|
80
|
+
# convenience - use +Document#raw_attributes+ where you dont care if the
|
|
81
|
+
# keys are all strings.
|
|
82
|
+
#
|
|
83
|
+
# @example Get the attributes.
|
|
84
|
+
# person.attributes
|
|
85
|
+
#
|
|
86
|
+
# @return [ HashWithIndifferentAccess ] The attributes.
|
|
87
|
+
def attributes
|
|
88
|
+
@attributes.with_indifferent_access
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Generate an id for this +Document+.
|
|
92
|
+
#
|
|
93
|
+
# @example Create the id.
|
|
94
|
+
# person.identify
|
|
95
|
+
#
|
|
96
|
+
# @return [ BSON::ObjectId, String ] A newly created id.
|
|
97
|
+
def identify
|
|
98
|
+
Identity.new(self).create
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Instantiate a new +Document+, setting the Document's attributes if
|
|
102
|
+
# given. If no attributes are provided, they will be initialized with
|
|
103
|
+
# an empty +Hash+.
|
|
104
|
+
#
|
|
105
|
+
# If a primary key is defined, the document's id will be set to that key,
|
|
106
|
+
# otherwise it will be set to a fresh +BSON::ObjectId+ string.
|
|
107
|
+
#
|
|
108
|
+
# @example Create a new document.
|
|
109
|
+
# Person.new(:title => "Sir")
|
|
110
|
+
#
|
|
111
|
+
# @param [ Hash ] attrs The attributes to set up the document with.
|
|
112
|
+
#
|
|
113
|
+
# @return [ Document ] A new document.
|
|
114
|
+
def initialize(attrs = nil)
|
|
115
|
+
@new_record = true
|
|
116
|
+
@attributes = default_attributes
|
|
117
|
+
# @todo: Durran: We know the document is polymorphic here but we have no
|
|
118
|
+
# metadata... Can we pass the document to the field setter and get the
|
|
119
|
+
# foreign key setter to set the type?
|
|
120
|
+
process(attrs) do |document|
|
|
121
|
+
yield self if block_given?
|
|
122
|
+
identify
|
|
123
|
+
run_callbacks(:initialize) { document }
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Return the attributes hash.
|
|
128
|
+
#
|
|
129
|
+
# @example Get the untouched attributes.
|
|
130
|
+
# person.raw_attributes
|
|
131
|
+
#
|
|
132
|
+
# @return [ Hash ] This document's attributes.
|
|
133
|
+
def raw_attributes
|
|
134
|
+
@attributes
|
|
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
|
+
tap do
|
|
154
|
+
relations.keys.each do |name|
|
|
155
|
+
if relation_exists?(name)
|
|
156
|
+
remove_instance_variable("@#{name}")
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Remove a child document from this parent. If an embeds one then set to
|
|
163
|
+
# nil, otherwise remove from the embeds many.
|
|
164
|
+
#
|
|
165
|
+
# This is called from the +RemoveEmbedded+ persistence command.
|
|
166
|
+
#
|
|
167
|
+
# @example Remove the child.
|
|
168
|
+
# document.remove_child(child)
|
|
169
|
+
#
|
|
170
|
+
# @param [ Document ] child The child (embedded) document to remove.
|
|
171
|
+
def remove_child(child)
|
|
172
|
+
name = child.metadata.name
|
|
173
|
+
if child.embedded_one?
|
|
174
|
+
remove_instance_variable("@#{name}") if instance_variable_defined?("@#{name}")
|
|
175
|
+
else
|
|
176
|
+
send(name).delete(child)
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Return an array with this +Document+ only in it.
|
|
181
|
+
#
|
|
182
|
+
# @example Return the document in an array.
|
|
183
|
+
# document.to_a
|
|
184
|
+
#
|
|
185
|
+
# @return [ Array<Document> ] An array with the document as its only item.
|
|
186
|
+
def to_a
|
|
187
|
+
[ self ]
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# Return a hash of the entire document hierarchy from this document and
|
|
191
|
+
# below. Used when the attributes are needed for everything and not just
|
|
192
|
+
# the current document.
|
|
193
|
+
#
|
|
194
|
+
# @example Get the full hierarchy.
|
|
195
|
+
# person.to_hash
|
|
196
|
+
#
|
|
197
|
+
# @return [ Hash ] A hash of all attributes in the hierarchy.
|
|
198
|
+
def to_hash
|
|
199
|
+
attributes = @attributes
|
|
200
|
+
attributes.tap do |attrs|
|
|
201
|
+
relations.select { |name, meta| meta.embedded? }.each do |name, meta|
|
|
202
|
+
relation = send(name, false, :continue => false)
|
|
203
|
+
attrs[name] = relation.to_hash unless relation.blank?
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
module ClassMethods #:nodoc:
|
|
209
|
+
|
|
210
|
+
# Performs class equality checking.
|
|
211
|
+
#
|
|
212
|
+
# @example Compare the classes.
|
|
213
|
+
# document === other
|
|
214
|
+
#
|
|
215
|
+
# @param [ Document, Object ] other The other object to compare with.
|
|
216
|
+
#
|
|
217
|
+
# @return [ true, false ] True if the classes are equal, false if not.
|
|
218
|
+
#
|
|
219
|
+
# @since 2.0.0.rc.4
|
|
220
|
+
def ===(other)
|
|
221
|
+
self == (other.is_a?(Class) ? other : other.class)
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# Instantiate a new object, only when loaded from the database or when
|
|
225
|
+
# the attributes have already been typecast.
|
|
226
|
+
#
|
|
227
|
+
# @example Create the document.
|
|
228
|
+
# Person.instantiate(:title => "Sir", :age => 30)
|
|
229
|
+
#
|
|
230
|
+
# @param [ Hash ] attrs The hash of attributes to instantiate with.
|
|
231
|
+
#
|
|
232
|
+
# @return [ Document ] A new document.
|
|
233
|
+
def instantiate(attrs = nil)
|
|
234
|
+
attributes = attrs || {}
|
|
235
|
+
if attributes["_id"]
|
|
236
|
+
allocate.tap do |doc|
|
|
237
|
+
doc.instance_variable_set(:@attributes, attributes)
|
|
238
|
+
doc.setup_modifications
|
|
239
|
+
end
|
|
240
|
+
else
|
|
241
|
+
new(attrs)
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# Returns all types to query for when using this class as the base.
|
|
246
|
+
#
|
|
247
|
+
# @example Get the types.
|
|
248
|
+
# document._types
|
|
249
|
+
#
|
|
250
|
+
# @return [ Array<Class> ] All subclasses of the current document.
|
|
251
|
+
def _types
|
|
252
|
+
@_type ||= [descendants + [self]].flatten.uniq.map(&:to_s)
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
end
|
|
@@ -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,27 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc
|
|
3
|
+
module Errors #:nodoc
|
|
4
|
+
|
|
5
|
+
# Default parent Mongoid error for all custom errors. This handles the base
|
|
6
|
+
# key for the translations and provides the convenience method for
|
|
7
|
+
# translating the messages.
|
|
8
|
+
class MongoidError < StandardError
|
|
9
|
+
BASE_KEY = "mongoid.errors.messages"
|
|
10
|
+
|
|
11
|
+
# Given the key of the specific error and the options hash, translate the
|
|
12
|
+
# message.
|
|
13
|
+
#
|
|
14
|
+
# Options:
|
|
15
|
+
#
|
|
16
|
+
# key: The key of the error in the locales.
|
|
17
|
+
# options: The objects to pass to create the message.
|
|
18
|
+
#
|
|
19
|
+
# Returns:
|
|
20
|
+
#
|
|
21
|
+
# A localized error message string.
|
|
22
|
+
def translate(key, options)
|
|
23
|
+
::I18n.translate("#{BASE_KEY}.#{key}", options)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module Mongoid #:nodoc
|
|
2
|
+
module Errors #:nodoc
|
|
3
|
+
|
|
4
|
+
# This error is raised when trying to create set nested records above the
|
|
5
|
+
# specified :limit
|
|
6
|
+
#
|
|
7
|
+
# Example:
|
|
8
|
+
#
|
|
9
|
+
#<tt>TooManyNestedAttributeRecords.new('association', limit)
|
|
10
|
+
class TooManyNestedAttributeRecords < MongoidError
|
|
11
|
+
def initialize(association, limit)
|
|
12
|
+
super(
|
|
13
|
+
translate(
|
|
14
|
+
"too_many_nested_attribute_records",
|
|
15
|
+
{ :association => association, :limit => limit }
|
|
16
|
+
)
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc
|
|
3
|
+
module Errors #:nodoc
|
|
4
|
+
|
|
5
|
+
# Raised when attempting to call create or create! through a
|
|
6
|
+
# references_many when the parent document has not been saved. This
|
|
7
|
+
# prevents the child from getting presisted and immediately being orphaned.
|
|
8
|
+
class UnsavedDocument < MongoidError
|
|
9
|
+
|
|
10
|
+
attr_reader :base, :document
|
|
11
|
+
|
|
12
|
+
def initialize(base, document)
|
|
13
|
+
@base, @document = base, document
|
|
14
|
+
super(
|
|
15
|
+
translate(
|
|
16
|
+
"unsaved_document",
|
|
17
|
+
{ :base => base.class.name, :document => document.class.name }
|
|
18
|
+
)
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc
|
|
3
|
+
module Errors #:nodoc
|
|
4
|
+
|
|
5
|
+
# Raised when the database version is not supported by Mongoid.
|
|
6
|
+
#
|
|
7
|
+
# Example:
|
|
8
|
+
#
|
|
9
|
+
# <tt>UnsupportedVersion.new(Mongo::ServerVersion.new("1.3.1"))</tt>
|
|
10
|
+
class UnsupportedVersion < MongoidError
|
|
11
|
+
def initialize(version)
|
|
12
|
+
super(
|
|
13
|
+
translate(
|
|
14
|
+
"unsupported_version",
|
|
15
|
+
{ :version => version, :mongo_version => Mongoid::MONGODB_VERSION }
|
|
16
|
+
)
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc
|
|
3
|
+
module Errors #:nodoc
|
|
4
|
+
|
|
5
|
+
# Raised when a persistence method ending in ! fails validation. The message
|
|
6
|
+
# will contain the full error messages from the +Document+ in question.
|
|
7
|
+
#
|
|
8
|
+
# Example:
|
|
9
|
+
#
|
|
10
|
+
# <tt>Validations.new(person.errors)</tt>
|
|
11
|
+
class Validations < MongoidError
|
|
12
|
+
attr_reader :document
|
|
13
|
+
def initialize(document)
|
|
14
|
+
@document = document
|
|
15
|
+
super(
|
|
16
|
+
translate(
|
|
17
|
+
"validations",
|
|
18
|
+
{ :errors => document.errors.full_messages.join(", ") }
|
|
19
|
+
)
|
|
20
|
+
)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
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/too_many_nested_attribute_records"
|
|
10
|
+
require "mongoid/errors/unsaved_document"
|
|
11
|
+
require "mongoid/errors/unsupported_version"
|
|
12
|
+
require "mongoid/errors/validations"
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Extensions #:nodoc:
|
|
4
|
+
module Array #:nodoc:
|
|
5
|
+
# This module converts arrays into mongoid related objects.
|
|
6
|
+
module Conversions #:nodoc:
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
module ClassMethods #:nodoc:
|
|
10
|
+
def raise_or_return(value)
|
|
11
|
+
unless value.nil? || value.is_a?(::Array)
|
|
12
|
+
raise Mongoid::Errors::InvalidType.new(::Array, value)
|
|
13
|
+
end
|
|
14
|
+
value
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
alias :get :raise_or_return
|
|
18
|
+
alias :set :raise_or_return
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Extensions #:nodoc:
|
|
4
|
+
module Array #:nodoc:
|
|
5
|
+
module Parentization #:nodoc:
|
|
6
|
+
# Adds the parent document to each element in the array.
|
|
7
|
+
def parentize(parent)
|
|
8
|
+
each { |obj| obj.parentize(parent) }
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require "bigdecimal"
|
|
3
|
+
|
|
4
|
+
module Mongoid #:nodoc:
|
|
5
|
+
module Extensions #:nodoc:
|
|
6
|
+
module BigDecimal #:nodoc:
|
|
7
|
+
module Conversions #:nodoc:
|
|
8
|
+
# Get the string as a +BigDecimal+
|
|
9
|
+
def get(value)
|
|
10
|
+
value ? ::BigDecimal.new(value) : value
|
|
11
|
+
end
|
|
12
|
+
# Set the value in the hash as a string.
|
|
13
|
+
def set(value)
|
|
14
|
+
value ? value.to_s : value
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Extensions #:nodoc:
|
|
4
|
+
module Binary #:nodoc:
|
|
5
|
+
module Conversions #:nodoc:
|
|
6
|
+
# Get the value from the db hash.
|
|
7
|
+
def get(value)
|
|
8
|
+
value
|
|
9
|
+
end
|
|
10
|
+
# Set the value in the db hash.
|
|
11
|
+
def set(value)
|
|
12
|
+
value
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Extensions #:nodoc:
|
|
4
|
+
module Boolean #:nodoc:
|
|
5
|
+
module Conversions #:nodoc:
|
|
6
|
+
extend ActiveSupport::Concern
|
|
7
|
+
|
|
8
|
+
BOOLEAN_MAP = {
|
|
9
|
+
true => true, "true" => true, "TRUE" => true, "1" => true, 1 => true, 1.0 => true,
|
|
10
|
+
false => false, "false" => false, "FALSE" => false, "0" => false, 0 => false, 0.0 => false
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module ClassMethods #:nodoc
|
|
14
|
+
|
|
15
|
+
def set(value)
|
|
16
|
+
value = BOOLEAN_MAP[value]
|
|
17
|
+
value.nil? ? nil : value
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def get(value)
|
|
21
|
+
value
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Extensions #:nodoc:
|
|
4
|
+
module Date #:nodoc:
|
|
5
|
+
module Conversions #:nodoc:
|
|
6
|
+
def get(value)
|
|
7
|
+
return nil if value.blank?
|
|
8
|
+
if Mongoid::Config.use_utc?
|
|
9
|
+
value.to_date
|
|
10
|
+
else
|
|
11
|
+
::Date.new(value.year, value.month, value.day)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
protected
|
|
16
|
+
|
|
17
|
+
def convert_to_time(value)
|
|
18
|
+
value = ::Date.parse(value) if value.is_a?(::String)
|
|
19
|
+
value = ::Date.civil(*value) if value.is_a?(::Array)
|
|
20
|
+
::Time.utc(value.year, value.month, value.day)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|