Module: Mongoid::Config
- Includes:
- ActiveModel::Observing
- Included in:
- Config
- Defined in:
- lib/mongoid/config.rb,
lib/mongoid/config/options.rb,
lib/mongoid/config/environment.rb,
lib/mongoid/config/validators/option.rb,
lib/mongoid/config/validators/session.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
-
#configured? ⇒ true, false
Has Mongoid been configured? This is checking that at least a valid session config exists.
-
#connect_to(name, options = { consistency: :eventual }) ⇒ Object
Connect to the provided database name on the default session.
-
#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_database(name) ⇒ String, Symbol
Override the database to use globally.
-
#override_session(name) ⇒ String, Symbol
Override the session 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?.
-
#sessions ⇒ Hash
Get the session configuration or an empty hash.
-
#sessions=(sessions) ⇒ Object
Set the session configuration options.
-
#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
#configured? ⇒ true, false
Has Mongoid been configured? This is checking that at least a valid session config exists.
43 44 45 |
# File 'lib/mongoid/config.rb', line 43 def configured? sessions.has_key?(:default) end |
#connect_to(name, options = { consistency: :eventual }) ⇒ Object
Use only in development or test environments for convenience.
Connect to the provided database name on the default session.
57 58 59 60 61 62 63 64 65 |
# File 'lib/mongoid/config.rb', line 57 def connect_to(name, = { consistency: :eventual }) self.sessions = { 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.
74 75 76 |
# File 'lib/mongoid/config.rb', line 74 def destructive_fields Components.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.
88 89 90 91 92 93 94 95 96 |
# File 'lib/mongoid/config.rb', line 88 def load!(path, environment = nil) settings = Environment.load_yaml(path, environment) if settings.present? Sessions.disconnect Sessions.clear load_configuration(settings) end settings end |
#load_configuration(settings) ⇒ Object
From a hash of settings, load all the configuration.
133 134 135 136 137 |
# File 'lib/mongoid/config.rb', line 133 def load_configuration(settings) configuration = settings.with_indifferent_access self. = configuration[:options] self.sessions = configuration[:sessions] end |
#models ⇒ Array<Class>
Get all the models in the application - this is everything that includes Mongoid::Document.
107 108 109 |
# File 'lib/mongoid/config.rb', line 107 def models @models ||= [] end |
#options=(options) ⇒ Object
Set the configuration options. Will validate each one individually.
207 208 209 210 211 212 213 214 |
# File 'lib/mongoid/config.rb', line 207 def () if .each_pair do |option, value| Validators::Option.validate(option) send("#{option}=", value) end end end |
#override_database(name) ⇒ String, Symbol
Override the database to use globally.
149 150 151 |
# File 'lib/mongoid/config.rb', line 149 def override_database(name) Threaded.database_override = name end |
#override_session(name) ⇒ String, Symbol
Override the session to use globally.
163 164 165 |
# File 'lib/mongoid/config.rb', line 163 def override_session(name) Threaded.session_override = name ? name.to_s : nil end |
#purge! ⇒ true
This is the fastest way to drop all data.
Purge all data in all collections, including indexes.
177 178 179 180 181 |
# File 'lib/mongoid/config.rb', line 177 def purge! Sessions.default.collections.each do |collection| collection.drop end and true end |
#register_model(klass) ⇒ Object
Register a model in the application with Mongoid.
119 120 121 122 123 |
# File 'lib/mongoid/config.rb', line 119 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?
264 265 266 |
# File 'lib/mongoid/config.rb', line 264 def running_with_passenger? @running_with_passenger ||= defined?(PhusionPassenger) end |
#sessions ⇒ Hash
Get the session configuration or an empty hash.
224 225 226 |
# File 'lib/mongoid/config.rb', line 224 def sessions @sessions ||= {} end |
#sessions=(sessions) ⇒ Object
Set the session configuration options.
236 237 238 239 240 241 242 |
# File 'lib/mongoid/config.rb', line 236 def sessions=(sessions) raise Errors::NoSessionsConfig.new unless sessions sess = sessions.with_indifferent_access Validators::Session.validate(sess) @sessions = sess sess end |
#time_zone ⇒ String
Get the time zone to use.
252 253 254 |
# File 'lib/mongoid/config.rb', line 252 def time_zone use_utc? ? "UTC" : ::Time.zone end |