mongoid-braxton 2.0.2

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 (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,45 @@
1
+ # encoding: utf-8
2
+ module Mongoid # :nodoc:
3
+ module Relations #:nodoc:
4
+
5
+ # The reflections module provides convenience methods that can retrieve
6
+ # useful information about associations.
7
+ module Reflections
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+
12
+ delegate \
13
+ :reflect_on_association,
14
+ :reflect_on_all_associations, :to => "self.class"
15
+ end
16
+
17
+ module ClassMethods #:nodoc
18
+
19
+ # Returns the relation metadata for the supplied name.
20
+ #
21
+ # @example Find relation metadata by name.
22
+ # Person.reflect_on_association(:addresses)
23
+ #
24
+ # @param [ String, Symbol ] name The name of the relation to find.
25
+ #
26
+ # @return [ Metadata ] The matching relation metadata.
27
+ def reflect_on_association(name)
28
+ relations[name.to_s]
29
+ end
30
+
31
+ # Returns all relation metadata for the supplied macros.
32
+ #
33
+ # @example Find multiple relation metadata by macro.
34
+ # Person.reflect_on_all_associations(:embeds_many)
35
+ #
36
+ # @param [ Array<String, Symbol> ] *macros The relation macros.
37
+ #
38
+ # @return [ Array<Metadata> ] The matching relation metadata.
39
+ def reflect_on_all_associations(*macros)
40
+ relations.values.select { |meta| macros.include?(meta.macro) }
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+
4
+ # Contains behaviour for determining if Mongoid is in safe mode.
5
+ module Safe
6
+
7
+ # Determine based on configuration if we are persisting in safe mode or
8
+ # not.
9
+ #
10
+ # The query option will always override the global configuration.
11
+ #
12
+ # @example Are we in safe mode?
13
+ # document.safe_mode?(:safe => true)
14
+ #
15
+ # @param [ Hash ] options Persistence options.
16
+ #
17
+ # @return [ true, false ] True if in safe mode, false if not.
18
+ def safe_mode?(options)
19
+ safe = options[:safe]
20
+ safe.nil? ? Mongoid.persist_in_safe_mode : safe
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,207 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+
4
+ # The +Safety+ module is used to provide a DSL to execute database operations
5
+ # in safe mode on a per query basis, either from the +Document+ class level
6
+ # or instance level.
7
+ module Safety
8
+ extend ActiveSupport::Concern
9
+
10
+ # Execute the following class-level persistence operation in safe mode.
11
+ #
12
+ # @example Upsert in safe mode.
13
+ # person.safely.upsert
14
+ #
15
+ # @example Destroy in safe mode with w and fsync options.
16
+ # person.safely(:w => 2, :fsync => true).destroy
17
+ #
18
+ # @param [ Hash ] options The safe mode options.
19
+ #
20
+ # @option options [ Integer ] :w The number of nodes to write to.
21
+ # @option options [ Integer ] :wtimeout Time to wait for return from all
22
+ # nodes.
23
+ # @option options [ true, false ] :fsync Should a fsync occur.
24
+ #
25
+ # @return [ Proxy ] The safety proxy.
26
+ def safely(safety = true)
27
+ Proxy.new(self, safety)
28
+ end
29
+
30
+ module ClassMethods #:nodoc:
31
+
32
+ # Execute the following class-level persistence operation in safe mode.
33
+ #
34
+ # @example Create in safe mode.
35
+ # Person.safely.create(:name => "John")
36
+ #
37
+ # @example Delete all in safe mode with options.
38
+ # Person.safely(:w => 2, :fsync => true).delete_all
39
+ #
40
+ # @param [ Hash ] options The safe mode options.
41
+ #
42
+ # @option options [ Integer ] :w The number of nodes to write to.
43
+ # @option options [ Integer ] :wtimeout Time to wait for return from all
44
+ # nodes.
45
+ # @option options [ true, false ] :fsync Should a fsync occur.
46
+ #
47
+ # @return [ Proxy ] The safety proxy.
48
+ def safely(safety = true)
49
+ Proxy.new(self, safety)
50
+ end
51
+ end
52
+
53
+ # When this class proxies a document or class, the next persistence
54
+ # operation executed on it will query in safe mode.
55
+ #
56
+ # Operations that took a hash of attributes had to be somewhat duplicated
57
+ # here since we do not want to allow a :safe attribute to be included in
58
+ # the args. This is because safe could be a common attribute name and we
59
+ # don't want the collision between the attribute and determining whether or
60
+ # not safe mode is allowed.
61
+ class Proxy
62
+
63
+ attr_reader :target, :safety_options
64
+
65
+ # Create a new +Document+. This will instantiate a new document and
66
+ # insert it in a single call. Will always return the document
67
+ # whether save passed or not.
68
+ #
69
+ # @example Safely create a document.
70
+ # Person.safely.create(:title => "Mr")
71
+ #
72
+ # @param [ Hash ] attributes The attributes to create with.
73
+ #
74
+ # @return [ Document ] The new document.
75
+ def create(attributes = {})
76
+ target.new(attributes).tap { |doc| doc.insert(:safe => safety_options) }
77
+ end
78
+
79
+ # Create a new +Document+. This will instantiate a new document and
80
+ # insert it in a single call. Will always return the document
81
+ # whether save passed or not, and if validation fails an error will be
82
+ # raise.
83
+ #
84
+ # @example Safely create a document.
85
+ # Person.safely.create!(:title => "Mr")
86
+ #
87
+ # @param [ Hash ] attributes The attributes to create with.
88
+ #
89
+ # @raise [ Errors::Validations ] If validation failed.
90
+ #
91
+ # @return [ Document ] If validation passed.
92
+ def create!(attributes = {})
93
+ target.new(attributes).tap do |document|
94
+ fail_validate!(document) if document.insert(:safe => safety_options).errors.any?
95
+ end
96
+ end
97
+
98
+ # Delete all documents given the supplied conditions. If no conditions
99
+ # are passed, the entire collection will be dropped for performance
100
+ # benefits. Does not fire any callbacks.
101
+ #
102
+ # @example Delete all documents.
103
+ # Person.safely.delete_all
104
+ #
105
+ # @example Conditionally delete all documents.
106
+ # Person.safely.delete_all(:conditions => { :title => "Sir" })
107
+ #
108
+ # @param [ Hash ] conditions The conditions to delete with.
109
+ #
110
+ # @return [ Integer ] The number of documents deleted.
111
+ def delete_all(conditions = {})
112
+ Mongoid::Persistence::RemoveAll.new(
113
+ target,
114
+ { :validate => false, :safe => safety_options },
115
+ conditions[:conditions] || {}
116
+ ).persist
117
+ end
118
+
119
+ # destroy all documents given the supplied conditions. If no conditions
120
+ # are passed, the entire collection will be dropped for performance
121
+ # benefits. Fires the destroy callbacks if conditions were passed.
122
+ #
123
+ # @example destroy all documents.
124
+ # Person.safely.destroy_all
125
+ #
126
+ # @example Conditionally destroy all documents.
127
+ # Person.safely.destroy_all(:conditions => { :title => "Sir" })
128
+ #
129
+ # @param [ Hash ] conditions The conditions to destroy with.
130
+ #
131
+ # @return [ Integer ] The number of documents destroyd.
132
+ def destroy_all(conditions = {})
133
+ documents = target.all(conditions)
134
+ documents.count.tap do |count|
135
+ documents.each { |doc| doc.destroy(:safe => safety_options) }
136
+ end
137
+ end
138
+
139
+ # Increment the field by the provided value, else if it doesn't exists set
140
+ # it to that value.
141
+ #
142
+ # @example Safely increment a field.
143
+ # person.safely.inc(:age, 1)
144
+ #
145
+ # @param [ Symbol, String ] field The field to increment.
146
+ # @param [ Integer ] value The value to increment by.
147
+ # @param [ Hash ] options Options to pass through to the driver.
148
+ def inc(field, value, options = {})
149
+ target.inc(field, value, :safe => safety_options)
150
+ end
151
+
152
+ # Create the new +Proxy+.
153
+ #
154
+ # @example Create the proxy.
155
+ # Proxy.new(document, :w => 3)
156
+ #
157
+ # @param [ Document, Class ] target Either the class or the instance.
158
+ # @param [ true, Hash ] safety_options The options.
159
+ def initialize(target, safety_options)
160
+ @target = target
161
+ @safety_options = safety_options
162
+ end
163
+
164
+ # We will use method missing to proxy calls to the target.
165
+ #
166
+ # @example Save safely.
167
+ # person.safely.save
168
+ #
169
+ # @param [ Array ] *args The arguments to pass on.
170
+ def method_missing(*args)
171
+ name = args[0]
172
+ attributes = args[1] || {}
173
+ target.send(name, attributes.merge(:safe => safety_options))
174
+ end
175
+
176
+ # Update the +Document+ attributes in the datbase.
177
+ #
178
+ # @example Safely update attributes.
179
+ # person.safely.update_attributes(:title => "Sir")
180
+ #
181
+ # @param [ Hash ] attributes The attributes to update.
182
+ #
183
+ # @return [ true, false ] Whether the document was saved.
184
+ def update_attributes(attributes = {})
185
+ target.write_attributes(attributes)
186
+ target.update(:safe => safety_options)
187
+ end
188
+
189
+ # Update the +Document+ attributes in the datbase.
190
+ #
191
+ # @example Safely update attributes.
192
+ # person.safely.update_attributes(:title => "Sir")
193
+ #
194
+ # @param [ Hash ] attributes The attributes to update.
195
+ #
196
+ # @raise [ Errors::Validations ] If validation failed.
197
+ #
198
+ # @return [ true ] If the document was saved.
199
+ def update_attributes!(attributes = {})
200
+ target.write_attributes(attributes)
201
+ update(:safe => safety_options).tap do |result|
202
+ target.class.fail_validate!(target) unless result
203
+ end
204
+ end
205
+ end
206
+ end
207
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ # This module handles behaviour for defining scopes on classes.
4
+ class Scope
5
+ attr_reader :conditions, :extensions
6
+
7
+ # Create the new +Scope+. If a block is passed in, this Scope will store
8
+ # the block for future calls to #extend.
9
+ #
10
+ # @example Create a new scope.
11
+ # Scope.new(:title => "Sir")
12
+ #
13
+ # @param [ Hash ] conditions The scoping limitations.
14
+ def initialize(conditions = {}, &block)
15
+ @conditions = conditions
16
+ @extensions = Module.new(&block) if block_given?
17
+ end
18
+
19
+ # Extend a supplied criteria.
20
+ #
21
+ # @example Extend the criteria.
22
+ # scope.extend(criteria)
23
+ #
24
+ # @param [ Criteria } criteria A mongoid criteria to extend.
25
+ #
26
+ # @return [ Criteria ] The new criteria object.
27
+ def extend(criteria)
28
+ extensions ? criteria.extend(extensions) : criteria
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,99 @@
1
+ # encoding: utf-8
2
+ module Mongoid # :nodoc:
3
+
4
+ # This module provides the extra behaviour for including relations in JSON
5
+ # and XML serialization.
6
+ module Serialization
7
+ extend ActiveSupport::Concern
8
+ include ActiveModel::Serialization
9
+
10
+ # Gets the document as a serializable hash, used by ActiveModel's JSON and
11
+ # XML serializers. This override is just to be able to pass the :include
12
+ # and :except options to get associations in the hash.
13
+ #
14
+ # @example Get the serializable hash.
15
+ # document.serializable_hash
16
+ #
17
+ # @example Get the serializable hash with options.
18
+ # document.serializable_hash(:include => :addresses)
19
+ #
20
+ # @param [ Hash ] options The options to pass.
21
+ #
22
+ # @option options [ Symbol ] :include What relations to include
23
+ # @option options [ Symbol ] :only Limit the fields to only these.
24
+ # @option options [ Symbol ] :except Dont include these fields.
25
+ #
26
+ # @return [ Hash ] The document, ready to be serialized.
27
+ #
28
+ # @since 2.0.0.rc.6
29
+ def serializable_hash(options = nil)
30
+ options ||= {}
31
+ super(options).tap do |attrs|
32
+ serialize_relations(attrs, options) if options[:include]
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ # For each of the provided include options, get the relation needed and
39
+ # provide it in the hash.
40
+ #
41
+ # @example Serialize the included relations.
42
+ # document.serialize_relations({}, :include => :addresses)
43
+ #
44
+ # @param [ Hash ] attributes The attributes to serialize.
45
+ # @param [ Hash ] options The serialization options.
46
+ #
47
+ # @option options [ Symbol ] :include What relations to include
48
+ # @option options [ Symbol ] :only Limit the fields to only these.
49
+ # @option options [ Symbol ] :except Dont include these fields.
50
+ #
51
+ # @since 2.0.0.rc.6
52
+ def serialize_relations(attributes = {}, options = {})
53
+ inclusions = options[:include]
54
+ relation_names(inclusions).each do |name|
55
+ metadata = relations[name.to_s]
56
+ relation = send(metadata.name, false, :eager => true)
57
+ if relation
58
+ attributes[metadata.name.to_s] =
59
+ relation.serializable_hash(relation_options(inclusions, options, name))
60
+ end
61
+ end
62
+ end
63
+
64
+ # Since the inclusions can be a hash, symbol, or array of symbols, this is
65
+ # provided as a convenience to parse out the names.
66
+ #
67
+ # @example Get the relation names.
68
+ # document.relation_names(:include => [ :addresses ])
69
+ #
70
+ # @param [ Hash, Symbol, Array<Symbol ] inclusions The inclusions.
71
+ #
72
+ # @return [ Array<Symbol> ] The names of the included relations.
73
+ #
74
+ # @since 2.0.0.rc.6
75
+ def relation_names(inclusions)
76
+ inclusions.is_a?(Hash) ? inclusions.keys : Array.wrap(inclusions)
77
+ end
78
+
79
+ # Since the inclusions can be a hash, symbol, or array of symbols, this is
80
+ # provided as a convenience to parse out the options.
81
+ #
82
+ # @example Get the relation options.
83
+ # document.relation_names(:include => [ :addresses ])
84
+ #
85
+ # @param [ Hash, Symbol, Array<Symbol ] inclusions The inclusions.
86
+ # @param [ Symbol ] name The name of the relation.
87
+ #
88
+ # @return [ Hash ] The options for the relation.
89
+ #
90
+ # @since 2.0.0.rc.6
91
+ def relation_options(inclusions, options, name)
92
+ if inclusions.is_a?(Hash)
93
+ inclusions[name]
94
+ else
95
+ { :except => options[:except], :only => options[:only] }
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+
4
+ # This module contains behaviour for adding shard key fields to updates.
5
+ module Sharding
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ cattr_accessor :shard_key_fields
10
+ self.shard_key_fields = []
11
+
12
+ delegate :shard_key_fields, :to => "self.class"
13
+ end
14
+
15
+ # Get the document selector with the defined shard keys.
16
+ #
17
+ # @example Get the selector for the shard keys.
18
+ # person.shard_key_selector
19
+ #
20
+ # @return [ Hash ] The shard key selector.
21
+ #
22
+ # @since 2.0.0
23
+ def shard_key_selector
24
+ {}.tap do |selector|
25
+ shard_key_fields.each do |field|
26
+ selector[field.to_s] = send(field)
27
+ end
28
+ end
29
+ end
30
+
31
+ module ClassMethods #:nodoc
32
+
33
+ # Specifies a shard key with the field(s) specified.
34
+ #
35
+ # @example Specify the shard key.
36
+ #
37
+ # class Person
38
+ # include Mongoid::Document
39
+ # field :first_name, :type => String
40
+ # field :last_name, :type => String
41
+ #
42
+ # shard_key :first_name, :last_name
43
+ # end
44
+ #
45
+ # @since 2.0.0
46
+ def shard_key(*names)
47
+ self.shard_key_fields = names
48
+ end
49
+ end
50
+ end
51
+ end