mongoid 2.8.1 → 3.0.0.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (351) hide show
  1. data/CHANGELOG.md +802 -58
  2. data/LICENSE +1 -1
  3. data/README.md +10 -11
  4. data/Rakefile +2 -8
  5. data/lib/config/locales/en.yml +441 -51
  6. data/lib/mongoid.rb +24 -39
  7. data/lib/mongoid/atomic.rb +16 -33
  8. data/lib/mongoid/atomic/modifiers.rb +2 -2
  9. data/lib/mongoid/atomic/paths/embedded.rb +4 -5
  10. data/lib/mongoid/atomic/paths/embedded/many.rb +6 -6
  11. data/lib/mongoid/atomic/paths/embedded/one.rb +5 -5
  12. data/lib/mongoid/atomic/paths/root.rb +4 -5
  13. data/lib/mongoid/attributes.rb +95 -32
  14. data/lib/mongoid/attributes/processing.rb +14 -10
  15. data/lib/mongoid/attributes/readonly.rb +56 -0
  16. data/lib/mongoid/callbacks.rb +90 -27
  17. data/lib/mongoid/collections/retry.rb +2 -3
  18. data/lib/mongoid/components.rb +11 -23
  19. data/lib/mongoid/config.rb +87 -233
  20. data/lib/mongoid/config/environment.rb +5 -6
  21. data/lib/mongoid/config/inflections.rb +6 -0
  22. data/lib/mongoid/config/options.rb +1 -1
  23. data/lib/mongoid/config/validators.rb +3 -0
  24. data/lib/mongoid/config/validators/option.rb +25 -0
  25. data/lib/mongoid/config/validators/session.rb +140 -0
  26. data/lib/mongoid/contextual.rb +50 -0
  27. data/lib/mongoid/contextual/aggregable/memory.rb +98 -0
  28. data/lib/mongoid/contextual/aggregable/mongo.rb +181 -0
  29. data/lib/mongoid/contextual/atomic.rb +179 -0
  30. data/lib/mongoid/contextual/command.rb +43 -0
  31. data/lib/mongoid/contextual/find_and_modify.rb +66 -0
  32. data/lib/mongoid/contextual/map_reduce.rb +273 -0
  33. data/lib/mongoid/contextual/memory.rb +383 -0
  34. data/lib/mongoid/contextual/mongo.rb +543 -0
  35. data/lib/mongoid/copyable.rb +3 -34
  36. data/lib/mongoid/criteria.rb +436 -250
  37. data/lib/mongoid/criterion/inspection.rb +14 -8
  38. data/lib/mongoid/criterion/scoping.rb +114 -44
  39. data/lib/mongoid/dirty.rb +152 -67
  40. data/lib/mongoid/document.rb +69 -50
  41. data/lib/mongoid/errors.rb +22 -1
  42. data/lib/mongoid/errors/ambiguous_relationship.rb +51 -0
  43. data/lib/mongoid/errors/callback.rb +5 -6
  44. data/lib/mongoid/errors/delete_restriction.rb +29 -0
  45. data/lib/mongoid/errors/document_not_found.rb +98 -17
  46. data/lib/mongoid/errors/eager_load.rb +3 -6
  47. data/lib/mongoid/errors/invalid_collection.rb +3 -3
  48. data/lib/mongoid/errors/invalid_config_option.rb +27 -0
  49. data/lib/mongoid/errors/invalid_database.rb +3 -3
  50. data/lib/mongoid/errors/invalid_field.rb +54 -8
  51. data/lib/mongoid/errors/invalid_field_option.rb +35 -0
  52. data/lib/mongoid/errors/invalid_find.rb +3 -3
  53. data/lib/mongoid/errors/invalid_index.rb +28 -0
  54. data/lib/mongoid/errors/invalid_options.rb +4 -4
  55. data/lib/mongoid/errors/invalid_scope.rb +24 -0
  56. data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +38 -0
  57. data/lib/mongoid/errors/invalid_storage_options.rb +27 -0
  58. data/lib/mongoid/errors/invalid_time.rb +3 -6
  59. data/lib/mongoid/errors/inverse_not_found.rb +29 -0
  60. data/lib/mongoid/errors/mixed_relations.rb +4 -9
  61. data/lib/mongoid/errors/mixed_session_configuration.rb +28 -0
  62. data/lib/mongoid/errors/mongoid_error.rb +54 -3
  63. data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +28 -0
  64. data/lib/mongoid/errors/no_default_session.rb +23 -0
  65. data/lib/mongoid/errors/no_environment.rb +3 -3
  66. data/lib/mongoid/errors/no_map_reduce_output.rb +24 -0
  67. data/lib/mongoid/errors/no_parent.rb +24 -0
  68. data/lib/mongoid/errors/no_session_config.rb +22 -0
  69. data/lib/mongoid/errors/no_session_database.rb +27 -0
  70. data/lib/mongoid/errors/no_session_hosts.rb +27 -0
  71. data/lib/mongoid/errors/no_sessions_config.rb +20 -0
  72. data/lib/mongoid/errors/readonly_attribute.rb +25 -0
  73. data/lib/mongoid/errors/scope_overwrite.rb +4 -4
  74. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +4 -4
  75. data/lib/mongoid/errors/unknown_attribute.rb +25 -0
  76. data/lib/mongoid/errors/unsaved_document.rb +4 -8
  77. data/lib/mongoid/errors/unsupported_javascript.rb +27 -0
  78. data/lib/mongoid/errors/unsupported_version.rb +4 -4
  79. data/lib/mongoid/errors/validations.rb +7 -6
  80. data/lib/mongoid/errors/versioning_not_on_root.rb +23 -0
  81. data/lib/mongoid/extensions.rb +28 -76
  82. data/lib/mongoid/extensions/array.rb +127 -0
  83. data/lib/mongoid/extensions/big_decimal.rb +42 -0
  84. data/lib/mongoid/extensions/boolean.rb +24 -0
  85. data/lib/mongoid/extensions/date.rb +70 -0
  86. data/lib/mongoid/extensions/date_time.rb +68 -0
  87. data/lib/mongoid/extensions/false_class.rb +26 -0
  88. data/lib/mongoid/extensions/float.rb +44 -0
  89. data/lib/mongoid/extensions/hash.rb +91 -0
  90. data/lib/mongoid/extensions/integer.rb +54 -0
  91. data/lib/mongoid/extensions/module.rb +28 -0
  92. data/lib/mongoid/extensions/nil_class.rb +21 -0
  93. data/lib/mongoid/extensions/object.rb +188 -0
  94. data/lib/mongoid/extensions/object_id.rb +53 -0
  95. data/lib/mongoid/extensions/range.rb +55 -0
  96. data/lib/mongoid/extensions/regexp.rb +27 -0
  97. data/lib/mongoid/extensions/set.rb +55 -0
  98. data/lib/mongoid/extensions/string.rb +155 -0
  99. data/lib/mongoid/extensions/symbol.rb +54 -0
  100. data/lib/mongoid/extensions/time.rb +78 -0
  101. data/lib/mongoid/extensions/time_with_zone.rb +55 -0
  102. data/lib/mongoid/extensions/true_class.rb +26 -0
  103. data/lib/mongoid/factory.rb +1 -1
  104. data/lib/mongoid/fields.rb +129 -194
  105. data/lib/mongoid/fields/foreign_key.rb +134 -0
  106. data/lib/mongoid/fields/localized.rb +73 -0
  107. data/lib/mongoid/fields/standard.rb +268 -0
  108. data/lib/mongoid/fields/validators.rb +2 -0
  109. data/lib/mongoid/fields/validators/macro.rb +83 -0
  110. data/lib/mongoid/finders.rb +42 -43
  111. data/lib/mongoid/hierarchy.rb +25 -14
  112. data/lib/mongoid/identity_map.rb +31 -19
  113. data/lib/mongoid/indexes.rb +66 -15
  114. data/lib/mongoid/indexes/validators/options.rb +80 -0
  115. data/lib/mongoid/inspection.rb +1 -1
  116. data/lib/mongoid/javascript.rb +1 -1
  117. data/lib/mongoid/json.rb +2 -2
  118. data/lib/mongoid/loggable.rb +69 -0
  119. data/lib/mongoid/matchers.rb +1 -1
  120. data/lib/mongoid/matchers/all.rb +7 -8
  121. data/lib/mongoid/matchers/and.rb +3 -3
  122. data/lib/mongoid/matchers/default.rb +6 -4
  123. data/lib/mongoid/matchers/exists.rb +2 -2
  124. data/lib/mongoid/matchers/gt.rb +2 -2
  125. data/lib/mongoid/matchers/gte.rb +2 -2
  126. data/lib/mongoid/matchers/in.rb +3 -7
  127. data/lib/mongoid/matchers/lt.rb +2 -2
  128. data/lib/mongoid/matchers/lte.rb +2 -2
  129. data/lib/mongoid/matchers/ne.rb +2 -2
  130. data/lib/mongoid/matchers/nin.rb +2 -2
  131. data/lib/mongoid/matchers/or.rb +2 -2
  132. data/lib/mongoid/matchers/size.rb +2 -2
  133. data/lib/mongoid/matchers/strategies.rb +3 -3
  134. data/lib/mongoid/multi_parameter_attributes.rb +8 -10
  135. data/lib/mongoid/nested_attributes.rb +17 -9
  136. data/lib/mongoid/observer.rb +1 -2
  137. data/lib/mongoid/paranoia.rb +13 -18
  138. data/lib/mongoid/persistence.rb +43 -39
  139. data/lib/mongoid/persistence/atomic.rb +2 -2
  140. data/lib/mongoid/persistence/atomic/add_to_set.rb +5 -9
  141. data/lib/mongoid/persistence/atomic/bit.rb +5 -7
  142. data/lib/mongoid/persistence/atomic/inc.rb +5 -7
  143. data/lib/mongoid/persistence/atomic/operation.rb +45 -6
  144. data/lib/mongoid/persistence/atomic/pop.rb +5 -7
  145. data/lib/mongoid/persistence/atomic/pull.rb +5 -7
  146. data/lib/mongoid/persistence/atomic/pull_all.rb +5 -7
  147. data/lib/mongoid/persistence/atomic/push.rb +4 -10
  148. data/lib/mongoid/persistence/atomic/push_all.rb +4 -10
  149. data/lib/mongoid/persistence/atomic/rename.rb +6 -7
  150. data/lib/mongoid/persistence/atomic/sets.rb +5 -7
  151. data/lib/mongoid/persistence/atomic/unset.rb +4 -5
  152. data/lib/mongoid/persistence/deletion.rb +2 -2
  153. data/lib/mongoid/persistence/insertion.rb +10 -16
  154. data/lib/mongoid/persistence/modification.rb +5 -9
  155. data/lib/mongoid/persistence/operations.rb +6 -19
  156. data/lib/mongoid/persistence/operations/embedded/insert.rb +7 -6
  157. data/lib/mongoid/persistence/operations/embedded/remove.rb +5 -5
  158. data/lib/mongoid/persistence/operations/insert.rb +4 -4
  159. data/lib/mongoid/persistence/operations/remove.rb +4 -4
  160. data/lib/mongoid/persistence/operations/update.rb +5 -5
  161. data/lib/mongoid/railtie.rb +26 -11
  162. data/lib/mongoid/railties/database.rake +22 -108
  163. data/lib/mongoid/relations.rb +4 -6
  164. data/lib/mongoid/relations/accessors.rb +119 -19
  165. data/lib/mongoid/relations/auto_save.rb +59 -5
  166. data/lib/mongoid/relations/binding.rb +211 -2
  167. data/lib/mongoid/relations/bindings/embedded/in.rb +16 -22
  168. data/lib/mongoid/relations/bindings/embedded/many.rb +9 -50
  169. data/lib/mongoid/relations/bindings/embedded/one.rb +10 -16
  170. data/lib/mongoid/relations/bindings/referenced/in.rb +31 -57
  171. data/lib/mongoid/relations/bindings/referenced/many.rb +8 -20
  172. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +15 -19
  173. data/lib/mongoid/relations/bindings/referenced/one.rb +10 -24
  174. data/lib/mongoid/relations/builder.rb +3 -3
  175. data/lib/mongoid/relations/builders.rb +19 -16
  176. data/lib/mongoid/relations/builders/embedded/in.rb +5 -5
  177. data/lib/mongoid/relations/builders/embedded/many.rb +12 -12
  178. data/lib/mongoid/relations/builders/embedded/one.rb +6 -6
  179. data/lib/mongoid/relations/builders/nested_attributes/many.rb +8 -8
  180. data/lib/mongoid/relations/builders/nested_attributes/one.rb +4 -4
  181. data/lib/mongoid/relations/builders/referenced/in.rb +4 -4
  182. data/lib/mongoid/relations/builders/referenced/many.rb +5 -5
  183. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +7 -5
  184. data/lib/mongoid/relations/builders/referenced/one.rb +5 -5
  185. data/lib/mongoid/relations/cascading.rb +6 -4
  186. data/lib/mongoid/relations/cascading/delete.rb +3 -5
  187. data/lib/mongoid/relations/cascading/destroy.rb +3 -3
  188. data/lib/mongoid/relations/cascading/nullify.rb +3 -3
  189. data/lib/mongoid/relations/cascading/restrict.rb +37 -0
  190. data/lib/mongoid/relations/constraint.rb +4 -3
  191. data/lib/mongoid/relations/conversions.rb +5 -6
  192. data/lib/mongoid/relations/cyclic.rb +7 -7
  193. data/lib/mongoid/relations/embedded/batchable.rb +346 -0
  194. data/lib/mongoid/relations/embedded/in.rb +23 -12
  195. data/lib/mongoid/relations/embedded/many.rb +99 -161
  196. data/lib/mongoid/relations/embedded/one.rb +25 -14
  197. data/lib/mongoid/relations/macros.rb +105 -61
  198. data/lib/mongoid/relations/many.rb +93 -14
  199. data/lib/mongoid/relations/metadata.rb +200 -45
  200. data/lib/mongoid/relations/nested_builder.rb +3 -5
  201. data/lib/mongoid/relations/one.rb +2 -2
  202. data/lib/mongoid/relations/options.rb +2 -2
  203. data/lib/mongoid/relations/polymorphic.rb +9 -9
  204. data/lib/mongoid/relations/proxy.rb +60 -31
  205. data/lib/mongoid/relations/referenced/in.rb +40 -15
  206. data/lib/mongoid/relations/referenced/many.rb +117 -132
  207. data/lib/mongoid/relations/referenced/many_to_many.rb +101 -46
  208. data/lib/mongoid/relations/referenced/one.rb +34 -13
  209. data/lib/mongoid/relations/reflections.rb +3 -3
  210. data/lib/mongoid/relations/synchronization.rb +19 -23
  211. data/lib/mongoid/relations/targets/enumerable.rb +86 -57
  212. data/lib/mongoid/reloading.rb +12 -14
  213. data/lib/mongoid/scoping.rb +329 -0
  214. data/lib/mongoid/serialization.rb +8 -27
  215. data/lib/mongoid/sessions.rb +359 -0
  216. data/lib/mongoid/sessions/factory.rb +106 -0
  217. data/lib/mongoid/sessions/mongo_uri.rb +93 -0
  218. data/lib/mongoid/sessions/validators.rb +2 -0
  219. data/lib/mongoid/sessions/validators/storage.rb +49 -0
  220. data/lib/mongoid/sharding.rb +6 -6
  221. data/lib/mongoid/state.rb +6 -7
  222. data/lib/mongoid/threaded.rb +167 -59
  223. data/lib/mongoid/threaded/lifecycle.rb +21 -22
  224. data/lib/mongoid/threaded/sessions.rb +0 -0
  225. data/lib/mongoid/timestamps.rb +1 -1
  226. data/lib/mongoid/timestamps/created.rb +8 -4
  227. data/lib/mongoid/timestamps/timeless.rb +6 -4
  228. data/lib/mongoid/timestamps/updated.rb +3 -3
  229. data/lib/mongoid/unit_of_work.rb +61 -0
  230. data/lib/mongoid/validations.rb +27 -19
  231. data/lib/mongoid/validations/associated.rb +2 -2
  232. data/lib/mongoid/validations/format.rb +2 -2
  233. data/lib/mongoid/validations/presence.rb +31 -5
  234. data/lib/mongoid/validations/uniqueness.rb +9 -12
  235. data/lib/mongoid/version.rb +2 -2
  236. data/lib/mongoid/versioning.rb +25 -26
  237. data/lib/rack/mongoid/middleware/identity_map.rb +3 -3
  238. data/lib/rails/generators/mongoid/config/config_generator.rb +1 -1
  239. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +59 -19
  240. data/lib/rails/generators/mongoid/model/model_generator.rb +7 -7
  241. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +2 -2
  242. data/lib/rails/generators/mongoid/observer/observer_generator.rb +4 -4
  243. data/lib/rails/generators/mongoid_generator.rb +5 -5
  244. data/lib/rails/mongoid.rb +69 -25
  245. metadata +110 -137
  246. checksums.yaml +0 -7
  247. data/lib/config/locales/bg.yml +0 -61
  248. data/lib/config/locales/de.yml +0 -61
  249. data/lib/config/locales/en-GB.yml +0 -64
  250. data/lib/config/locales/es.yml +0 -59
  251. data/lib/config/locales/fr.yml +0 -62
  252. data/lib/config/locales/hi.yml +0 -53
  253. data/lib/config/locales/hu.yml +0 -64
  254. data/lib/config/locales/id.yml +0 -62
  255. data/lib/config/locales/it.yml +0 -59
  256. data/lib/config/locales/ja.yml +0 -57
  257. data/lib/config/locales/kr.yml +0 -54
  258. data/lib/config/locales/nl.yml +0 -61
  259. data/lib/config/locales/pl.yml +0 -59
  260. data/lib/config/locales/pt-BR.yml +0 -60
  261. data/lib/config/locales/pt.yml +0 -60
  262. data/lib/config/locales/ro.yml +0 -66
  263. data/lib/config/locales/ru.yml +0 -61
  264. data/lib/config/locales/sv.yml +0 -60
  265. data/lib/config/locales/vi.yml +0 -62
  266. data/lib/config/locales/zh-CN.yml +0 -53
  267. data/lib/mongoid/collection.rb +0 -157
  268. data/lib/mongoid/collections.rb +0 -120
  269. data/lib/mongoid/collections/master.rb +0 -45
  270. data/lib/mongoid/collections/operations.rb +0 -44
  271. data/lib/mongoid/config/database.rb +0 -181
  272. data/lib/mongoid/config/replset_database.rb +0 -80
  273. data/lib/mongoid/contexts.rb +0 -25
  274. data/lib/mongoid/contexts/enumerable.rb +0 -313
  275. data/lib/mongoid/contexts/enumerable/sort.rb +0 -43
  276. data/lib/mongoid/contexts/mongo.rb +0 -487
  277. data/lib/mongoid/criterion/builder.rb +0 -34
  278. data/lib/mongoid/criterion/complex.rb +0 -84
  279. data/lib/mongoid/criterion/creational.rb +0 -34
  280. data/lib/mongoid/criterion/exclusion.rb +0 -110
  281. data/lib/mongoid/criterion/inclusion.rb +0 -290
  282. data/lib/mongoid/criterion/optional.rb +0 -259
  283. data/lib/mongoid/criterion/selector.rb +0 -177
  284. data/lib/mongoid/cursor.rb +0 -88
  285. data/lib/mongoid/default_scope.rb +0 -36
  286. data/lib/mongoid/errors/invalid_type.rb +0 -25
  287. data/lib/mongoid/extensions/array/deep_copy.rb +0 -25
  288. data/lib/mongoid/extensions/array/deletion.rb +0 -29
  289. data/lib/mongoid/extensions/false_class/equality.rb +0 -26
  290. data/lib/mongoid/extensions/hash/criteria_helpers.rb +0 -47
  291. data/lib/mongoid/extensions/hash/deep_copy.rb +0 -25
  292. data/lib/mongoid/extensions/hash/scoping.rb +0 -25
  293. data/lib/mongoid/extensions/integer/checks.rb +0 -23
  294. data/lib/mongoid/extensions/nil/collectionization.rb +0 -23
  295. data/lib/mongoid/extensions/object/checks.rb +0 -29
  296. data/lib/mongoid/extensions/object/deep_copy.rb +0 -21
  297. data/lib/mongoid/extensions/object/reflections.rb +0 -48
  298. data/lib/mongoid/extensions/object/substitutable.rb +0 -15
  299. data/lib/mongoid/extensions/object/yoda.rb +0 -44
  300. data/lib/mongoid/extensions/object_id/conversions.rb +0 -60
  301. data/lib/mongoid/extensions/proc/scoping.rb +0 -25
  302. data/lib/mongoid/extensions/string/checks.rb +0 -36
  303. data/lib/mongoid/extensions/string/conversions.rb +0 -22
  304. data/lib/mongoid/extensions/string/inflections.rb +0 -118
  305. data/lib/mongoid/extensions/symbol/checks.rb +0 -23
  306. data/lib/mongoid/extensions/symbol/inflections.rb +0 -67
  307. data/lib/mongoid/extensions/true_class/equality.rb +0 -26
  308. data/lib/mongoid/extras.rb +0 -31
  309. data/lib/mongoid/fields/internal/array.rb +0 -77
  310. data/lib/mongoid/fields/internal/big_decimal.rb +0 -63
  311. data/lib/mongoid/fields/internal/bignum.rb +0 -10
  312. data/lib/mongoid/fields/internal/binary.rb +0 -11
  313. data/lib/mongoid/fields/internal/boolean.rb +0 -58
  314. data/lib/mongoid/fields/internal/date.rb +0 -51
  315. data/lib/mongoid/fields/internal/date_time.rb +0 -28
  316. data/lib/mongoid/fields/internal/false_class.rb +0 -10
  317. data/lib/mongoid/fields/internal/fixnum.rb +0 -10
  318. data/lib/mongoid/fields/internal/float.rb +0 -47
  319. data/lib/mongoid/fields/internal/foreign_keys/array.rb +0 -88
  320. data/lib/mongoid/fields/internal/foreign_keys/object.rb +0 -56
  321. data/lib/mongoid/fields/internal/hash.rb +0 -11
  322. data/lib/mongoid/fields/internal/integer.rb +0 -59
  323. data/lib/mongoid/fields/internal/localized.rb +0 -62
  324. data/lib/mongoid/fields/internal/nil_class.rb +0 -53
  325. data/lib/mongoid/fields/internal/object.rb +0 -11
  326. data/lib/mongoid/fields/internal/object_id.rb +0 -46
  327. data/lib/mongoid/fields/internal/range.rb +0 -61
  328. data/lib/mongoid/fields/internal/set.rb +0 -57
  329. data/lib/mongoid/fields/internal/string.rb +0 -42
  330. data/lib/mongoid/fields/internal/symbol.rb +0 -43
  331. data/lib/mongoid/fields/internal/time.rb +0 -23
  332. data/lib/mongoid/fields/internal/time_with_zone.rb +0 -23
  333. data/lib/mongoid/fields/internal/timekeeping.rb +0 -122
  334. data/lib/mongoid/fields/internal/true_class.rb +0 -10
  335. data/lib/mongoid/fields/mappings.rb +0 -42
  336. data/lib/mongoid/fields/serializable.rb +0 -270
  337. data/lib/mongoid/identity.rb +0 -92
  338. data/lib/mongoid/keys.rb +0 -144
  339. data/lib/mongoid/logger.rb +0 -45
  340. data/lib/mongoid/multi_database.rb +0 -36
  341. data/lib/mongoid/named_scope.rb +0 -166
  342. data/lib/mongoid/relations/embedded/atomic.rb +0 -89
  343. data/lib/mongoid/relations/embedded/atomic/operation.rb +0 -63
  344. data/lib/mongoid/relations/embedded/atomic/pull.rb +0 -65
  345. data/lib/mongoid/relations/embedded/atomic/push_all.rb +0 -59
  346. data/lib/mongoid/relations/embedded/atomic/set.rb +0 -61
  347. data/lib/mongoid/relations/embedded/atomic/unset.rb +0 -41
  348. data/lib/mongoid/relations/referenced/batch.rb +0 -73
  349. data/lib/mongoid/relations/referenced/batch/insert.rb +0 -57
  350. data/lib/mongoid/safety.rb +0 -105
  351. data/lib/mongoid/scope.rb +0 -31
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
- module Mongoid #:nodoc:
3
- module Attributes #:nodoc:
2
+ module Mongoid
3
+ module Attributes
4
4
 
