mongoid-multi-db 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (276) hide show
  1. data/CHANGELOG.md +615 -0
  2. data/LICENSE +20 -0
  3. data/README.md +62 -0
  4. data/Rakefile +49 -0
  5. data/lib/config/locales/bg.yml +54 -0
  6. data/lib/config/locales/de.yml +54 -0
  7. data/lib/config/locales/en-GB.yml +55 -0
  8. data/lib/config/locales/en.yml +55 -0
  9. data/lib/config/locales/es.yml +52 -0
  10. data/lib/config/locales/fr.yml +55 -0
  11. data/lib/config/locales/hi.yml +46 -0
  12. data/lib/config/locales/hu.yml +57 -0
  13. data/lib/config/locales/id.yml +55 -0
  14. data/lib/config/locales/it.yml +52 -0
  15. data/lib/config/locales/ja.yml +50 -0
  16. data/lib/config/locales/kr.yml +47 -0
  17. data/lib/config/locales/nl.yml +52 -0
  18. data/lib/config/locales/pl.yml +52 -0
  19. data/lib/config/locales/pt-BR.yml +53 -0
  20. data/lib/config/locales/pt.yml +53 -0
  21. data/lib/config/locales/ro.yml +59 -0
  22. data/lib/config/locales/ru.yml +54 -0
  23. data/lib/config/locales/sv.yml +53 -0
  24. data/lib/config/locales/vi.yml +55 -0
  25. data/lib/config/locales/zh-CN.yml +46 -0
  26. data/lib/mongoid.rb +148 -0
  27. data/lib/mongoid/atomic.rb +230 -0
  28. data/lib/mongoid/atomic/modifiers.rb +243 -0
  29. data/lib/mongoid/atomic/paths.rb +3 -0
  30. data/lib/mongoid/atomic/paths/embedded.rb +43 -0
  31. data/lib/mongoid/atomic/paths/embedded/many.rb +44 -0
  32. data/lib/mongoid/atomic/paths/embedded/one.rb +43 -0
  33. data/lib/mongoid/atomic/paths/root.rb +40 -0
  34. data/lib/mongoid/attributes.rb +234 -0
  35. data/lib/mongoid/attributes/processing.rb +146 -0
  36. data/lib/mongoid/callbacks.rb +135 -0
  37. data/lib/mongoid/collection.rb +153 -0
  38. data/lib/mongoid/collection_proxy.rb +59 -0
  39. data/lib/mongoid/collections.rb +120 -0
  40. data/lib/mongoid/collections/master.rb +45 -0
  41. data/lib/mongoid/collections/operations.rb +44 -0
  42. data/lib/mongoid/collections/retry.rb +46 -0
  43. data/lib/mongoid/components.rb +96 -0
  44. data/lib/mongoid/config.rb +347 -0
  45. data/lib/mongoid/config/database.rb +186 -0
  46. data/lib/mongoid/config/replset_database.rb +82 -0
  47. data/lib/mongoid/connection_proxy.rb +30 -0
  48. data/lib/mongoid/contexts.rb +25 -0
  49. data/lib/mongoid/contexts/enumerable.rb +288 -0
  50. data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
  51. data/lib/mongoid/contexts/mongo.rb +409 -0
  52. data/lib/mongoid/copyable.rb +48 -0
  53. data/lib/mongoid/criteria.rb +418 -0
  54. data/lib/mongoid/criterion/builder.rb +34 -0
  55. data/lib/mongoid/criterion/complex.rb +84 -0
  56. data/lib/mongoid/criterion/creational.rb +34 -0
  57. data/lib/mongoid/criterion/exclusion.rb +108 -0
  58. data/lib/mongoid/criterion/inclusion.rb +305 -0
  59. data/lib/mongoid/criterion/inspection.rb +22 -0
  60. data/lib/mongoid/criterion/optional.rb +232 -0
  61. data/lib/mongoid/criterion/selector.rb +153 -0
  62. data/lib/mongoid/cursor.rb +86 -0
  63. data/lib/mongoid/database_proxy.rb +97 -0
  64. data/lib/mongoid/default_scope.rb +36 -0
  65. data/lib/mongoid/dirty.rb +110 -0
  66. data/lib/mongoid/document.rb +280 -0
  67. data/lib/mongoid/errors.rb +17 -0
  68. data/lib/mongoid/errors/callback.rb +26 -0
  69. data/lib/mongoid/errors/document_not_found.rb +28 -0
  70. data/lib/mongoid/errors/eager_load.rb +25 -0
  71. data/lib/mongoid/errors/invalid_collection.rb +18 -0
  72. data/lib/mongoid/errors/invalid_database.rb +19 -0
  73. data/lib/mongoid/errors/invalid_field.rb +18 -0
  74. data/lib/mongoid/errors/invalid_find.rb +19 -0
  75. data/lib/mongoid/errors/invalid_options.rb +28 -0
  76. data/lib/mongoid/errors/invalid_time.rb +25 -0
  77. data/lib/mongoid/errors/invalid_type.rb +25 -0
  78. data/lib/mongoid/errors/mixed_relations.rb +37 -0
  79. data/lib/mongoid/errors/mongoid_error.rb +26 -0
  80. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +20 -0
  81. data/lib/mongoid/errors/unsaved_document.rb +23 -0
  82. data/lib/mongoid/errors/unsupported_version.rb +20 -0
  83. data/lib/mongoid/errors/validations.rb +23 -0
  84. data/lib/mongoid/extensions.rb +82 -0
  85. data/lib/mongoid/extensions/array/deletion.rb +29 -0
  86. data/lib/mongoid/extensions/false_class/equality.rb +26 -0
  87. data/lib/mongoid/extensions/hash/criteria_helpers.rb +45 -0
  88. data/lib/mongoid/extensions/hash/scoping.rb +25 -0
  89. data/lib/mongoid/extensions/integer/checks.rb +23 -0
  90. data/lib/mongoid/extensions/nil/collectionization.rb +23 -0
  91. data/lib/mongoid/extensions/object/checks.rb +29 -0
  92. data/lib/mongoid/extensions/object/reflections.rb +48 -0
  93. data/lib/mongoid/extensions/object/substitutable.rb +15 -0
  94. data/lib/mongoid/extensions/object/yoda.rb +44 -0
  95. data/lib/mongoid/extensions/object_id/conversions.rb +60 -0
  96. data/lib/mongoid/extensions/proc/scoping.rb +25 -0
  97. data/lib/mongoid/extensions/string/checks.rb +36 -0
  98. data/lib/mongoid/extensions/string/conversions.rb +22 -0
  99. data/lib/mongoid/extensions/string/inflections.rb +118 -0
  100. data/lib/mongoid/extensions/symbol/checks.rb +23 -0
  101. data/lib/mongoid/extensions/symbol/inflections.rb +66 -0
  102. data/lib/mongoid/extensions/true_class/equality.rb +26 -0
  103. data/lib/mongoid/extras.rb +31 -0
  104. data/lib/mongoid/factory.rb +46 -0
  105. data/lib/mongoid/fields.rb +332 -0
  106. data/lib/mongoid/fields/mappings.rb +41 -0
  107. data/lib/mongoid/fields/serializable.rb +201 -0
  108. data/lib/mongoid/fields/serializable/array.rb +49 -0
  109. data/lib/mongoid/fields/serializable/big_decimal.rb +42 -0
  110. data/lib/mongoid/fields/serializable/bignum.rb +10 -0
  111. data/lib/mongoid/fields/serializable/binary.rb +11 -0
  112. data/lib/mongoid/fields/serializable/boolean.rb +43 -0
  113. data/lib/mongoid/fields/serializable/date.rb +51 -0
  114. data/lib/mongoid/fields/serializable/date_time.rb +28 -0
  115. data/lib/mongoid/fields/serializable/fixnum.rb +10 -0
  116. data/lib/mongoid/fields/serializable/float.rb +32 -0
  117. data/lib/mongoid/fields/serializable/foreign_keys/array.rb +42 -0
  118. data/lib/mongoid/fields/serializable/foreign_keys/object.rb +47 -0
  119. data/lib/mongoid/fields/serializable/hash.rb +11 -0
  120. data/lib/mongoid/fields/serializable/integer.rb +44 -0
  121. data/lib/mongoid/fields/serializable/localized.rb +41 -0
  122. data/lib/mongoid/fields/serializable/nil_class.rb +38 -0
  123. data/lib/mongoid/fields/serializable/object.rb +11 -0
  124. data/lib/mongoid/fields/serializable/object_id.rb +31 -0
  125. data/lib/mongoid/fields/serializable/range.rb +42 -0
  126. data/lib/mongoid/fields/serializable/set.rb +42 -0
  127. data/lib/mongoid/fields/serializable/string.rb +27 -0
  128. data/lib/mongoid/fields/serializable/symbol.rb +27 -0
  129. data/lib/mongoid/fields/serializable/time.rb +23 -0
  130. data/lib/mongoid/fields/serializable/time_with_zone.rb +23 -0
  131. data/lib/mongoid/fields/serializable/timekeeping.rb +106 -0
  132. data/lib/mongoid/finders.rb +152 -0
  133. data/lib/mongoid/hierarchy.rb +120 -0
  134. data/lib/mongoid/identity.rb +92 -0
  135. data/lib/mongoid/identity_map.rb +119 -0
  136. data/lib/mongoid/indexes.rb +54 -0
  137. data/lib/mongoid/inspection.rb +54 -0
  138. data/lib/mongoid/javascript.rb +20 -0
  139. data/lib/mongoid/javascript/functions.yml +63 -0
  140. data/lib/mongoid/json.rb +16 -0
  141. data/lib/mongoid/keys.rb +144 -0
  142. data/lib/mongoid/logger.rb +39 -0
  143. data/lib/mongoid/matchers.rb +32 -0
  144. data/lib/mongoid/matchers/all.rb +21 -0
  145. data/lib/mongoid/matchers/and.rb +30 -0
  146. data/lib/mongoid/matchers/default.rb +70 -0
  147. data/lib/mongoid/matchers/exists.rb +23 -0
  148. data/lib/mongoid/matchers/gt.rb +21 -0
  149. data/lib/mongoid/matchers/gte.rb +21 -0
  150. data/lib/mongoid/matchers/in.rb +21 -0
  151. data/lib/mongoid/matchers/lt.rb +21 -0
  152. data/lib/mongoid/matchers/lte.rb +21 -0
  153. data/lib/mongoid/matchers/ne.rb +21 -0
  154. data/lib/mongoid/matchers/nin.rb +21 -0
  155. data/lib/mongoid/matchers/or.rb +33 -0
  156. data/lib/mongoid/matchers/size.rb +21 -0
  157. data/lib/mongoid/matchers/strategies.rb +93 -0
  158. data/lib/mongoid/multi_database.rb +31 -0
  159. data/lib/mongoid/multi_parameter_attributes.rb +106 -0
  160. data/lib/mongoid/named_scope.rb +146 -0
  161. data/lib/mongoid/nested_attributes.rb +54 -0
  162. data/lib/mongoid/observer.rb +170 -0
  163. data/lib/mongoid/paranoia.rb +158 -0
  164. data/lib/mongoid/persistence.rb +264 -0
  165. data/lib/mongoid/persistence/atomic.rb +223 -0
  166. data/lib/mongoid/persistence/atomic/add_to_set.rb +35 -0
  167. data/lib/mongoid/persistence/atomic/bit.rb +37 -0
  168. data/lib/mongoid/persistence/atomic/inc.rb +31 -0
  169. data/lib/mongoid/persistence/atomic/operation.rb +85 -0
  170. data/lib/mongoid/persistence/atomic/pop.rb +34 -0
  171. data/lib/mongoid/persistence/atomic/pull.rb +34 -0
  172. data/lib/mongoid/persistence/atomic/pull_all.rb +34 -0
  173. data/lib/mongoid/persistence/atomic/push.rb +31 -0
  174. data/lib/mongoid/persistence/atomic/push_all.rb +31 -0
  175. data/lib/mongoid/persistence/atomic/rename.rb +31 -0
  176. data/lib/mongoid/persistence/atomic/sets.rb +30 -0
  177. data/lib/mongoid/persistence/atomic/unset.rb +28 -0
  178. data/lib/mongoid/persistence/deletion.rb +32 -0
  179. data/lib/mongoid/persistence/insertion.rb +41 -0
  180. data/lib/mongoid/persistence/modification.rb +37 -0
  181. data/lib/mongoid/persistence/operations.rb +211 -0
  182. data/lib/mongoid/persistence/operations/embedded/insert.rb +42 -0
  183. data/lib/mongoid/persistence/operations/embedded/remove.rb +40 -0
  184. data/lib/mongoid/persistence/operations/insert.rb +34 -0
  185. data/lib/mongoid/persistence/operations/remove.rb +33 -0
  186. data/lib/mongoid/persistence/operations/update.rb +64 -0
  187. data/lib/mongoid/railtie.rb +126 -0
  188. data/lib/mongoid/railties/database.rake +182 -0
  189. data/lib/mongoid/railties/document.rb +12 -0
  190. data/lib/mongoid/relations.rb +144 -0
  191. data/lib/mongoid/relations/accessors.rb +138 -0
  192. data/lib/mongoid/relations/auto_save.rb +38 -0
  193. data/lib/mongoid/relations/binding.rb +26 -0
  194. data/lib/mongoid/relations/bindings.rb +9 -0
  195. data/lib/mongoid/relations/bindings/embedded/in.rb +69 -0
  196. data/lib/mongoid/relations/bindings/embedded/many.rb +93 -0
  197. data/lib/mongoid/relations/bindings/embedded/one.rb +61 -0
  198. data/lib/mongoid/relations/bindings/referenced/in.rb +76 -0
  199. data/lib/mongoid/relations/bindings/referenced/many.rb +54 -0
  200. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +51 -0
  201. data/lib/mongoid/relations/bindings/referenced/one.rb +58 -0
  202. data/lib/mongoid/relations/builder.rb +57 -0
  203. data/lib/mongoid/relations/builders.rb +83 -0
  204. data/lib/mongoid/relations/builders/embedded/in.rb +29 -0
  205. data/lib/mongoid/relations/builders/embedded/many.rb +40 -0
  206. data/lib/mongoid/relations/builders/embedded/one.rb +30 -0
  207. data/lib/mongoid/relations/builders/nested_attributes/many.rb +110 -0
  208. data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
  209. data/lib/mongoid/relations/builders/referenced/in.rb +26 -0
  210. data/lib/mongoid/relations/builders/referenced/many.rb +27 -0
  211. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +38 -0
  212. data/lib/mongoid/relations/builders/referenced/one.rb +26 -0
  213. data/lib/mongoid/relations/cascading.rb +56 -0
  214. data/lib/mongoid/relations/cascading/delete.rb +19 -0
  215. data/lib/mongoid/relations/cascading/destroy.rb +26 -0
  216. data/lib/mongoid/relations/cascading/nullify.rb +18 -0
  217. data/lib/mongoid/relations/cascading/strategy.rb +26 -0
  218. data/lib/mongoid/relations/constraint.rb +42 -0
  219. data/lib/mongoid/relations/conversions.rb +35 -0
  220. data/lib/mongoid/relations/cyclic.rb +103 -0
  221. data/lib/mongoid/relations/embedded/atomic.rb +89 -0
  222. data/lib/mongoid/relations/embedded/atomic/operation.rb +63 -0
  223. data/lib/mongoid/relations/embedded/atomic/pull.rb +65 -0
  224. data/lib/mongoid/relations/embedded/atomic/push_all.rb +59 -0
  225. data/lib/mongoid/relations/embedded/atomic/set.rb +61 -0
  226. data/lib/mongoid/relations/embedded/atomic/unset.rb +41 -0
  227. data/lib/mongoid/relations/embedded/in.rb +220 -0
  228. data/lib/mongoid/relations/embedded/many.rb +560 -0
  229. data/lib/mongoid/relations/embedded/one.rb +206 -0
  230. data/lib/mongoid/relations/embedded/sort.rb +31 -0
  231. data/lib/mongoid/relations/macros.rb +310 -0
  232. data/lib/mongoid/relations/many.rb +135 -0
  233. data/lib/mongoid/relations/metadata.rb +919 -0
  234. data/lib/mongoid/relations/nested_builder.rb +75 -0
  235. data/lib/mongoid/relations/one.rb +36 -0
  236. data/lib/mongoid/relations/options.rb +47 -0
  237. data/lib/mongoid/relations/polymorphic.rb +40 -0
  238. data/lib/mongoid/relations/proxy.rb +145 -0
  239. data/lib/mongoid/relations/referenced/batch.rb +72 -0
  240. data/lib/mongoid/relations/referenced/batch/insert.rb +57 -0
  241. data/lib/mongoid/relations/referenced/in.rb +262 -0
  242. data/lib/mongoid/relations/referenced/many.rb +623 -0
  243. data/lib/mongoid/relations/referenced/many_to_many.rb +396 -0
  244. data/lib/mongoid/relations/referenced/one.rb +272 -0
  245. data/lib/mongoid/relations/reflections.rb +62 -0
  246. data/lib/mongoid/relations/synchronization.rb +153 -0
  247. data/lib/mongoid/relations/targets.rb +2 -0
  248. data/lib/mongoid/relations/targets/enumerable.rb +372 -0
  249. data/lib/mongoid/reloading.rb +91 -0
  250. data/lib/mongoid/safety.rb +105 -0
  251. data/lib/mongoid/scope.rb +31 -0
  252. data/lib/mongoid/serialization.rb +134 -0
  253. data/lib/mongoid/sharding.rb +61 -0
  254. data/lib/mongoid/state.rb +97 -0
  255. data/lib/mongoid/threaded.rb +530 -0
  256. data/lib/mongoid/threaded/lifecycle.rb +192 -0
  257. data/lib/mongoid/timestamps.rb +15 -0
  258. data/lib/mongoid/timestamps/created.rb +24 -0
  259. data/lib/mongoid/timestamps/timeless.rb +50 -0
  260. data/lib/mongoid/timestamps/updated.rb +26 -0
  261. data/lib/mongoid/validations.rb +140 -0
  262. data/lib/mongoid/validations/associated.rb +46 -0
  263. data/lib/mongoid/validations/uniqueness.rb +145 -0
  264. data/lib/mongoid/version.rb +4 -0
  265. data/lib/mongoid/versioning.rb +185 -0
  266. data/lib/rack/mongoid.rb +2 -0
  267. data/lib/rack/mongoid/middleware/identity_map.rb +38 -0
  268. data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
  269. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +20 -0
  270. data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
  271. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +19 -0
  272. data/lib/rails/generators/mongoid/observer/observer_generator.rb +17 -0
  273. data/lib/rails/generators/mongoid/observer/templates/observer.rb.tt +4 -0
  274. data/lib/rails/generators/mongoid_generator.rb +70 -0
  275. data/lib/rails/mongoid.rb +91 -0
  276. metadata +465 -0
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Collections #:nodoc:
4
+
5
+ # This class wraps the MongoDB master database.
6
+ class Master
7
+ include Mongoid::Collections::Retry
8
+
9
+ attr_reader :collection
10
+
11
+ # All read and write operations should delegate to the master connection.
12
+ # These operations mimic the methods on a Mongo:Collection.
13
+ #
14
+ # @example Proxy the driver save.
15
+ # collection.save({ :name => "Al" })
16
+ Operations::ALL.each do |name|
17
+ class_eval <<-EOS, __FILE__, __LINE__
18
+ def #{name}(*args)
19
+ retry_on_connection_failure do
20
+ collection.#{name}(*args)
21
+ end
22
+ end
23
+ EOS
24
+ end
25
+
26
+ # Create the new database writer. Will create a collection from the
27
+ # master database.
28
+ #
29
+ # @example Create a new wrapped master.
30
+ # Master.new(db, "testing")
31
+ #
32
+ # @param [ Mongo::DB ] master The master database.
33
+ # @param [ String ] name The name of the database.
34
+ # @param [ Hash ] options The collection options.
35
+ #
36
+ # @option options [ true, false ] :capped If the collection is capped.
37
+ # @option options [ Integer ] :size The capped collection size.
38
+ # @option options [ Integer ] :max The maximum number of docs in the
39
+ # capped collection.
40
+ def initialize(master, name, options = {})
41
+ @collection = master.create_collection(name, options)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Collections #:nodoc:
4
+ module Operations #:nodoc:
5
+
6
+ # Constant definining all the read operations available for a
7
+ # Mongo:Collection. This is used in delegation.
8
+ READ = [
9
+ :[],
10
+ :db,
11
+ :count,
12
+ :distinct,
13
+ :find,
14
+ :find_one,
15
+ :group,
16
+ :index_information,
17
+ :map_reduce,
18
+ :mapreduce,
19
+ :stats,
20
+ :options
21
+ ]
22
+
23
+ # Constant definining all the write operations available for a
24
+ # Mongo:Collection. This is used in delegation.
25
+ WRITE = [
26
+ :<<,
27
+ :create_index,
28
+ :drop,
29
+ :drop_index,
30
+ :drop_indexes,
31
+ :find_and_modify,
32
+ :insert,
33
+ :remove,
34
+ :rename,
35
+ :save,
36
+ :update
37
+ ]
38
+
39
+ # Convenience constant for getting back all collection operations.
40
+ ALL = (READ + WRITE)
41
+ PROXIED = ALL - [ :find, :find_one, :map_reduce, :mapreduce, :update ]
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Collections #:nodoc:
4
+
5
+ # Provides behaviour for retrying commands on connection failure.
6
+ module Retry
7
+
8
+ # Retries command on connection failures.
9
+ #
10
+ # This is useful when using replica sets. When a primary server wents
11
+ # down and a command is issued, the driver will raise a
12
+ # Mongo::ConnectionFailure. We wait a little bit, because nodes are
13
+ # electing themselves, and then retry the given command.
14
+ #
15
+ # By setting Mongoid.max_retries_on_connection_failure to a value of 0,
16
+ # no attempt will be made, immediately raising connection failure.
17
+ # Otherwise it will attempt to make the specified number of retries
18
+ # and then raising the exception to clients.
19
+ #
20
+ # @example Retry the command.
21
+ # retry_on_connection_failure do
22
+ # collection.send(name, *args)
23
+ # end
24
+ #
25
+ # @since 2.0.0
26
+ def retry_on_connection_failure
27
+ retries = 0
28
+ begin
29
+ yield
30
+ rescue Mongo::ConnectionFailure => ex
31
+ retries += 1
32
+ raise ex if retries > Mongoid.max_retries_on_connection_failure
33
+ Kernel.sleep(0.5)
34
+ log_retry retries
35
+ retry
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def log_retry(retry_number)
42
+ Mongoid.logger.warn "A Mongo::ConnectionFailure was raised. Retry attempt ##{retry_number}."
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,96 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Components #:nodoc
4
+ extend ActiveSupport::Concern
5
+
6
+ # All modules that a +Document+ is composed of are defined in this
7
+ # module, to keep the document class from getting too cluttered.
8
+ included do
9
+ extend ActiveModel::Translation
10
+ extend Mongoid::Finders
11
+ end
12
+
13
+ include ActiveModel::Conversion
14
+ include ActiveModel::MassAssignmentSecurity
15
+ include ActiveModel::Naming
16
+ include ActiveModel::Observing
17
+ include ActiveModel::Serializers::JSON
18
+ include ActiveModel::Serializers::Xml
19
+ include Mongoid::Atomic
20
+ include Mongoid::Dirty
21
+ include Mongoid::Attributes
22
+ include Mongoid::Collections
23
+ include Mongoid::Copyable
24
+ include Mongoid::DefaultScope
25
+ include Mongoid::Extras
26
+ include Mongoid::Fields
27
+ include Mongoid::Hierarchy
28
+ include Mongoid::Indexes
29
+ include Mongoid::Inspection
30
+ include Mongoid::JSON
31
+ include Mongoid::Keys
32
+ include Mongoid::Matchers
33
+ include Mongoid::NamedScope
34
+ include Mongoid::NestedAttributes
35
+ include Mongoid::Persistence
36
+ include Mongoid::Relations
37
+ include Mongoid::Reloading
38
+ include Mongoid::Safety
39
+ include Mongoid::Serialization
40
+ include Mongoid::Sharding
41
+ include Mongoid::State
42
+ include Mongoid::Threaded::Lifecycle
43
+ include Mongoid::Timestamps::Timeless
44
+ include Mongoid::Validations
45
+ include Mongoid::Callbacks
46
+ include Mongoid::MultiDatabase
47
+
48
+ MODULES = [
49
+ Mongoid::Atomic,
50
+ Mongoid::Attributes,
51
+ Mongoid::Collections,
52
+ Mongoid::Copyable,
53
+ Mongoid::DefaultScope,
54
+ Mongoid::Dirty,
55
+ Mongoid::Extras,
56
+ Mongoid::Fields,
57
+ Mongoid::Hierarchy,
58
+ Mongoid::Indexes,
59
+ Mongoid::Inspection,
60
+ Mongoid::JSON,
61
+ Mongoid::Keys,
62
+ Mongoid::Matchers,
63
+ Mongoid::NamedScope,
64
+ Mongoid::NestedAttributes,
65
+ Mongoid::Persistence,
66
+ Mongoid::Relations,
67
+ Mongoid::Safety,
68
+ Mongoid::Serialization,
69
+ Mongoid::Sharding,
70
+ Mongoid::State,
71
+ Mongoid::Validations,
72
+ Mongoid::Callbacks,
73
+ Mongoid::MultiDatabase,
74
+ ]
75
+
76
+ class << self
77
+
78
+ # Get a list of methods that would be a bad idea to define as field names
79
+ # or override when including Mongoid::Document.
80
+ #
81
+ # @example Bad thing!
82
+ # Mongoid::Components.prohibited_methods
83
+ #
84
+ # @return [ Array<Symbol> ]
85
+ #
86
+ # @since 2.1.8
87
+ def prohibited_methods
88
+ @prohibited_methods ||= MODULES.inject([]) do |methods, mod|
89
+ methods.tap do |mets|
90
+ mets << mod.instance_methods.map{ |m| m.to_sym }
91
+ end
92
+ end.flatten
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,347 @@
1
+ # encoding: utf-8
2
+ require "uri"
3
+ require "mongoid/config/database"
4
+ require "mongoid/config/replset_database"
5
+
6
+ module Mongoid #:nodoc
7
+
8
+ # This module defines all the configuration options for Mongoid, including the
9
+ # database connections.
10
+ #
11
+ # @todo Durran: This module needs an overhaul, remove singleton, etc.
12
+ module Config
13
+ extend self
14
+ include ActiveModel::Observing
15
+
16
+ attr_accessor :master, :settings, :defaults
17
+ @settings = {}
18
+ @defaults = {}
19
+
20
+ # Define a configuration option with a default.
21
+ #
22
+ # @example Define the option.
23
+ # Config.option(:persist_in_safe_mode, :default => false)
24
+ #
25
+ # @param [ Symbol ] name The name of the configuration option.
26
+ # @param [ Hash ] options Extras for the option.
27
+ #
28
+ # @option options [ Object ] :default The default value.
29
+ #
30
+ # @since 2.0.0.rc.1
31
+ def option(name, options = {})
32
+ defaults[name] = settings[name] = options[:default]
33
+
34
+ class_eval <<-RUBY
35
+ def #{name}
36
+ settings[#{name.inspect}]
37
+ end
38
+
39
+ def #{name}=(value)
40
+ settings[#{name.inspect}] = value
41
+ end
42
+
43
+ def #{name}?
44
+ #{name}
45
+ end
46
+ RUBY
47
+ end
48
+
49
+ option :allow_dynamic_fields, :default => true
50
+ option :autocreate_indexes, :default => false
51
+ option :identity_map_enabled, :default => false
52
+ option :include_root_in_json, :default => false
53
+ option :max_retries_on_connection_failure, :default => 0
54
+ option :parameterize_keys, :default => true
55
+ option :persist_in_safe_mode, :default => false
56
+ option :preload_models, :default => false
57
+ option :raise_not_found_error, :default => true
58
+ option :skip_version_check, :default => false
59
+ option :time_zone, :default => nil
60
+ option :use_utc, :default => false
61
+
62
+ # Adds a new I18n locale file to the load path.
63
+ #
64
+ # @example Add a portuguese locale.
65
+ # Mongoid::Config.add_language('pt')
66
+ #
67
+ # @example Add all available languages.
68
+ # Mongoid::Config.add_language('*')
69
+ #
70
+ # @param [ String ] language_code The language to add.
71
+ def add_language(language_code = nil)
72
+ Dir[
73
+ File.join(
74
+ File.dirname(__FILE__), "..", "config", "locales", "#{language_code}.yml"
75
+ )
76
+ ].each do |file|
77
+ I18n.load_path << File.expand_path(file)
78
+ end
79
+ end
80
+
81
+ # Get any extra databases that have been configured.
82
+ #
83
+ # @example Get the extras.
84
+ # config.databases
85
+ #
86
+ # @return [ Hash ] A hash of secondary databases.
87
+ def databases
88
+ configure_extras(@settings["databases"]) unless @databases || !@settings
89
+ @databases || {}
90
+ end
91
+
92
+ # @todo Durran: There were no tests around the databases setter, not sure
93
+ # what the exact expectation was. Set with a hash?
94
+ def databases=(databases)
95
+ end
96
+
97
+ # Return field names that could cause destructive things to happen if
98
+ # defined in a Mongoid::Document.
99
+ #
100
+ # @example Get the destructive fields.
101
+ # config.destructive_fields
102
+ #
103
+ # @return [ Array<String> ] An array of bad field names.
104
+ def destructive_fields
105
+ Components.prohibited_methods
106
+ end
107
+
108
+ # Configure mongoid from a hash. This is usually called after parsing a
109
+ # yaml config file such as mongoid.yml.
110
+ #
111
+ # @example Configure Mongoid.
112
+ # config.from_hash({})
113
+ #
114
+ # @param [ Hash ] options The settings to use.
115
+ def from_hash(options = {})
116
+ options.except("database", "slaves", "databases").each_pair do |name, value|
117
+ send("#{name}=", value) if respond_to?("#{name}=")
118
+ end
119
+ @master, @slaves = configure_databases(options)
120
+ configure_extras(options["databases"])
121
+ end
122
+
123
+ # Load the settings from a compliant mongoid.yml file. This can be used for
124
+ # easy setup with frameworks other than Rails.
125
+ #
126
+ # @example Configure Mongoid.
127
+ # Mongoid.load!("/path/to/mongoid.yml")
128
+ #
129
+ # @param [ String ] path The path to the file.
130
+ #
131
+ # @since 2.0.1
132
+ def load!(path)
133
+ environment = defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : ENV["RACK_ENV"]
134
+ settings = YAML.load(ERB.new(File.new(path).read).result)[environment]
135
+ if settings.present?
136
+ from_hash(settings)
137
+ end
138
+ end
139
+
140
+ # Returns the default logger, which is either a Rails logger of stdout logger
141
+ #
142
+ # @example Get the default logger
143
+ # config.default_logger
144
+ #
145
+ # @return [ Logger ] The default Logger instance.
146
+ def default_logger
147
+ defined?(Rails) && Rails.respond_to?(:logger) ? Rails.logger : ::Logger.new($stdout)
148
+ end
149
+
150
+ # Returns the logger, or defaults to Rails logger or stdout logger.
151
+ #
152
+ # @example Get the logger.
153
+ # config.logger
154
+ #
155
+ # @return [ Logger ] The configured logger or a default Logger instance.
156
+ def logger
157
+ @logger = default_logger unless defined?(@logger)
158
+ @logger
159
+ end
160
+
161
+ # Sets the logger for Mongoid to use.
162
+ #
163
+ # @example Set the logger.
164
+ # config.logger = Logger.new($stdout, :warn)
165
+ #
166
+ # @return [ Logger ] The newly set logger.
167
+ def logger=(logger)
168
+ case logger
169
+ when false, nil then @logger = nil
170
+ when true then @logger = default_logger
171
+ else
172
+ @logger = logger if logger.respond_to?(:info)
173
+ end
174
+ end
175
+
176
+ # Purge all data in all collections, including indexes.
177
+ #
178
+ # @example Purge all data.
179
+ # Mongoid::Config.purge!
180
+ #
181
+ # @since 2.0.2
182
+ def purge!
183
+ master.collections.map do |collection|
184
+ collection.drop if collection.name !~ /system/
185
+ end
186
+ end
187
+
188
+ # Sets whether the times returned from the database use the ruby or
189
+ # the ActiveSupport time zone.
190
+ #
191
+ # @note If you omit this setting, then times will use the ruby time zone.
192
+ #
193
+ # @example Set the time zone config.
194
+ # Config.use_activesupport_time_zone = true
195
+ #
196
+ # @param [ true, false ] value Whether to use Active Support time zones.
197
+ #
198
+ # @return [ true, false ] The supplied value or false if nil.
199
+ def use_activesupport_time_zone=(value)
200
+ @use_activesupport_time_zone = value || false
201
+ end
202
+ attr_reader :use_activesupport_time_zone
203
+ alias_method :use_activesupport_time_zone?, :use_activesupport_time_zone
204
+
205
+ # Sets the Mongo::DB master database to be used. If the object trying to be
206
+ # set is not a valid +Mongo::DB+, then an error will be raised.
207
+ #
208
+ # @example Set the master database.
209
+ # config.master = Mongo::Connection.new.db("test")
210
+ #
211
+ # @param [ Mongo::DB ] db The master database.
212
+ #
213
+ # @raise [ Errors::InvalidDatabase ] If the master isnt a valid object.
214
+ #
215
+ # @return [ Mongo::DB ] The master instance.
216
+ def master=(db)
217
+ check_database!(db)
218
+ @master = db
219
+ end
220
+ alias :database= :master=
221
+
222
+ # Returns the master database, or if none has been set it will raise an
223
+ # error.
224
+ #
225
+ # @example Get the master database.
226
+ # config.master
227
+ #
228
+ # @raise [ Errors::InvalidDatabase ] If the database was not set.
229
+ #
230
+ # @return [ Mongo::DB ] The master database.
231
+ def master
232
+ unless @master
233
+ @master, @slaves = configure_databases(@settings) unless @settings.blank?
234
+ raise Errors::InvalidDatabase.new(nil) unless @master
235
+ end
236
+ if @reconnect
237
+ @reconnect = false
238
+ reconnect!
239
+ end
240
+ @master
241
+ end
242
+ alias :database :master
243
+
244
+ # Convenience method for connecting to the master database after forking a
245
+ # new process.
246
+ #
247
+ # @example Reconnect to the master.
248
+ # Mongoid.reconnect!
249
+ #
250
+ # @param [ true, false ] now Perform the reconnection immediately?
251
+ def reconnect!(now = true)
252
+ if now
253
+ master.connection.connect
254
+ else
255
+ # We set a @reconnect flag so that #master knows to reconnect the next
256
+ # time the connection is accessed.
257
+ @reconnect = true
258
+ end
259
+ end
260
+
261
+ # Reset the configuration options to the defaults.
262
+ #
263
+ # @example Reset the configuration options.
264
+ # config.reset
265
+ def reset
266
+ settings.replace(defaults)
267
+ end
268
+
269
+ # @deprecated User replica sets instead.
270
+ def slaves
271
+ slave_warning!
272
+ end
273
+
274
+ # @deprecated User replica sets instead.
275
+ def slaves=(dbs)
276
+ slave_warning!
277
+ end
278
+
279
+ protected
280
+
281
+ # Check if the database is valid and the correct version.
282
+ #
283
+ # @example Check if the database is valid.
284
+ # config.check_database!
285
+ #
286
+ # @param [ Mongo::DB ] database The db to check.
287
+ #
288
+ # @raise [ Errors::InvalidDatabase ] If the object is not valid.
289
+ # @raise [ Errors::UnsupportedVersion ] If the db version is too old.
290
+ def check_database!(database)
291
+ raise Errors::InvalidDatabase.new(database) unless database.kind_of?(Mongo::DB)
292
+ unless skip_version_check
293
+ version = database.connection.server_version
294
+ raise Errors::UnsupportedVersion.new(version) if version < Mongoid::MONGODB_VERSION
295
+ end
296
+ end
297
+
298
+ # Get a database from settings.
299
+ #
300
+ # @example Configure the master and slave dbs.
301
+ # config.configure_databases("database" => "mongoid")
302
+ #
303
+ # @param [ Hash ] options The options to use.
304
+ #
305
+ # @option options [ String ] :database The database name.
306
+ # @option options [ String ] :host The database host.
307
+ # @option options [ String ] :password The password for authentication.
308
+ # @option options [ Integer ] :port The port for the database.
309
+ # @option options [ Array<Hash> ] :slaves The slave db options.
310
+ # @option options [ String ] :uri The uri for the database.
311
+ # @option options [ String ] :username The user for authentication.
312
+ #
313
+ # @since 2.0.0.rc.1
314
+ def configure_databases(options)
315
+ if options.has_key?('hosts')
316
+ ReplsetDatabase.new(options).configure
317
+ else
318
+ Database.new(options).configure
319
+ end
320
+ end
321
+
322
+ # Get the secondary databases from settings.
323
+ #
324
+ # @example Configure the master and slave dbs.
325
+ # config.configure_extras("databases" => settings)
326
+ #
327
+ # @param [ Hash ] options The options to use.
328
+ #
329
+ # @since 2.0.0.rc.1
330
+ def configure_extras(extras)
331
+ @databases = (extras || []).inject({}) do |dbs, (name, options)|
332
+ dbs.tap do |extra|
333
+ dbs[name], dbs["#{name}_slaves"] = configure_databases(options)
334
+ end
335
+ end
336
+ end
337
+
338
+ # Temporarily here so people can move to replica sets.
339
+ def slave_warning!
340
+ warn(
341
+ "Using Mongoid for traditional slave databases will be removed in the " +
342
+ "next release in preference of replica sets. Please change your setup " +
343
+ "accordingly."
344
+ )
345
+ end
346
+ end
347
+ end