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
Instance Method Summary collapse
-
#connect_to(name) ⇒ 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.
-
#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.
-
#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.
Methods included from Options
defaults, option, reset, settings
Instance Method Details
#connect_to(name) ⇒ Object
Use only in development or test environments for convenience.
Connect to the provided database name on the default session.
38 39 40 41 42 43 44 45 |
# File 'lib/mongoid/config.rb', line 38 def connect_to(name) self.sessions = { default: { database: name, hosts: [ "localhost:27017" ] } } end |
#destructive_fields ⇒ Array<String>
Return field names that could cause destructive things to happen if defined in a Mongoid::Document.
54 55 56 |
# File 'lib/mongoid/config.rb', line 54 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.
68 69 70 71 72 |
# File 'lib/mongoid/config.rb', line 68 def load!(path, environment = nil) settings = Environment.load_yaml(path, environment) load_configuration(settings) if settings.present? settings end |
#options=(options) ⇒ Object
Set the configuration options. Will validate each one individually.
124 125 126 127 128 129 130 131 |
# File 'lib/mongoid/config.rb', line 124 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.
84 85 86 |
# File 'lib/mongoid/config.rb', line 84 def override_database(name) Threaded.database_override = name end |
#override_session(name) ⇒ String, Symbol
Override the session to use globally.
98 99 100 |
# File 'lib/mongoid/config.rb', line 98 def override_session(name) Threaded.session_override = name ? name.to_s : nil end |
#purge! ⇒ true
Purge all data in all collections, including indexes.
110 111 112 113 114 |
# File 'lib/mongoid/config.rb', line 110 def purge! Sessions.default.collections.each do |collection| collection.drop end and true end |
#sessions ⇒ Hash
Get the session configuration or an empty hash.
141 142 143 |
# File 'lib/mongoid/config.rb', line 141 def sessions @sessions ||= {} end |
#sessions=(sessions) ⇒ Object
Set the session configuration options.
153 154 155 156 157 158 159 |
# File 'lib/mongoid/config.rb', line 153 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.
169 170 171 |
# File 'lib/mongoid/config.rb', line 169 def time_zone use_utc? ? "UTC" : ::Time.zone end |