5
5
  # This module contains the behavior for processing attributes.
6
6
  module Processing
@@ -11,19 +11,21 @@ module Mongoid #:nodoc:
11
11
  # put into the document's attributes.
12
12
  #
13
13
  # @example Process the attributes.
14
- # person.process(:title => "sir", :age => 40)
14
+ # person.process_attributes(:title => "sir", :age => 40)
15
15
  #
16
16
  # @param [ Hash ] attrs The attributes to set.
17
17
  # @param [ Symbol ] role A role for scoped mass assignment.
18
18
  # @param [ Boolean ] guard_protected_attributes False to skip mass assignment protection.
19
19
  #
20
20
  # @since 2.0.0.rc.7
21
- def process(attrs = nil, role = :default, guard_protected_attributes = true)
21
+ def process_attributes(attrs = nil, role = :default, guard_protected_attributes = true)
22
22
  attrs ||= {}
23
- attrs = sanitize_for_mass_assignment(attrs, role) if guard_protected_attributes
24
- attrs.each_pair do |key, value|
25
- next if pending_attribute?(key, value)
26
- process_attribute(key, value)
23
+ if attrs.any?
24
+ attrs = sanitize_for_mass_assignment(attrs, role) if guard_protected_attributes
25
+ attrs.each_pair do |key, value|
26
+ next if pending_attribute?(key, value)
27
+ process_attribute(key, value)
28
+ end
27
29
  end
