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,39 @@
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
+ retry
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,45 @@
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::Atomicity
20
+ include Mongoid::Attributes
21
+ include Mongoid::Collections
22
+ include Mongoid::Copyable
23
+ include Mongoid::DefaultScope
24
+ include Mongoid::Dirty
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::Paths
36
+ include Mongoid::Persistence
37
+ include Mongoid::Relations
38
+ include Mongoid::Safety
39
+ include Mongoid::Serialization
40
+ include Mongoid::Sharding
41
+ include Mongoid::State
42
+ include Mongoid::Validations
43
+ include Mongoid::Callbacks
44
+ end
45
+ end
@@ -0,0 +1,349 @@
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
17
+ @settings = {}
18
+
19
+ # Define a configuration option with a default.
20
+ #
21
+ # @example Define the option.
22
+ # Config.option(:persist_in_safe_mode, :default => false)
23
+ #
24
+ # @param [ Symbol ] name The name of the configuration option.
25
+ # @param [ Hash ] options Extras for the option.
26
+ #
27
+ # @option options [ Object ] :default The default value.
28
+ #
29
+ # @since 2.0.0.rc.1
30
+ def option(name, options = {})
31
+ define_method(name) do
32
+ settings.has_key?(name) ? settings[name] : options[:default]
33
+ end
34
+ define_method("#{name}=") { |value| settings[name] = value }
35
+ define_method("#{name}?") { send(name) }
36
+ end
37
+
38
+ option :allow_dynamic_fields, :default => true
39
+ option :autocreate_indexes, :default => false
40
+ option :binding_defaults, :default => { :binding => false, :continue => true }
41
+ option :embedded_object_id, :default => true
42
+ option :include_root_in_json, :default => false
43
+ option :max_retries_on_connection_failure, :default => 0
44
+ option :parameterize_keys, :default => true
45
+ option :persist_in_safe_mode, :default => false
46
+ option :preload_models, :default => true
47
+ option :raise_not_found_error, :default => true
48
+ option :skip_version_check, :default => false
49
+ option :time_zone, :default => nil
50
+ option :use_utc, :default => false
51
+
52
+ # Adds a new I18n locale file to the load path.
53
+ #
54
+ # @example Add a portuguese locale.
55
+ # Mongoid::Config.add_language('pt')
56
+ #
57
+ # @example Add all available languages.
58
+ # Mongoid::Config.add_language('*')
59
+ #
60
+ # @param [ String ] language_code The language to add.
61
+ def add_language(language_code = nil)
62
+ Dir[
63
+ File.join(
64
+ File.dirname(__FILE__), "..", "config", "locales", "#{language_code}.yml"
65
+ )
66
+ ].each do |file|
67
+ I18n.load_path << File.expand_path(file)
68
+ end
69
+ end
70
+
71
+ # Get any extra databases that have been configured.
72
+ #
73
+ # @example Get the extras.
74
+ # config.databases
75
+ #
76
+ # @return [ Hash ] A hash of secondary databases.
77
+ def databases
78
+ configure_extras(@settings["databases"]) unless @databases || !@settings
79
+ @databases || {}
80
+ end
81
+
82
+ # @todo Durran: There were no tests around the databases setter, not sure
83
+ # what the exact expectation was. Set with a hash?
84
+ def databases=(databases)
85
+ end
86
+
87
+ # Return field names that could cause destructive things to happen if
88
+ # defined in a Mongoid::Document.
89
+ #
90
+ # @example Get the destructive fields.
91
+ # config.destructive_fields
92
+ #
93
+ # @return [ Array<String> ] An array of bad field names.
94
+ def destructive_fields
95
+ @destructive_fields ||= lambda {
96
+ klass = Class.new do
97
+ include Mongoid::Document
98
+ end
99
+ klass.instance_methods(true).collect { |method| method.to_s }
100
+ }.call
101
+ end
102
+
103
+ # Configure mongoid from a hash. This is usually called after parsing a
104
+ # yaml config file such as mongoid.yml.
105
+ #
106
+ # @example Configure Mongoid.
107
+ # config.from_hash({})
108
+ #
109
+ # @param [ Hash ] options The settings to use.
110
+ def from_hash(options = {})
111
+ options.except("database", "slaves", "databases").each_pair do |name, value|
112
+ send("#{name}=", value) if respond_to?("#{name}=")
113
+ end
114
+ @master, @slaves = configure_databases(options)
115
+ configure_extras(options["databases"])
116
+ end
117
+
118
+ # Load the settings from a compliant mongoid.yml file. This can be used for
119
+ # easy setup with frameworks other than Rails.
120
+ #
121
+ # @example Configure Mongoid.
122
+ # Mongoid.load!("/path/to/mongoid.yml")
123
+ #
124
+ # @param [ String ] path The path to the file.
125
+ #
126
+ # @since 2.0.1
127
+ def load!(path)
128
+ environment = defined?(Rails) ? Rails.env : ENV["RACK_ENV"]
129
+ settings = YAML.load(ERB.new(File.new(path).read).result)[environment]
130
+ if settings.present?
131
+ from_hash(settings)
132
+ end
133
+ end
134
+
135
+ # Returns the default logger, which is either a Rails logger of stdout logger
136
+ #
137
+ # @example Get the default logger
138
+ # config.default_logger
139
+ #
140
+ # @return [ Logger ] The default Logger instance.
141
+ def default_logger
142
+ defined?(Rails) ? Rails.logger : ::Logger.new($stdout)
143
+ end
144
+
145
+ # Returns the logger, or defaults to Rails logger or stdout logger.
146
+ #
147
+ # @example Get the logger.
148
+ # config.logger
149
+ #
150
+ # @return [ Logger ] The configured logger or a default Logger instance.
151
+ def logger
152
+ @logger = default_logger unless defined?(@logger)
153
+ @logger
154
+ end
155
+
156
+ # Sets the logger for Mongoid to use.
157
+ #
158
+ # @example Set the logger.
159
+ # config.logger = Logger.new($stdout, :warn)
160
+ #
161
+ # @return [ Logger ] The newly set logger.
162
+ def logger=(logger)
163
+ @logger = logger
164
+ end
165
+
166
+ # Purge all data in all collections, including indexes.
167
+ #
168
+ # @example Purge all data.
169
+ # Mongoid::Config.purge!
170
+ #
171
+ # @since 2.0.2
172
+ def purge!
173
+ master.collections.map do |collection|
174
+ collection.drop if collection.name !~ /system/
175
+ end
176
+ end
177
+
178
+ # Sets whether the times returned from the database use the ruby or
179
+ # the ActiveSupport time zone.
180
+ # If you omit this setting, then times will use the ruby time zone.
181
+ #
182
+ # Example:
183
+ #
184
+ # <tt>Config.use_activesupport_time_zone = true</tt>
185
+ #
186
+ # Returns:
187
+ #
188
+ # A boolean
189
+ def use_activesupport_time_zone=(value)
190
+ @use_activesupport_time_zone = value || false
191
+ end
192
+
193
+ # Sets whether the times returned from the database use the ruby or
194
+ # the ActiveSupport time zone.
195
+ # If the setting is false, then times will use the ruby time zone.
196
+ #
197
+ # Example:
198
+ #
199
+ # <tt>Config.use_activesupport_time_zone</tt>
200
+ #
201
+ # Returns:
202
+ #
203
+ # A boolean
204
+ attr_reader :use_activesupport_time_zone
205
+ alias_method :use_activesupport_time_zone?, :use_activesupport_time_zone
206
+
207
+ # Sets the Mongo::DB master database to be used. If the object trying to be
208
+ # set is not a valid +Mongo::DB+, then an error will be raised.
209
+ #
210
+ # @example Set the master database.
211
+ # config.master = Mongo::Connection.db("test")
212
+ #
213
+ # @param [ Mongo::DB ] db The master database.
214
+ #
215
+ # @raise [ Errors::InvalidDatabase ] If the master isnt a valid object.
216
+ #
217
+ # @return [ Mongo::DB ] The master instance.
218
+ def master=(db)
219
+ check_database!(db)
220
+ @master = db
221
+ end
222
+ alias :database= :master=
223
+
224
+ # Returns the master database, or if none has been set it will raise an
225
+ # error.
226
+ #
227
+ # @example Get the master database.
228
+ # config.master
229
+ #
230
+ # @raise [ Errors::InvalidDatabase ] If the database was not set.
231
+ #
232
+ # @return [ Mongo::DB ] The master database.
233
+ def master
234
+ unless @master
235
+ @master, @slaves = configure_databases(@settings) unless @settings.blank?
236
+ raise Errors::InvalidDatabase.new(nil) unless @master
237
+ end
238
+ if @reconnect
239
+ @reconnect = false
240
+ reconnect!
241
+ end
242
+ @master
243
+ end
244
+ alias :database :master
245
+
246
+ # Convenience method for connecting to the master database after forking a
247
+ # new process.
248
+ #
249
+ # @example Reconnect to the master.
250
+ # Mongoid.reconnect!
251
+ #
252
+ # @param [ true, false ] now Perform the reconnection immediately?
253
+ def reconnect!(now = true)
254
+ if now
255
+ master.connection.connect
256
+ else
257
+ # We set a @reconnect flag so that #master knows to reconnect the next
258
+ # time the connection is accessed.
259
+ @reconnect = true
260
+ end
261
+ end
262
+
263
+ # Reset the configuration options to the defaults.
264
+ #
265
+ # @example Reset the configuration options.
266
+ # config.reset
267
+ def reset
268
+ settings.clear
269
+ end
270
+
271
+ # @deprecated User replica sets instead.
272
+ def slaves
273
+ slave_warning!
274
+ end
275
+
276
+ # @deprecated User replica sets instead.
277
+ def slaves=(dbs)
278
+ slave_warning!
279
+ end
280
+
281
+ protected
282
+
283
+ # Check if the database is valid and the correct version.
284
+ #
285
+ # @example Check if the database is valid.
286
+ # config.check_database!
287
+ #
288
+ # @param [ Mongo::DB ] database The db to check.
289
+ #
290
+ # @raise [ Errors::InvalidDatabase ] If the object is not valid.
291
+ # @raise [ Errors::UnsupportedVersion ] If the db version is too old.
292
+ def check_database!(database)
293
+ raise Errors::InvalidDatabase.new(database) unless database.kind_of?(Mongo::DB)
294
+ unless skip_version_check
295
+ version = database.connection.server_version
296
+ raise Errors::UnsupportedVersion.new(version) if version < Mongoid::MONGODB_VERSION
297
+ end
298
+ end
299
+
300
+ # Get a database from settings.
301
+ #
302
+ # @example Configure the master and slave dbs.
303
+ # config.configure_databases("database" => "mongoid")
304
+ #
305
+ # @param [ Hash ] options The options to use.
306
+ #
307
+ # @option options [ String ] :database The database name.
308
+ # @option options [ String ] :host The database host.
309
+ # @option options [ String ] :password The password for authentication.
310
+ # @option options [ Integer ] :port The port for the database.
311
+ # @option options [ Array<Hash> ] :slaves The slave db options.
312
+ # @option options [ String ] :uri The uri for the database.
313
+ # @option options [ String ] :username The user for authentication.
314
+ #
315
+ # @since 2.0.0.rc.1
316
+ def configure_databases(options)
317
+ if options.has_key?('hosts')
318
+ ReplsetDatabase.new(options).configure
319
+ else
320
+ Database.new(options).configure
321
+ end
322
+ end
323
+
324
+ # Get the secondary databases from settings.
325
+ #
326
+ # @example Configure the master and slave dbs.
327
+ # config.configure_extras("databases" => settings)
328
+ #
329
+ # @param [ Hash ] options The options to use.
330
+ #
331
+ # @since 2.0.0.rc.1
332
+ def configure_extras(extras)
333
+ @databases = (extras || []).inject({}) do |dbs, (name, options)|
334
+ dbs.tap do |extra|
335
+ dbs[name], dbs["#{name}_slaves"] = configure_databases(options)
336
+ end
337
+ end
338
+ end
339
+
340
+ # Temporarily here so people can move to replica sets.
341
+ def slave_warning!
342
+ warn(
343
+ "Using Mongoid for traditional slave databases will be removed in the " +
344
+ "next release in preference of replica sets. Please change your setup " +
345
+ "accordingly."
346
+ )
347
+ end
348
+ end
349
+ end
@@ -0,0 +1,167 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Config #:nodoc:
4
+
5
+ # This class handles the configuration and initialization of a mongodb
6
+ # database from options.
7
+ class Database < Hash
8
+
9
+ # Configure the database connections. This will return an array
10
+ # containing the master and an array of slaves.
11
+ #
12
+ # @example Configure the connection.
13
+ # db.configure
14
+ #
15
+ # @return [ Array<Mongo::DB, Array<Mongo:DB>> ] The Mongo databases.
16
+ #
17
+ # @since 2.0.0.rc.1
18
+ def configure
19
+ [ master.db(name), slaves.map { |slave| slave.db(name) } ]
20
+ end
21
+
22
+ # Create the new db configuration class.
23
+ #
24
+ # @example Initialize the class.
25
+ # Config::Database.new(
26
+ # false, "uri" => { "mongodb://durran:password@localhost:27017/mongoid" }
27
+ # )
28
+ #
29
+ # @param [ Hash ] options The configuration options.
30
+ #
31
+ # @option options [ String ] :database The database name.
32
+ # @option options [ String ] :host The database host.
33
+ # @option options [ String ] :password The password for authentication.
34
+ # @option options [ Integer ] :port The port for the database.
35
+ # @option options [ String ] :uri The uri for the database.
36
+ # @option options [ String ] :username The user for authentication.
37
+ #
38
+ # @since 2.0.0.rc.1
39
+ def initialize(options = {})
40
+ merge!(options)
41
+ end
42
+
43
+ private
44
+
45
+ # Do we need to authenticate against the database?
46
+ #
47
+ # @example Are we authenticating?
48
+ # db.authenticating?
49
+ #
50
+ # @return [ true, false ] True if auth is needed, false if not.
51
+ #
52
+ # @since 2.0.0.rc.1
53
+ def authenticating?
54
+ username || password
55
+ end
56
+
57
+ # Takes the supplied options in the hash and created a URI from them to
58
+ # pass to the Mongo connection object.
59
+ #
60
+ # @example Build the URI.
61
+ # db.build_uri
62
+ #
63
+ # @param [ Hash ] options The options to build with.
64
+ #
65
+ # @return [ String ] A mongo compliant URI string.
66
+ #
67
+ # @since 2.0.0.rc.1
68
+ def build_uri(options = {})
69
+ "mongodb://".tap do |base|
70
+ base << "#{username}:#{password}@" if authenticating?
71
+ base << "#{options["host"] || "localhost"}:#{options["port"] || 27017}"
72
+ base << "/#{self["database"]}" if authenticating?
73
+ end
74
+ end
75
+
76
+ # Create the mongo master connection from either the supplied URI
77
+ # or a generated one, while setting pool size and logging.
78
+ #
79
+ # @example Create the connection.
80
+ # db.connection
81
+ #
82
+ # @return [ Mongo::Connection ] The mongo connection.
83
+ #
84
+ # @since 2.0.0.rc.1
85
+ def master
86
+ Mongo::Connection.from_uri(uri(self), optional).tap do |conn|
87
+ conn.apply_saved_authentication
88
+ end
89
+ end
90
+
91
+ # Create the mongo slave connections from either the supplied URI
92
+ # or a generated one, while setting pool size and logging.
93
+ #
94
+ # @example Create the connection.
95
+ # db.connection
96
+ #
97
+ # @return [ Array<Mongo::Connection> ] The mongo slave connections.
98
+ #
99
+ # @since 2.0.0.rc.1
100
+ def slaves
101
+ (self["slaves"] || []).map do |options|
102
+ Mongo::Connection.from_uri(uri(options), optional(true)).tap do |conn|
103
+ conn.apply_saved_authentication
104
+ end
105
+ end
106
+ end
107
+
108
+ # Convenience for accessing the hash via dot notation.
109
+ #
110
+ # @example Access a value in alternate syntax.
111
+ # db.host
112
+ #
113
+ # @return [ Object ] The value in the hash.
114
+ #
115
+ # @since 2.0.0.rc.1
116
+ def method_missing(name, *args, &block)
117
+ self[name.to_s]
118
+ end
119
+
120
+ # Get the name of the database, from either the URI supplied or the
121
+ # database value in the options.
122
+ #
123
+ # @example Get the database name.
124
+ # db.name
125
+ #
126
+ # @return [ String ] The database name.
127
+ #
128
+ # @since 2.0.0.rc.1
129
+ def name
130
+ db_name = URI.parse(uri(self)).path.to_s.sub("/", "")
131
+ db_name.blank? ? database : db_name
132
+ end
133
+
134
+ # Get the options used in creating the database connection.
135
+ #
136
+ # @example Get the options.
137
+ # db.options
138
+ #
139
+ # @param [ true, false ] slave Are the options for a slave db?
140
+ #
141
+ # @return [ Hash ] The hash of configuration options.
142
+ #
143
+ # @since 2.0.0.rc.1
144
+ def optional(slave = false)
145
+ {
146
+ :pool_size => pool_size,
147
+ :logger => Mongoid::Logger.new,
148
+ :slave_ok => slave
149
+ }
150
+ end
151
+
152
+ # Get a Mongo compliant URI for the database connection.
153
+ #
154
+ # @example Get the URI.
155
+ # db.uri
156
+ #
157
+ # @param [ Hash ] options The options hash.
158
+ #
159
+ # @return [ String ] The URI for the connection.
160
+ #
161
+ # @since 2.0.0.rc.1
162
+ def uri(options = {})
163
+ options["uri"] || build_uri(options)
164
+ end
165
+ end
166
+ end
167
+ end