Module: Mongoid::Config
- Extended by:
- Forwardable, Config, Defaults, Encryption, Options
- Includes:
- DeprecatedOptions
- Included in:
- Config
- Defined in:
- lib/mongoid/config.rb,
lib/mongoid/config/options.rb,
lib/mongoid/config/defaults.rb,
lib/mongoid/config/encryption.rb,
lib/mongoid/config/environment.rb,
lib/mongoid/config/validators/client.rb,
lib/mongoid/config/validators/option.rb,
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, Encryption, Environment, Options, Validators
Constant Summary collapse
- LOCK =
Mutex.new
- VALID_ISOLATION_LEVELS =
%i[ rails thread fiber ].freeze
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.
-
#real_isolation_level ⇒ Object
private
Returns the (potentially-dereferenced) isolation level that Mongoid will use to store its internal state.
-
#register_model(klass) ⇒ Object
Register a model in the application with Mongoid.
- #running_with_passenger? ⇒ true | false deprecated Deprecated.
-
#time_zone ⇒ String
Get the time zone to use.
-
#truncate! ⇒ true
Truncate all data in all collections, but not the indexes.
-
#validate_isolation_level!(level) ⇒ Object
private
Checks to see if the provided isolation level is something that Mongoid supports.
Methods included from Options
defaults, log_level, option, reset, settings
Methods included from Defaults
Methods included from Encryption
Methods included from DeprecatedOptions
Instance Method Details
#clients ⇒ Hash
Get the client configuration or an empty hash.
482 483 484 |
# File 'lib/mongoid/config.rb', line 482 def clients @clients ||= {} end |
#config ⇒ self
Returns the Config singleton, for use in the configure DSL.
300 301 302 |
# File 'lib/mongoid/config.rb', line 300 def config self end |
#configured? ⇒ true | false
Has Mongoid been configured? This is checking that at least a valid client config exists.
311 312 313 |
# File 'lib/mongoid/config.rb', line 311 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.
323 324 325 326 327 328 329 330 331 |
# File 'lib/mongoid/config.rb', line 323 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.
390 391 392 393 394 |
# File 'lib/mongoid/config.rb', line 390 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.
340 341 342 |
# File 'lib/mongoid/config.rb', line 340 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.
352 353 354 355 356 357 358 359 360 |
# File 'lib/mongoid/config.rb', line 352 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.
402 403 404 405 406 407 408 |
# File 'lib/mongoid/config.rb', line 402 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.
369 370 371 |
# File 'lib/mongoid/config.rb', line 369 def models @models ||= [] end |
#options=(options) ⇒ Object
Set the configuration options. Will validate each one individually.
466 467 468 469 470 471 472 473 474 |
# File 'lib/mongoid/config.rb', line 466 def () return unless Validators::AsyncQueryExecutor.validate() .each_pair do |option, value| Validators::Option.validate(option) send("#{option}=", value) end end |
#override_client(name) ⇒ String | Symbol
Override the client to use globally.
430 431 432 |
# File 'lib/mongoid/config.rb', line 430 def override_client(name) Threaded.client_override = name ? name.to_s : nil end |
#override_database(name) ⇒ String | Symbol
Override the database to use globally.
418 419 420 |
# File 'lib/mongoid/config.rb', line 418 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.
442 443 444 |
# File 'lib/mongoid/config.rb', line 442 def purge! global_client.database.collections.each(&:drop) and true end |
#real_isolation_level ⇒ 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.
Returns the (potentially-dereferenced) isolation level that Mongoid
will use to store its internal state. If isolation_level is set to
:rails, this will return the isolation level that Rails is current
configured to use (ActiveSupport::IsolatedExecutionState.isolation_level).
If using an older version of Rails that does not support
ActiveSupport::IsolatedExecutionState, this will return :thread
instead.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/mongoid/config.rb', line 172 def real_isolation_level return isolation_level unless isolation_level == :rails if defined?(ActiveSupport::IsolatedExecutionState) ActiveSupport::IsolatedExecutionState.isolation_level.tap do |level| # We can't guarantee that Rails will always support the same # isolation levels as Mongoid, so we check here to make sure # it's something we can work with. validate_isolation_level!(level) end else # The default, if Rails does not support IsolatedExecutionState, :thread end end |
#register_model(klass) ⇒ Object
Register a model in the application with Mongoid.
379 380 381 382 383 |
# File 'lib/mongoid/config.rb', line 379 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?
504 505 506 |
# File 'lib/mongoid/config.rb', line 504 def running_with_passenger? @running_with_passenger ||= defined?(PhusionPassenger) end |
#time_zone ⇒ String
Get the time zone to use.
492 493 494 |
# File 'lib/mongoid/config.rb', line 492 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.
454 455 456 457 458 |
# File 'lib/mongoid/config.rb', line 454 def truncate! global_client.database.collections.each do |collection| collection.find.delete_many end and true end |
#validate_isolation_level!(level) ⇒ 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.
Checks to see if the provided isolation level is something that Mongoid supports. Raises Errors::UnsupportedIsolationLevel if it is not.
This will also raise an error if the isolation level is set to :fiber
and the Ruby version is less than 3.2, since fiber-local storage
is not supported in earlier Ruby versions.
196 197 198 199 200 201 202 |
# File 'lib/mongoid/config.rb', line 196 def validate_isolation_level!(level) raise Errors::UnsupportedIsolationLevel.new(level) unless VALID_ISOLATION_LEVELS.include?(level) return unless level == :fiber && RUBY_VERSION < '3.2' raise Errors::UnsupportedIsolationLevel.new(level) end |