28
30
  yield self if block_given?
29
31
  process_pending
@@ -50,7 +52,7 @@ module Mongoid #:nodoc:
50
52
  pending_relations[name] = value
51
53
  return true
52
54
  end
53
- if nested_attributes.include?("#{name}=")
55
+ if nested_attributes.has_key?(name)
54
56
  pending_nested[name] = value
55
57
  return true
56
58
  end
@@ -92,9 +94,11 @@ module Mongoid #:nodoc:
92
94
  #
93
95
  # @since 2.0.0.rc.7
94
96
  def process_attribute(name, value)
95
- if Mongoid.allow_dynamic_fields && !respond_to?("#{name}=")
97
+ responds = respond_to?("#{name}=")
98
+ if Mongoid.allow_dynamic_fields && !responds
96
99
  write_attribute(name, value)
97
100
  else
101
+ raise Errors::UnknownAttribute.new(self.class, name) unless responds
98
102
  send("#{name}=", value)
99
103
  end
100
104
  end
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Attributes
4
+
5
+ # This module defines behaviour for readonly attributes.
6
+ module Readonly
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ class_attribute :readonly_attributes
11
+ self.readonly_attributes = ::Set.new
12
+ end
13
+
14
+ # Are we able to write the attribute with the provided name?
15
+ #
16
+ # @example Can we write the attribute?
17
+ # model.attribute_writable?(:title)
18
+ #
19
+ # @param [ String, Symbol ] name The name of the field.
20
+ #
21
+ # @return [ true, false ] If the document is new, or if the field is not
22
+ # readonly.
23
+ #
24
+ # @since 3.0.0
25
+ def attribute_writable?(name)
26
+ new_record? || !readonly_attributes.include?(name.to_s)
27
+ end
28
+
29
+ module ClassMethods
30
+
31
+ # Defines an attribute as readonly. This will ensure that the value for
32
+ # the attribute is only set when the document is new or we are
33
+ # creating. In other cases, the field write will be ignored with the
34
+ # exception of #remove_attribute and #update_attribute, where an error
35
+ # will get raised.
36
+ #
37
+ # @example Flag fields as readonly.
38
+ # class Band
39
+ # include Mongoid::Document
40
+ # field :name, type: String
41
+ # field :genre, type: String
42
+ # attr_readonly :name, :genre
43
+ # end
44
+ #
45
+ # @param [ Array<Symbol> ] names The names of the fields.
46
+ #
47
+ # @since 3.0.0
48
+ def attr_readonly(*names)
49
+ names.each do |name|
50
+ readonly_attributes << name.to_s
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,40 +1,74 @@
1
1
  # encoding: utf-8
