Module: Mongoid::Config
- Included in:
- Config
- Defined in:
- lib/mongoid/config.rb,
lib/mongoid/config/options.rb,
lib/mongoid/config/environment.rb,
lib/mongoid/config/validators/client.rb,
lib/mongoid/config/validators/option.rb
Overview
This module defines all the configuration options for Mongoid, including the database connections.
Defined Under Namespace
Modules: Environment, Options, Validators
Constant Summary collapse
- LOCK =
Mutex.new
Instance Method Summary collapse
-
#clients ⇒ Hash
Get the client configuration or an empty hash.
-
#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.
-
#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, option, reset, settings
Instance Method Details
#clients ⇒ Hash
Get the client configuration or an empty hash.
216 217 218 |
# File 'lib/mongoid/config.rb', line 216 def clients @clients ||= {} end |
#configured? ⇒ true, false
Has Mongoid been configured? This is checking that at least a valid client config exists.
37 38 39 |
# File 'lib/mongoid/config.rb', line 37 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.
51 52 53 54 55 56 57 58 59 |
# File 'lib/mongoid/config.rb', line 51 def connect_to(name, = { read: { mode: :primary }}) self.clients = { default: { database: name, hosts: [ "localhost:27017" ], options: } } end |
#destructive_fields ⇒ Array<String>
Return field names that could cause destructive things to happen if defined in a Mongoid::Document.
68 69 70 |
# File 'lib/mongoid/config.rb', line 68 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.
82 83 84 85 86 87 88 89 90 |
# File 'lib/mongoid/config.rb', line 82 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.
127 128 129 130 131 |
# File 'lib/mongoid/config.rb', line 127 def load_configuration(settings) configuration = settings.with_indifferent_access self. = configuration[:options] self.clients = configuration[:clients] end |
#models ⇒ Array<Class>
Get all the models in the application - this is everything that includes Mongoid::Document.
101 102 103 |
# File 'lib/mongoid/config.rb', line 101 def models @models ||= [] end |
#options=(options) ⇒ Object
Set the configuration options. Will validate each one individually.
199 200 201 202 203 204 205 206 |
# File 'lib/mongoid/config.rb', line 199 def () if .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.
157 158 159 |
# File 'lib/mongoid/config.rb', line 157 def override_client(name) Threaded.client_override = name ? name.to_s : nil end |
#override_database(name) ⇒ String, Symbol
Override the database to use globally.
143 144 145 |
# File 'lib/mongoid/config.rb', line 143 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.
171 172 173 |
# File 'lib/mongoid/config.rb', line 171 def purge! Clients.default.database.collections.each(&:drop) and true end |
#register_model(klass) ⇒ Object
Register a model in the application with Mongoid.
113 114 115 116 117 |
# File 'lib/mongoid/config.rb', line 113 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?
240 241 242 |
# File 'lib/mongoid/config.rb', line 240 def running_with_passenger? @running_with_passenger ||= defined?(PhusionPassenger) end |
#time_zone ⇒ String
Get the time zone to use.
228 229 230 |
# File 'lib/mongoid/config.rb', line 228 def time_zone use_utc? ? "UTC" : ::Time.zone end |