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.
Files changed (199) 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 +44 -0
  5. data/lib/config/locales/de.yml +44 -0
  6. data/lib/config/locales/en.yml +45 -0
  7. data/lib/config/locales/es.yml +44 -0
  8. data/lib/config/locales/fr.yml +45 -0
  9. data/lib/config/locales/hu.yml +47 -0
  10. data/lib/config/locales/it.yml +42 -0
  11. data/lib/config/locales/kr.yml +68 -0
  12. data/lib/config/locales/nl.yml +42 -0
  13. data/lib/config/locales/pl.yml +42 -0
  14. data/lib/config/locales/pt-br.yml +43 -0
  15. data/lib/config/locales/pt.yml +43 -0
  16. data/lib/config/locales/ro.yml +49 -0
  17. data/lib/config/locales/sv.yml +43 -0
  18. data/lib/config/locales/zh-CN.yml +34 -0
  19. data/lib/mongoid/atomicity.rb +111 -0
  20. data/lib/mongoid/attributes.rb +251 -0
  21. data/lib/mongoid/callbacks.rb +13 -0
  22. data/lib/mongoid/collection.rb +137 -0
  23. data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
  24. data/lib/mongoid/collections/master.rb +29 -0
  25. data/lib/mongoid/collections/operations.rb +42 -0
  26. data/lib/mongoid/collections/slaves.rb +45 -0
  27. data/lib/mongoid/collections.rb +70 -0
  28. data/lib/mongoid/components.rb +45 -0
  29. data/lib/mongoid/config/database.rb +167 -0
  30. data/lib/mongoid/config/replset_database.rb +48 -0
  31. data/lib/mongoid/config.rb +343 -0
  32. data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
  33. data/lib/mongoid/contexts/enumerable.rb +226 -0
  34. data/lib/mongoid/contexts/ids.rb +25 -0
  35. data/lib/mongoid/contexts/mongo.rb +345 -0
  36. data/lib/mongoid/contexts/paging.rb +50 -0
  37. data/lib/mongoid/contexts.rb +21 -0
  38. data/lib/mongoid/copyable.rb +44 -0
  39. data/lib/mongoid/criteria.rb +325 -0
  40. data/lib/mongoid/criterion/complex.rb +34 -0
  41. data/lib/mongoid/criterion/creational.rb +34 -0
  42. data/lib/mongoid/criterion/exclusion.rb +67 -0
  43. data/lib/mongoid/criterion/inclusion.rb +134 -0
  44. data/lib/mongoid/criterion/inspection.rb +20 -0
  45. data/lib/mongoid/criterion/optional.rb +213 -0
  46. data/lib/mongoid/criterion/selector.rb +74 -0
  47. data/lib/mongoid/cursor.rb +81 -0
  48. data/lib/mongoid/default_scope.rb +28 -0
  49. data/lib/mongoid/dirty.rb +251 -0
  50. data/lib/mongoid/document.rb +256 -0
  51. data/lib/mongoid/errors/document_not_found.rb +29 -0
  52. data/lib/mongoid/errors/invalid_collection.rb +19 -0
  53. data/lib/mongoid/errors/invalid_database.rb +20 -0
  54. data/lib/mongoid/errors/invalid_field.rb +19 -0
  55. data/lib/mongoid/errors/invalid_options.rb +16 -0
  56. data/lib/mongoid/errors/invalid_type.rb +26 -0
  57. data/lib/mongoid/errors/mongoid_error.rb +27 -0
  58. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +21 -0
  59. data/lib/mongoid/errors/unsaved_document.rb +23 -0
  60. data/lib/mongoid/errors/unsupported_version.rb +21 -0
  61. data/lib/mongoid/errors/validations.rb +24 -0
  62. data/lib/mongoid/errors.rb +12 -0
  63. data/lib/mongoid/extensions/array/conversions.rb +23 -0
  64. data/lib/mongoid/extensions/array/parentization.rb +13 -0
  65. data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
  66. data/lib/mongoid/extensions/binary/conversions.rb +17 -0
  67. data/lib/mongoid/extensions/boolean/conversions.rb +27 -0
  68. data/lib/mongoid/extensions/date/conversions.rb +25 -0
  69. data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
  70. data/lib/mongoid/extensions/false_class/equality.rb +13 -0
  71. data/lib/mongoid/extensions/float/conversions.rb +20 -0
  72. data/lib/mongoid/extensions/hash/conversions.rb +19 -0
  73. data/lib/mongoid/extensions/hash/criteria_helpers.rb +22 -0
  74. data/lib/mongoid/extensions/hash/scoping.rb +12 -0
  75. data/lib/mongoid/extensions/integer/conversions.rb +20 -0
  76. data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
  77. data/lib/mongoid/extensions/object/conversions.rb +25 -0
  78. data/lib/mongoid/extensions/object/reflections.rb +17 -0
  79. data/lib/mongoid/extensions/object/yoda.rb +27 -0
  80. data/lib/mongoid/extensions/object_id/conversions.rb +57 -0
  81. data/lib/mongoid/extensions/proc/scoping.rb +12 -0
  82. data/lib/mongoid/extensions/set/conversions.rb +20 -0
  83. data/lib/mongoid/extensions/string/conversions.rb +34 -0
  84. data/lib/mongoid/extensions/string/inflections.rb +97 -0
  85. data/lib/mongoid/extensions/symbol/conversions.rb +21 -0
  86. data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
  87. data/lib/mongoid/extensions/time_conversions.rb +38 -0
  88. data/lib/mongoid/extensions/true_class/equality.rb +13 -0
  89. data/lib/mongoid/extensions.rb +116 -0
  90. data/lib/mongoid/extras.rb +61 -0
  91. data/lib/mongoid/factory.rb +20 -0
  92. data/lib/mongoid/field.rb +95 -0
  93. data/lib/mongoid/fields.rb +138 -0
  94. data/lib/mongoid/finders.rb +173 -0
  95. data/lib/mongoid/hierarchy.rb +85 -0
  96. data/lib/mongoid/identity.rb +89 -0
  97. data/lib/mongoid/indexes.rb +38 -0
  98. data/lib/mongoid/inspection.rb +58 -0
  99. data/lib/mongoid/javascript/functions.yml +37 -0
  100. data/lib/mongoid/javascript.rb +21 -0
  101. data/lib/mongoid/json.rb +16 -0
  102. data/lib/mongoid/keys.rb +77 -0
  103. data/lib/mongoid/logger.rb +18 -0
  104. data/lib/mongoid/matchers/all.rb +11 -0
  105. data/lib/mongoid/matchers/default.rb +27 -0
  106. data/lib/mongoid/matchers/exists.rb +13 -0
  107. data/lib/mongoid/matchers/gt.rb +11 -0
  108. data/lib/mongoid/matchers/gte.rb +11 -0
  109. data/lib/mongoid/matchers/in.rb +11 -0
  110. data/lib/mongoid/matchers/lt.rb +11 -0
  111. data/lib/mongoid/matchers/lte.rb +11 -0
  112. data/lib/mongoid/matchers/ne.rb +11 -0
  113. data/lib/mongoid/matchers/nin.rb +11 -0
  114. data/lib/mongoid/matchers/size.rb +11 -0
  115. data/lib/mongoid/matchers.rb +55 -0
  116. data/lib/mongoid/modifiers/command.rb +18 -0
  117. data/lib/mongoid/modifiers/inc.rb +24 -0
  118. data/lib/mongoid/modifiers.rb +24 -0
  119. data/lib/mongoid/multi_database.rb +11 -0
  120. data/lib/mongoid/multi_parameter_attributes.rb +80 -0
  121. data/lib/mongoid/named_scope.rb +36 -0
  122. data/lib/mongoid/nested_attributes.rb +43 -0
  123. data/lib/mongoid/paranoia.rb +103 -0
  124. data/lib/mongoid/paths.rb +61 -0
  125. data/lib/mongoid/persistence/command.rb +59 -0
  126. data/lib/mongoid/persistence/insert.rb +53 -0
  127. data/lib/mongoid/persistence/insert_embedded.rb +42 -0
  128. data/lib/mongoid/persistence/remove.rb +44 -0
  129. data/lib/mongoid/persistence/remove_all.rb +40 -0
  130. data/lib/mongoid/persistence/remove_embedded.rb +48 -0
  131. data/lib/mongoid/persistence/update.rb +76 -0
  132. data/lib/mongoid/persistence.rb +237 -0
  133. data/lib/mongoid/railtie.rb +129 -0
  134. data/lib/mongoid/railties/database.rake +171 -0
  135. data/lib/mongoid/railties/document.rb +12 -0
  136. data/lib/mongoid/relations/accessors.rb +157 -0
  137. data/lib/mongoid/relations/auto_save.rb +34 -0
  138. data/lib/mongoid/relations/binding.rb +26 -0
  139. data/lib/mongoid/relations/bindings/embedded/in.rb +82 -0
  140. data/lib/mongoid/relations/bindings/embedded/many.rb +98 -0
  141. data/lib/mongoid/relations/bindings/embedded/one.rb +66 -0
  142. data/lib/mongoid/relations/bindings/referenced/in.rb +74 -0
  143. data/lib/mongoid/relations/bindings/referenced/many.rb +96 -0
  144. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +99 -0
  145. data/lib/mongoid/relations/bindings/referenced/one.rb +66 -0
  146. data/lib/mongoid/relations/bindings.rb +9 -0
  147. data/lib/mongoid/relations/builder.rb +42 -0
  148. data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
  149. data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
  150. data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
  151. data/lib/mongoid/relations/builders/nested_attributes/many.rb +116 -0
  152. data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
  153. data/lib/mongoid/relations/builders/referenced/in.rb +32 -0
  154. data/lib/mongoid/relations/builders/referenced/many.rb +26 -0
  155. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
  156. data/lib/mongoid/relations/builders/referenced/one.rb +30 -0
  157. data/lib/mongoid/relations/builders.rb +79 -0
  158. data/lib/mongoid/relations/cascading/delete.rb +19 -0
  159. data/lib/mongoid/relations/cascading/destroy.rb +19 -0
  160. data/lib/mongoid/relations/cascading/nullify.rb +18 -0
  161. data/lib/mongoid/relations/cascading/strategy.rb +26 -0
  162. data/lib/mongoid/relations/cascading.rb +55 -0
  163. data/lib/mongoid/relations/constraint.rb +45 -0
  164. data/lib/mongoid/relations/cyclic.rb +97 -0
  165. data/lib/mongoid/relations/embedded/in.rb +173 -0
  166. data/lib/mongoid/relations/embedded/many.rb +483 -0
  167. data/lib/mongoid/relations/embedded/one.rb +170 -0
  168. data/lib/mongoid/relations/macros.rb +306 -0
  169. data/lib/mongoid/relations/many.rb +171 -0
  170. data/lib/mongoid/relations/metadata.rb +533 -0
  171. data/lib/mongoid/relations/nested_builder.rb +68 -0
  172. data/lib/mongoid/relations/one.rb +47 -0
  173. data/lib/mongoid/relations/polymorphic.rb +54 -0
  174. data/lib/mongoid/relations/proxy.rb +128 -0
  175. data/lib/mongoid/relations/referenced/in.rb +216 -0
  176. data/lib/mongoid/relations/referenced/many.rb +443 -0
  177. data/lib/mongoid/relations/referenced/many_to_many.rb +344 -0
  178. data/lib/mongoid/relations/referenced/one.rb +206 -0
  179. data/lib/mongoid/relations/reflections.rb +45 -0
  180. data/lib/mongoid/relations.rb +105 -0
  181. data/lib/mongoid/safe.rb +23 -0
  182. data/lib/mongoid/safety.rb +207 -0
  183. data/lib/mongoid/scope.rb +31 -0
  184. data/lib/mongoid/serialization.rb +99 -0
  185. data/lib/mongoid/state.rb +66 -0
  186. data/lib/mongoid/timestamps.rb +38 -0
  187. data/lib/mongoid/validations/associated.rb +42 -0
  188. data/lib/mongoid/validations/uniqueness.rb +85 -0
  189. data/lib/mongoid/validations.rb +117 -0
  190. data/lib/mongoid/version.rb +4 -0
  191. data/lib/mongoid/versioning.rb +51 -0
  192. data/lib/mongoid.rb +139 -0
  193. data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
  194. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +23 -0
  195. data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
  196. data/lib/rails/generators/mongoid/model/templates/model.rb +17 -0
  197. data/lib/rails/generators/mongoid_generator.rb +61 -0
  198. data/lib/rails/mongoid.rb +57 -0
  199. metadata +380 -0
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ module Mongoid # :nodoc:
3
+ module Relations #:nodoc:
4
+
5
+ # Used for converting foreign key values to the correct type based on the
6
+ # types of ids that the document stores.
7
+ #
8
+ # @note Durran: The name of this class is this way to match the metadata
9
+ # getter, and foreign_key was already taken there.
10
+ class Constraint
11
+ attr_reader :metadata
12
+
13
+ # Create the new constraint with the metadata.
14
+ #
15
+ # @example Instantiate the constraint.
16
+ # Constraint.new(metdata)
17
+ #
18
+ # @param [ Metadata ] metadata The metadata of the relation.
19
+ #
20
+ # @since 2.0.0.rc.7
21
+ def initialize(metadata)
22
+ @metadata = metadata
23
+ end
24
+
25
+ # Convert the supplied object to the appropriate type to set as the
26
+ # foreign key for a relation.
27
+ #
28
+ # @example Convert the object.
29
+ # constraint.convert("12345")
30
+ #
31
+ # @param [ Object ] object The object to convert.
32
+ #
33
+ # @return [ Object ] The object cast to the correct type.
34
+ #
35
+ # @since 2.0.0.rc.7
36
+ def convert(object)
37
+ return object if metadata.polymorphic?
38
+ if metadata.stores_foreign_key? && metadata.klass.using_object_ids?
39
+ return BSON::ObjectId.cast!(metadata.klass, object)
40
+ end
41
+ object
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,97 @@
1
+ # encoding: utf-8
2
+ module Mongoid # :nodoc:
3
+ module Relations #:nodoc:
4
+
5
+ # This module provides convenience macros for using cyclic embedded
6
+ # relations.
7
+ module Cyclic
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods #:nodoc:
11
+
12
+ # Create a cyclic embedded relation that creates a tree hierarchy for
13
+ # the document and many embedded child documents.
14
+ #
15
+ # @example Set up a recursive embeds many.
16
+ #
17
+ # class Role
18
+ # include Mongoid::Document
19
+ # recursively_embeds_many
20
+ # end
21
+ #
22
+ # @example The previous example is a shorcut for this.
23
+ #
24
+ # class Role
25
+ # include Mongoid::Document
26
+ # embeds_many :child_roles, :class_name => "Role", :cyclic => true
27
+ # embedded_in :parent_role, :class_name => "Role", :cyclic => true
28
+ # end
29
+ #
30
+ # This provides the default nomenclature for accessing a parent document
31
+ # or its children.
32
+ #
33
+ # @since 2.0.0.rc.1
34
+ def recursively_embeds_many
35
+ embeds_many cyclic_child_name, :class_name => self.name, :cyclic => true
36
+ embedded_in cyclic_parent_name, :class_name => self.name, :cyclic => true
37
+ end
38
+
39
+ # Create a cyclic embedded relation that creates a single self
40
+ # referencing relationship for a parent and a single child.
41
+ #
42
+ # @example Set up a recursive embeds one.
43
+ #
44
+ # class Role
45
+ # include Mongoid::Document
46
+ # recursively_embeds_one
47
+ # end
48
+ #
49
+ # @example The previous example is a shorcut for this.
50
+ #
51
+ # class Role
52
+ # include Mongoid::Document
53
+ # embeds_one :child_role, :class_name => "Role", :cyclic => true
54
+ # embedded_in :parent_role, :class_name => "Role", :cyclic => true
55
+ # end
56
+ #
57
+ # This provides the default nomenclature for accessing a parent document
58
+ # or its children.
59
+ #
60
+ # @since 2.0.0.rc.1
61
+ def recursively_embeds_one
62
+ embeds_one cyclic_child_name(false), :class_name => self.name, :cyclic => true
63
+ embedded_in cyclic_parent_name, :class_name => self.name, :cyclic => true
64
+ end
65
+
66
+ private
67
+
68
+ # Determines the parent name given the class.
69
+ #
70
+ # @example Determine the parent name.
71
+ # Role.cyclic_parent_name
72
+ #
73
+ # @return [ String ] "parent_" plus the class name underscored.
74
+ #
75
+ # @since 2.0.0.rc.1
76
+ def cyclic_parent_name
77
+ ("parent_" << self.name.underscore.singularize).to_sym
78
+ end
79
+
80
+ # Determines the child name given the class.
81
+ #
82
+ # @example Determine the child name.
83
+ # Role.cyclic_child_name
84
+ #
85
+ # @param [ true, false ] many Is the a many relation?
86
+ #
87
+ # @return [ String ] "child_" plus the class name underscored in
88
+ # singular or plural form.
89
+ #
90
+ # @since 2.0.0.rc.1
91
+ def cyclic_child_name(many = true)
92
+ ("child_" << self.name.underscore.send(many ? :pluralize : :singularize)).to_sym
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,173 @@
1
+ # encoding: utf-8
2
+ module Mongoid # :nodoc:
3
+ module Relations #:nodoc:
4
+ module Embedded
5
+
6
+ # This class defines the behaviour necessary to handle relations that are
7
+ # embedded within another relation, either as a single document or
8
+ # multiple documents.
9
+ class In < Relations::One
10
+
11
+ # Binds the base object to the inverse of the relation. This is so we
12
+ # are referenced to the actual objects themselves and dont hit the
13
+ # database twice when setting the relations up.
14
+ #
15
+ # This is called after first creating the relation, or if a new object
16
+ # is set on the relation.
17
+ #
18
+ # @example Bind the relation.
19
+ # name.person.bind(:continue => true)
20
+ #
21
+ # @param [ Hash ] options The options to bind with.
22
+ #
23
+ # @option options [ true, false ] :binding Are we in build mode?
24
+ # @option options [ true, false ] :continue Continue binding the
25
+ # inverse?
26
+ #
27
+ # @since 2.0.0.rc.1
28
+ def bind(options = {})
29
+ binding.bind(options)
30
+ base.save if target.persisted? && !options[:binding]
31
+ end
32
+
33
+ # Instantiate a new embedded_in relation.
34
+ #
35
+ # @example Create the new relation.
36
+ # Embedded::In.new(name, person, metadata)
37
+ #
38
+ # @param [ Document ] base The document the relation hangs off of.
39
+ # @param [ Document ] target The target (parent) of the relation.
40
+ # @param [ Metadata ] metadata The relation metadata.
41
+ #
42
+ # @return [ In ] The proxy.
43
+ def initialize(base, target, metadata)
44
+ init(base, target, metadata) do
45
+ characterize_one(target)
46
+ base.parentize(target)
47
+ end
48
+ end
49
+
50
+ # Unbinds the base object to the inverse of the relation. This occurs
51
+ # when setting a side of the relation to nil.
52
+ #
53
+ # Will delete the object if necessary.
54
+ #
55
+ # @example Unbind the relation.
56
+ # name.person.unbind(:continue => false)
57
+ #
58
+ # @param [ Proxy ] old_target The previous target of the relation.
59
+ # @param [ Hash ] options The options to bind with.
60
+ #
61
+ # @option options [ true, false ] :binding Are we in build mode?
62
+ # @option options [ true, false ] :continue Continue binding the
63
+ # inverse?
64
+ #
65
+ # @since 2.0.0.rc.1
66
+ def unbind(old_target, options = {})
67
+ binding(old_target).unbind(options)
68
+ base.delete if old_target.persisted? && !base.destroyed?
69
+ end
70
+
71
+ private
72
+
73
+ # Instantiate the binding associated with this relation.
74
+ #
75
+ # @example Get the binding.
76
+ # binding([ address ])
77
+ #
78
+ # @param [ Proxy ] new_target The new documents to bind with.
79
+ #
80
+ # @return [ Binding ] A binding object.
81
+ #
82
+ # @since 2.0.0.rc.1
83
+ def binding(new_target = nil)
84
+ Bindings::Embedded::In.new(base, new_target || target, metadata)
85
+ end
86
+
87
+ class << self
88
+
89
+ # Return the builder that is responsible for generating the documents
90
+ # that will be used by this relation.
91
+ #
92
+ # @example Get the builder.
93
+ # Embedded::In.builder(meta, object, person)
94
+ #
95
+ # @param [ Metadata ] meta The metadata of the relation.
96
+ # @param [ Document, Hash ] object A document or attributes to build with.
97
+ #
98
+ # @return [ Builder ] A newly instantiated builder object.
99
+ #
100
+ # @since 2.0.0.rc.1
101
+ def builder(meta, object)
102
+ Builders::Embedded::In.new(meta, object)
103
+ end
104
+
105
+ # Returns true if the relation is an embedded one. In this case
106
+ # always true.
107
+ #
108
+ # @example Is this relation embedded?
109
+ # Embedded::In.embedded?
110
+ #
111
+ # @return [ true ] true.
112
+ #
113
+ # @since 2.0.0.rc.1
114
+ def embedded?
115
+ true
116
+ end
117
+
118
+ # Returns the macro for this relation. Used mostly as a helper in
119
+ # reflection.
120
+ #
121
+ # @example Get the macro.
122
+ # Mongoid::Relations::Embedded::In.macro
123
+ #
124
+ # @return [ Symbol ] :embedded_in.
125
+ #
126
+ # @since 2.0.0.rc.1
127
+ def macro
128
+ :embedded_in
129
+ end
130
+
131
+ # Return the nested builder that is responsible for generating
132
+ # the documents that will be used by this relation.
133
+ #
134
+ # @example Get the builder.
135
+ # NestedAttributes::One.builder(attributes, options)
136
+ #
137
+ # @param [ Metadata ] metadata The relation metadata.
138
+ # @param [ Hash ] attributes The attributes to build with.
139
+ # @param [ Hash ] options The options for the builder.
140
+ #
141
+ # @option options [ true, false ] :allow_destroy Can documents be
142
+ # deleted?
143
+ # @option options [ Integer ] :limit Max number of documents to
144
+ # create at once.
145
+ # @option options [ Proc, Symbol ] :reject_if If documents match this
146
+ # option then they are ignored.
147
+ # @option options [ true, false ] :update_only Only existing documents
148
+ # can be modified.
149
+ #
150
+ # @return [ Builder ] A newly instantiated nested builder object.
151
+ #
152
+ # @since 2.0.0.rc.1
153
+ def nested_builder(metadata, attributes, options)
154
+ Builders::NestedAttributes::One.new(metadata, attributes, options)
155
+ end
156
+
157
+ # Tells the caller if this relation is one that stores the foreign
158
+ # key on its own objects.
159
+ #
160
+ # @example Does this relation store a foreign key?
161
+ # Embedded::In.stores_foreign_key?
162
+ #
163
+ # @return [ false ] false.
164
+ #
165
+ # @since 2.0.0.rc.1
166
+ def stores_foreign_key?
167
+ false
168
+ end
169
+ end
170
+ end
171
+ end
172
+ end
173
+ end