2
- module Mongoid #:nodoc:
2
+ module Mongoid
3
3
 
4
4
  # This module contains all the callback hooks for Mongoid.
5
5
  module Callbacks
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  CALLBACKS = [
9
- :before_validation, :after_validation,
10
- :after_initialize, :after_build,
11
- :before_create, :around_create, :after_create,
12
- :before_destroy, :around_destroy, :after_destroy,
13
- :before_save, :around_save, :after_save,
14
- :before_update, :around_update, :after_update,
9
+ :after_build,
10
+ :after_create,
11
+ :after_destroy,
12
+ :after_initialize,
13
+ :after_save,
14
+ :after_update,
15
+ :after_validation,
16
+ :around_create,
17
+ :around_destroy,
18
+ :around_save,
19
+ :around_update,
20
+ :before_create,
21
+ :before_destroy,
22
+ :before_save,
23
+ :before_update,
24
+ :before_validation
15
25
  ]
16
26
 
17
27
  included do
18
28
  extend ActiveModel::Callbacks
19
29
  include ActiveModel::Validations::Callbacks
20
30
 
21
- define_model_callbacks :initialize, :only => :after
22
- define_model_callbacks :build, :only => :after
31
+ define_model_callbacks :initialize, only: :after
32
+ define_model_callbacks :build, only: :after
23
33
  define_model_callbacks :create, :destroy, :save, :update
