Module: Mongoid::Config
- Includes:
- DeprecatedOptions
- Included in:
- Config
- Defined in:
- build/mongoid-8.1/lib/mongoid/config.rb,
build/mongoid-8.1/lib/mongoid/config/options.rb,
build/mongoid-8.1/lib/mongoid/config/defaults.rb,
build/mongoid-8.1/lib/mongoid/config/environment.rb,
build/mongoid-8.1/lib/mongoid/config/validators/client.rb,
build/mongoid-8.1/lib/mongoid/config/validators/option.rb,
build/mongoid-8.1/lib/mongoid/config/validators/async_query_executor.rb
Overview
This module defines all the configuration options for Mongoid, including the database connections.
Defined Under Namespace
Modules: Defaults, DeprecatedOptions, Environment, Options, Validators
Constant Summary collapse
- LOCK =
Mutex.new
Constants included from DeprecatedOptions
Instance Method Summary collapse
-
#clients ⇒ Hash
Get the client configuration or an empty hash.
-
#config ⇒ self
Returns the Config singleton, for use in the configure DSL.
-
#configured? ⇒ true | false
Has Mongoid been configured? This is checking that at least a valid client config exists.
-
#connect_to(name, options = { read: { mode: :primary }}) ⇒ Object
Connect to the provided database name on the default client.
-
#deregister_model(klass) ⇒ Object
private
Deregister a model in the application with Mongoid.
-
#destructive_fields ⇒ Array<String>
Return field names that could cause destructive things to happen if defined in a Mongoid::Document.
-
#load!(path, environment = nil) ⇒ Object
Load the settings from a compliant mongoid.yml file.
-
#load_configuration(settings) ⇒ Object
From a hash of settings, load all the configuration.
-
#models ⇒ Array<Class>
Get all the models in the application - this is everything that includes Mongoid::Document.
-
#options=(options) ⇒ Object
Set the configuration options.
-
#override_client(name) ⇒ String | Symbol
Override the client to use globally.
-
#override_database(name) ⇒ String | Symbol
Override the database to use globally.
-
#purge! ⇒ true
Purge all data in all collections, including indexes.
-
#register_model(klass) ⇒ Object
Register a model in the application with Mongoid.
-
#running_with_passenger? ⇒ true | false
Is the application running under passenger?.
-
#time_zone ⇒ String
Get the time zone to use.
-
#truncate! ⇒ true
Truncate all data in all collections, but not the indexes.
Methods included from Options
defaults, log_level, option, reset, settings
Methods included from Defaults
Methods included from DeprecatedOptions
Instance Method Details
#clients ⇒ Hash
Get the client configuration or an empty hash.
373 374 375 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 373 def clients @clients ||= {} end |
#config ⇒ self
Returns the Config singleton, for use in the configure DSL.
191 192 193 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 191 def config self end |
#configured? ⇒ true | false
Has Mongoid been configured? This is checking that at least a valid client config exists.
202 203 204 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 202 def configured? clients.key?(:default) end |
#connect_to(name, options = { read: { mode: :primary }}) ⇒ Object
Use only in development or test environments for convenience.
Connect to the provided database name on the default client.
214 215 216 217 218 219 220 221 222 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 214 def connect_to(name, = { read: { mode: :primary }}) self.clients = { default: { database: name, hosts: [ "localhost:27017" ], options: } } end |
#deregister_model(klass) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Deregister a model in the application with Mongoid.
281 282 283 284 285 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 281 def deregister_model(klass) LOCK.synchronize do models.delete(klass) end end |
#destructive_fields ⇒ Array<String>
Return field names that could cause destructive things to happen if defined in a Mongoid::Document.
231 232 233 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 231 def destructive_fields Composable.prohibited_methods end |
#load!(path, environment = nil) ⇒ Object
Load the settings from a compliant mongoid.yml file. This can be used for easy setup with frameworks other than Rails.
243 244 245 246 247 248 249 250 251 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 243 def load!(path, environment = nil) settings = Environment.load_yaml(path, environment) if settings.present? Clients.disconnect Clients.clear load_configuration(settings) end settings end |
#load_configuration(settings) ⇒ Object
From a hash of settings, load all the configuration.
293 294 295 296 297 298 299 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 293 def load_configuration(settings) configuration = settings.with_indifferent_access self. = configuration[:options] self.clients = configuration[:clients] Mongo. = configuration[:driver_options] || {} set_log_levels end |
#models ⇒ Array<Class>
Get all the models in the application - this is everything that includes Mongoid::Document.
260 261 262 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 260 def models @models ||= [] end |
#options=(options) ⇒ Object
Set the configuration options. Will validate each one individually.
357 358 359 360 361 362 363 364 365 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 357 def () if Validators::AsyncQueryExecutor.validate() .each_pair do |option, value| Validators::Option.validate(option) send("#{option}=", value) end end end |
#override_client(name) ⇒ String | Symbol
Override the client to use globally.
321 322 323 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 321 def override_client(name) Threaded.client_override = name ? name.to_s : nil end |
#override_database(name) ⇒ String | Symbol
Override the database to use globally.
309 310 311 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 309 def override_database(name) Threaded.database_override = name end |
#purge! ⇒ true
This is the fastest way to drop all data.
Purge all data in all collections, including indexes.
333 334 335 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 333 def purge! global_client.database.collections.each(&:drop) and true end |
#register_model(klass) ⇒ Object
Register a model in the application with Mongoid.
270 271 272 273 274 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 270 def register_model(klass) LOCK.synchronize do models.push(klass) unless models.include?(klass) end end |
#running_with_passenger? ⇒ true | false
Is the application running under passenger?
393 394 395 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 393 def running_with_passenger? @running_with_passenger ||= defined?(PhusionPassenger) end |
#time_zone ⇒ String
Get the time zone to use.
383 384 385 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 383 def time_zone use_utc? ? "UTC" : ::Time.zone end |
#truncate! ⇒ true
This will be slower than purge!
Truncate all data in all collections, but not the indexes.
345 346 347 348 349 |
# File 'build/mongoid-8.1/lib/mongoid/config.rb', line 345 def truncate! global_client.database.collections.each do |collection| collection.find.delete_many end and true end |