24
34
  end
25
35
 
26
- # Is the provided type of callback executable by this document?
36
+ # Run only the after callbacks for the specific event.
27
37
  #
28
- # @example Is the callback executable?
29
- # document.callback_executable?(:save)
38
+ # @note ActiveSupport does not allow this type of behaviour by default, so
39
+ # Mongoid has to get around it and implement itself.
30
40
  #
31
- # @param [ Symbol ] kin The type of callback.
41
+ # @example Run only the after save callbacks.
42
+ # model.run_after_callbacks(:save)
32
43
  #
33
- # @return [ true, false ] If the callback can be executed.
44
+ # @param [ Array<Symbol> ] kinds The events that are occurring.
34
45
  #
35
- # @since 2.5.1
36
- def callback_executable?(kind)
37
- respond_to?("_#{kind}_callbacks")
46
+ # @return [ Object ] The result of the chain executing.
47
+ #
48
+ # @since 3.0.0
49
+ def run_after_callbacks(*kinds)
50
+ kinds.each do |kind|
51
+ run_targeted_callbacks(:after, kind)
52
+ end
53
+ end
54
+
55
+ # Run only the before callbacks for the specific event.
56
+ #
57
+ # @note ActiveSupport does not allow this type of behaviour by default, so
58
+ # Mongoid has to get around it and implement itself.
59
+ #
60
+ # @example Run only the before save callbacks.
61
+ # model.run_before_callbacks(:save, :create)
62
+ #
63
+ # @param [ Array<Symbol> ] kinds The events that are occurring.
64
+ #
65
+ # @return [ Object ] The result of the chain executing.
66
+ #
67
+ # @since 3.0.0
68
+ def run_before_callbacks(*kinds)
69
+ kinds.each do |kind|
70
+ run_targeted_callbacks(:before, kind)
71
+ end
38
72
  end
39
73
 
40
74
  # Run the callbacks for the document. This overrides active support's
@@ -58,7 +92,7 @@ module Mongoid #:nodoc:
58
92
  return false
59
93
  end
60
94
  end
61
- callback_executable?(kind) ? super(kind, *args, &block) : true
95
+ super(kind, *args, &block)
62
96
  end
63
97
 
64
98
  private
@@ -73,19 +107,21 @@ module Mongoid #:nodoc:
73
107
  # @return [ Array<Document> ] The children.
74
108
  #
75
109
  # @since 2.3.0
76
- def cascadable_children(kind)
77
- [].tap do |children|
78
- relations.each_pair do |name, metadata|
79
- next unless metadata.cascading_callbacks?
110
+ def cascadable_children(kind, children = Set.new)
111
+ embedded_relations.each_pair do |name, metadata|
112
+ next unless metadata.cascading_callbacks?
113
+ without_autobuild do
80
114
  delayed_pulls = delayed_atomic_pulls[name]
81
- children.concat(delayed_pulls) if delayed_pulls
82
- child = send(name)
83
- Array.wrap(child).each do |doc|
84
- children.push(doc) if cascadable_child?(kind, doc)
85
- children.concat(doc.send(:cascadable_children, kind))
115
+ children.merge(delayed_pulls) if delayed_pulls
116
+ relation = send(name)
117
+ Array.wrap(relation).each do |child|
118
+ next if children.include?(child)
119
+ children.add(child) if cascadable_child?(kind, child)
120
+ children.merge(child.send(:cascadable_children, kind, children))
86
121
  end
87
122
  end
88
123
  end
124
+ children.to_a
89
125
  end
90
126
 
91
127
  # Determine if the child should fire the callback.
@@ -127,5 +163,32 @@ module Mongoid #:nodoc:
127
163
  kind
128
164
  end
129
165
  end
166
+
167
+ # Run only the callbacks for the target location (before, after, around)
168
+ # and kind (save, update, create).
169
+ #
170
+ # @example Run the targeted callbacks.
171
+ # model.run_targeted_callbacks(:before, :save)
172
+ #
173
+ # @param [ Symbol ] place The time to run, :before, :after, :around.
174
+ # @param [ Symbol ] kind The type of callback, :save, :create, :update.
175
+ #
176
+ # @return [ Object ] The result of the chain execution.
177
+ #
178
+ # @since 3.0.0
179
+ def run_targeted_callbacks(place, kind)
180
+ name = "_run__#{place}__#{kind}__callbacks"
181
+ unless respond_to?(name)
182
+ chain = ActiveSupport::Callbacks::CallbackChain.new(name, {})
183
+ send("_#{kind}_callbacks").each do |callback|
184
+ chain.push(callback) if callback.kind == place
185
+ end
186
+ class_eval <<-EOM
187
+ def #{name}() #{chain.compile} end
188
+ protected :#{name}
189
+ EOM
190
+ end
191
+ send(name)
192
+ end
130
193
  end
131
194
  end
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
- module Mongoid #:nodoc:
3
- module Collections #:nodoc:
2
+ module Mongoid
3
+ module Collections
4
4
 
5
5
  # Provides behaviour for retrying commands on connection failure.
6
6
  module Retry
@@ -32,7 +32,6 @@ module Mongoid #:nodoc:
32
32
  retry
33
33
  rescue Mongo::OperationFailure => ex
34
34
  if ex.message =~ /not master/
35
- Mongoid.reconnect!
36
35
  retries = increase_retry_attempts(retries, ex)
37
36
  retry
38
37
  else
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
- module Mongoid #:nodoc
3
- module Components #:nodoc
2
+ module Mongoid
3
+ module Components
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  # All modules that a +Document+ is composed of are defined in this
@@ -21,22 +21,19 @@ module Mongoid #:nodoc
21
21
  include Mongoid::Atomic
22
22
  include Mongoid::Dirty
23
23
  include Mongoid::Attributes
24
- include Mongoid::Collections
25
- include Mongoid::DefaultScope
26
- include Mongoid::Extras
24
+ include Mongoid::Copyable
27
25
  include Mongoid::Fields
28
26
  include Mongoid::Hierarchy
29
27
  include Mongoid::Indexes
30
28
  include Mongoid::Inspection
31
29
  include Mongoid::JSON
32
- include Mongoid::Keys
33
30
  include Mongoid::Matchers
34
- include Mongoid::NamedScope
35
31
  include Mongoid::NestedAttributes
36
32
  include Mongoid::Persistence
37
33
  include Mongoid::Relations
38
34
  include Mongoid::Reloading
39
- include Mongoid::Safety
35
+ include Mongoid::Scoping
36
+ include Mongoid::Sessions
40
37
  include Mongoid::Serialization
41
38
  include Mongoid::Sharding
42
39
  include Mongoid::State
@@ -44,36 +41,29 @@ module Mongoid #:nodoc
44
41
  include Mongoid::Timestamps::Timeless
45
42
  include Mongoid::Validations
46
43
  include Mongoid::Callbacks
47
- include Mongoid::Copyable
48
- include Mongoid::MultiDatabase
49
44
 
50
45
  MODULES = [
51
46
  Mongoid::Atomic,
52
47
  Mongoid::Attributes,
53
- Mongoid::Collections,
54
48
  Mongoid::Copyable,
55
- Mongoid::DefaultScope,
56
49
  Mongoid::Dirty,
57
- Mongoid::Extras,
58
50
  Mongoid::Fields,
59
51
  Mongoid::Hierarchy,
60
52
  Mongoid::Indexes,
61
53
  Mongoid::Inspection,
62
54
  Mongoid::JSON,
63
- Mongoid::Keys,
55
+ Mongoid::Loggable,
64
56
  Mongoid::Matchers,
65
- Mongoid::NamedScope,
66
57
  Mongoid::NestedAttributes,
67
58
  Mongoid::Persistence,
68
59
  Mongoid::Relations,
69
60
  Mongoid::Relations::Proxy,
70
- Mongoid::Safety,
61
+ Mongoid::Scoping,
71
62
  Mongoid::Serialization,
72
63
  Mongoid::Sharding,
73
64
  Mongoid::State,
74
65
  Mongoid::Validations,
75
- Mongoid::Callbacks,
76
- Mongoid::MultiDatabase,
66
+ Mongoid::Callbacks
77
67
  ]
78
68
 
79
69
  class << self
@@ -88,11 +78,9 @@ module Mongoid #:nodoc
88
78
  #
89
79
  # @since 2.1.8
90
80
  def prohibited_methods
91
- @prohibited_methods ||= MODULES.inject([]) do |methods, mod|
92
- methods.tap do |mets|
93
- mets << mod.instance_methods.map{ |m| m.to_sym }
94
- end
95
- end.flatten
81
+ @prohibited_methods ||= MODULES.flat_map do |mod|
82
+ mod.instance_methods.map{ |m| m.to_sym }
83
+ end
96
84
  end
97
85
  end
98
86
  end
@@ -1,90 +1,29 @@
1
1
  # encoding: utf-8
2
- require "uri"
3
- require "mongoid/config/database"
4
2
  require "mongoid/config/environment"
5
- require "mongoid/config/replset_database"
3
+ require "mongoid/config/inflections"
6
4
  require "mongoid/config/options"
5
+ require "mongoid/config/validators"
7
6
 
8
- module Mongoid #:nodoc
7
+ module Mongoid
9
8
 
10
9
  # This module defines all the configuration options for Mongoid, including the
11
10
  # database connections.
12
- #
13
- # @todo Durran: This module needs an overhaul, remove singleton, etc.
14
11
  module Config
15
12
  extend self
16
13
  extend Options
17
14
  include ActiveModel::Observing
18
15
 
19
- # @attribute [rw] master The master database.
20
- attr_accessor :master
21
-
22
- option :allow_dynamic_fields, :default => true
23
- option :autocreate_indexes, :default => false
24
- option :identity_map_enabled, :default => false
25
- option :include_root_in_json, :default => false
26
- option :include_type_for_serialization, :default => false
27
- option :max_retries_on_connection_failure, :default => 0
28
- option :parameterize_keys, :default => true
29
- option :scope_overwrite_exception, :default => false
30
- option :persist_in_safe_mode, :default => false
31
- option :preload_models, :default => false
32
- option :raise_not_found_error, :default => true
33
- option :skip_version_check, :default => false
34
- option :time_zone, :default => nil
35
- option :use_utc, :default => false
36
-
37
- # Adds a new I18n locale file to the load path.
38
- #
39
- # @example Add a portuguese locale.
40
- # Mongoid::Config.add_language('pt')
41
- #
42
- # @example Add all available languages.
43
- # Mongoid::Config.add_language('*')
44
- #
45
- # @param [ String ] language_code The language to add.
46
- def add_language(language_code = nil)
47
- Dir[
48
- File.join(
49
- File.dirname(__FILE__), "..", "config", "locales", "#{language_code}.yml"
50
- )
51
- ].each do |file|
52
- I18n.load_path << File.expand_path(file)
53
- end
54
- end
55
-
56
- # keys to remove from self to not pass through to Mongo::Connection
57
- PRIVATE_OPTIONS =
58
- %w(uri host hosts port database databases username
59
- password logger use_activesupport_time_zone)
60
-
61
- # Get the blacklisted options from passing through to the driver.
62
- #
63
- # @example Get the blacklisted options.
64
- # Config.blacklisted_options
65
- #
66
- # @return [ Array<String> ] The blacklist.
67
- #
68
- # @since 2.4.7
69
- def blacklisted_options
70
- PRIVATE_OPTIONS + settings.keys.map(&:to_s)
71
- end
72
-
73
- # Get any extra databases that have been configured.
74
- #
75
- # @example Get the extras.
76
- # config.databases
77
- #
78
- # @return [ Hash ] A hash of secondary databases.
79
- def databases
80
- configure_extras(@settings["databases"]) unless @databases || !@settings
81
- @databases || {}
82
- end
83
-
84
- # @todo Durran: There were no tests around the databases setter, not sure
85
- # what the exact expectation was. Set with a hash?
86
- def databases=(databases)
87
- end
16
+ option :allow_dynamic_fields, default: true
17
+ option :identity_map_enabled, default: false
18
+ option :include_root_in_json, default: false
19
+ option :include_type_for_serialization, default: false
20
+ option :preload_models, default: false
21
+ option :protect_sensitive_fields, default: true
22
+ option :raise_not_found_error, default: true
23
+ option :scope_overwrite_exception, default: false
24
+ option :skip_version_check, default: false
25
+ option :use_activesupport_time_zone, default: true
26
+ option :use_utc, default: false
88
27
 
89
28
  # Return field names that could cause destructive things to happen if
90
29
  # defined in a Mongoid::Document.
@@ -97,21 +36,6 @@ module Mongoid #:nodoc
97
36
  Components.prohibited_methods
98
37
  end
99
38
 
100
- # Configure mongoid from a hash. This is usually called after parsing a
101
- # yaml config file such as mongoid.yml.
102
- #
103
- # @example Configure Mongoid.
104
- # config.from_hash({})
105
- #
106
- # @param [ Hash ] options The settings to use.
107
- def from_hash(options = {})
108
- options.except("database", "slaves", "databases").each_pair do |name, value|
109
- send("#{name}=", value) if respond_to?("#{name}=")
110
- end
111
- @master, @slaves = configure_databases(options)
112
- configure_extras(options["databases"])
113
- end
114
-
115
39
  # Load the settings from a compliant mongoid.yml file. This can be used for
116
40
  # easy setup with frameworks other than Rails.
117
41
  #
@@ -119,48 +43,32 @@ module Mongoid #:nodoc
119
43
  # Mongoid.load!("/path/to/mongoid.yml")
120
44
  #
121
45
  # @param [ String ] path The path to the file.
46
+ # @param [ String, Symbol ] environment The environment to load.
122
47
  #
123
48
  # @since 2.0.1
124
- def load!(path)
125
- Environment.load_yaml(path).tap do |settings|
126
- from_hash(settings) if settings.present?
127
- end
128
- end
129
-
130
- # Returns the default logger, which is either a Rails logger of stdout logger
131
- #
132
- # @example Get the default logger
133
- # config.default_logger
134
- #
135
- # @return [ Logger ] The default Logger instance.
136
- def default_logger
137
- defined?(Rails) && Rails.respond_to?(:logger) ? Rails.logger : ::Logger.new($stdout)
49
+ def load!(path, environment = nil)
50
+ settings = Environment.load_yaml(path, environment)
51
+ load_configuration(settings) if settings.present?
52
+ settings
138
53
  end
139
54
 
140
- # Returns the logger, or defaults to Rails logger or stdout logger.
55
+ # Connect to the provided database name on the default session.
141
56
  #
142
- # @example Get the logger.
143
- # config.logger
57
+ # @note Use only in development or test environments for convenience.
144
58
  #
145
- # @return [ Logger ] The configured logger or a default Logger instance.
146
- def logger
147
- @logger = default_logger unless defined?(@logger)
148
- @logger
149
- end
150
-
151
- # Sets the logger for Mongoid to use.
59
+ # @example Set the database to connect to.
60
+ # config.connect_to("mongoid_test")
152
61
  #
153
- # @example Set the logger.
154
- # config.logger = Logger.new($stdout, :warn)
62
+ # @param [ String ] name The database name.
155
63
  #
156
- # @return [ Logger ] The newly set logger.
157
- def logger=(logger)
158
- case logger
159
- when false, nil then @logger = nil
160
- when true then @logger = default_logger
161
- else
162
- @logger = logger if logger.respond_to?(:info)
163
- end
64
+ # @since 3.0.0
65
+ def connect_to(name)
66
+ self.sessions = {
67
+ default: {
68
+ database: name,
69
+ hosts: [ "localhost:27017" ]
70
+ }
71
+ }
164
72
  end
165
73
 
166
74
  # Purge all data in all collections, including indexes.
@@ -168,143 +76,89 @@ module Mongoid #:nodoc
168
76
  # @example Purge all data.
169
77
  # Mongoid::Config.purge!
170
78
  #
79
+ # @return [ true ] true.
80
+ #
171
81
  # @since 2.0.2
172
82
  def purge!
173
- master.collections.map do |collection|
174
- collection.drop if collection.name !~ /system/
175
- end
83
+ session = Sessions.default
84
+ collections = session["system.namespaces"].find(name: { "$not" => /system|\$/ }).to_a
85
+ collections.each do |collection|
86
+ _, name = collection["name"].split(".", 2)
87
+ session[name].drop
88
+ end and true
176
89
  end
177
90
 
178
- # Sets whether the times returned from the database use the ruby or
179
- # the ActiveSupport time zone.
91
+ # Set the configuration options. Will validate each one individually.
180
92
  #
181
- # @note If you omit this setting, then times will use the ruby time zone.
93
+ # @example Set the options.
94
+ # config.options = { raise_not_found_error: true }
182
95
  #
183
- # @example Set the time zone config.
184
- # Config.use_activesupport_time_zone = true
96
+ # @param [ Hash ] options The configuration options.
185
97
  #
186
- # @param [ true, false ] value Whether to use Active Support time zones.
187
- #
188
- # @return [ true, false ] The supplied value or false if nil.
189
- def use_activesupport_time_zone=(value)
190
- @use_activesupport_time_zone = value || false
98
+ # @since 3.0.0
99
+ def options=(options)
100
+ if options
101
+ options.each_pair do |option, value|
102
+ Validators::Option.validate(option)
103
+ send("#{option}=", value)
104
+ end
105
+ end
191
106
  end
192
- attr_reader :use_activesupport_time_zone
193
- alias_method :use_activesupport_time_zone?, :use_activesupport_time_zone
194
107
 
195
- # Sets the Mongo::DB master database to be used. If the object trying to be
196
- # set is not a valid +Mongo::DB+, then an error will be raised.
197
- #
198
- # @example Set the master database.
199
- # config.master = Mongo::Connection.new.db("test")
108
+ # Get the session configuration or an empty hash.
200
109
  #
201
- # @param [ Mongo::DB ] db The master database.
110
+ # @example Get the sessions configuration.
111
+ # config.sessions
202
112
  #
203
- # @raise [ Errors::InvalidDatabase ] If the master isnt a valid object.
113
+ # @return [ Hash ] The sessions configuration.
204
114
  #
205
- # @return [ Mongo::DB ] The master instance.
206
- def master=(db)
207
- check_database!(db)
208
- @master = db
115
+ # @since 3.0.0
116
+ def sessions
117
+ @sessions ||= {}
209
118
  end
210
- alias :database= :master=
211
119
 
212
- # Returns the master database, or if none has been set it will raise an
213
- # error.
120
+ # Set the session configuration options.
214
121
  #
215
- # @example Get the master database.
216
- # config.master
217
- #
218
- # @raise [ Errors::InvalidDatabase ] If the database was not set.
219
- #
220
- # @return [ Mongo::DB ] The master database.
221
- def master
222
- unless @master
223
- @master, @slaves = configure_databases(@settings) unless @settings.blank?
224
- raise Errors::InvalidDatabase.new(nil) unless @master
225
- end
226
- if @reconnect
227
- @reconnect = false
228
- reconnect!
229
- end
230
- @master
231
- end
232
- alias :database :master
233
-
234
- # Convenience method for connecting to the master database after forking a
235
- # new process.
122
+ # @example Set the session configuration options.
123
+ # config.sessions = { default: { hosts: [ "localhost:27017" ] }}
236
124
  #
237
- # @example Reconnect to the master.
238
- # Mongoid.reconnect!
125
+ # @param [ Hash ] sessions The configuration options.
239
126
  #
240
- # @param [ true, false ] now Perform the reconnection immediately?
241
- def reconnect!(now = true)
242
- if now
243
- master.connection.connect
244
- else
245
- # We set a @reconnect flag so that #master knows to reconnect the next
246
- # time the connection is accessed.
247
- @reconnect = true
248
- end
127
+ # @since 3.0.0
128
+ def sessions=(sessions)
129
+ raise Errors::NoSessionsConfig.new unless sessions
130
+ sess = sessions.with_indifferent_access
131
+ Validators::Session.validate(sess)
132
+ @sessions = sess
133
+ sess
249
134
  end
250
135
 
251
- protected
252
-
253
- # Check if the database is valid and the correct version.
136
+ # Get the time zone to use.
254
137
  #
255
- # @example Check if the database is valid.
256
- # config.check_database!
138
+ # @example Get the time zone.
139
+ # Config.time_zone
257
140
  #
258
- # @param [ Mongo::DB ] database The db to check.
141
+ # @return [ String ] The time zone.
259
142
  #
260
- # @raise [ Errors::InvalidDatabase ] If the object is not valid.
261
- # @raise [ Errors::UnsupportedVersion ] If the db version is too old.
262
- def check_database!(database)
263
- raise Errors::InvalidDatabase.new(database) unless database.kind_of?(Mongo::DB)
264
- unless skip_version_check
265
- version = database.connection.server_version
266
- raise Errors::UnsupportedVersion.new(version) if version < Mongoid::MONGODB_VERSION
267
- end
143
+ # @since 3.0.0
144
+ def time_zone
145
+ use_utc? ? "UTC" : ::Time.zone
268
146
  end
269
147
 
270
- # Get a database from settings.
271
- #
272
- # @example Configure the master and slave dbs.
273
- # config.configure_databases("database" => "mongoid")
274
- #
275
- # @param [ Hash ] options The options to use.
276
- #
277
- # @option options [ String ] :database The database name.
278
- # @option options [ String ] :host The database host.
279
- # @option options [ String ] :password The password for authentication.
280
- # @option options [ Integer ] :port The port for the database.
281
- # @option options [ Array<Hash> ] :slaves The slave db options.
282
- # @option options [ String ] :uri The uri for the database.
283
- # @option options [ String ] :username The user for authentication.
284
- #
285
- # @since 2.0.0.rc.1
286
- def configure_databases(options)
287
- if options.has_key?('hosts')
288
- ReplsetDatabase.new(options).configure
289
- else
290
- Database.new(options).configure
291
- end
292
- end
148
+ private
293
149
 
294
- # Get the secondary databases from settings.
150
+ # From a hash of settings, load all the configuration.
295
151
  #
296
- # @example Configure the master and slave dbs.
297
- # config.configure_extras("databases" => settings)
152
+ # @example Load the configuration.
153
+ # config.load_configuration(settings)
298
154
  #
299
- # @param [ Hash ] options The options to use.
155
+ # @param [ Hash ] settings The configuration settings.
300
156
  #
301
- # @since 2.0.0.rc.1
302
- def configure_extras(extras)
303
- @databases = (extras || []).inject({}) do |dbs, (name, options)|
304
- dbs.tap do |extra|
305
- dbs[name], dbs["#{name}_slaves"] = configure_databases(options)
306
- end
307
- end
157
+ # @since 3.0.0
158
+ def load_configuration(settings)
159
+ configuration = settings.with_indifferent_access
160
+ self.options = configuration[:options]
161
+ self.sessions = configuration[:sessions]
308
162
  end
309
163
  end
310
164
  end