Module: Mongoid::Config
- Extended by:
- Config
- Includes:
- ActiveModel::Observing
- Included in:
- Config
- Defined in:
- lib/mongoid/config.rb,
lib/mongoid/config/database.rb,
lib/mongoid/config/replset_database.rb
Overview
:nodoc:
Defined Under Namespace
Classes: Database, ReplsetDatabase
Instance Attribute Summary collapse
-
#defaults ⇒ Object
Returns the value of attribute defaults.
-
#master ⇒ Mongo::DB
(also: #database)
Returns the master database, or if none has been set it will raise an error.
-
#settings ⇒ Object
Returns the value of attribute settings.
-
#use_activesupport_time_zone ⇒ Object
(also: #use_activesupport_time_zone?)
Returns the value of attribute use_activesupport_time_zone.
Instance Method Summary collapse
-
#add_language(language_code = nil) ⇒ Object
Adds a new I18n locale file to the load path.
-
#databases ⇒ Hash
Get any extra databases that have been configured.
-
#databases=(databases) ⇒ Object
what the exact expectation was.
-
#default_logger ⇒ Logger
Returns the default logger, which is either a Rails logger of stdout logger.
-
#destructive_fields ⇒ Array<String>
Return field names that could cause destructive things to happen if defined in a Mongoid::Document.
-
#from_hash(options = {}) ⇒ Object
Configure mongoid from a hash.
-
#load!(path) ⇒ Object
Load the settings from a compliant mongoid.yml file.
-
#logger ⇒ Logger
Returns the logger, or defaults to Rails logger or stdout logger.
-
#logger=(logger) ⇒ Logger
Sets the logger for Mongoid to use.
-
#option(name, options = {}) ⇒ Object
Define a configuration option with a default.
-
#purge! ⇒ Object
Purge all data in all collections, including indexes.
-
#reconnect!(now = true) ⇒ Object
Convenience method for connecting to the master database after forking a new process.
-
#reset ⇒ Object
Reset the configuration options to the defaults.
-
#slaves ⇒ Object
deprecated
Deprecated.
User replica sets instead.
-
#slaves=(dbs) ⇒ Object
deprecated
Deprecated.
User replica sets instead.
Instance Attribute Details
#defaults ⇒ Object
Returns the value of attribute defaults.
16 17 18 |
# File 'lib/mongoid/config.rb', line 16 def defaults @defaults end |
#master ⇒ Mongo::DB Also known as: database
Returns the master database, or if none has been set it will raise an error.
231 232 233 |
# File 'lib/mongoid/config.rb', line 231 def master @master end |
#settings ⇒ Object
Returns the value of attribute settings.
16 17 18 |
# File 'lib/mongoid/config.rb', line 16 def settings @settings end |
#use_activesupport_time_zone ⇒ Object Also known as: use_activesupport_time_zone?
Returns the value of attribute use_activesupport_time_zone.
202 203 204 |
# File 'lib/mongoid/config.rb', line 202 def use_activesupport_time_zone @use_activesupport_time_zone end |
Instance Method Details
#add_language(language_code = nil) ⇒ Object
Adds a new I18n locale file to the load path.
71 72 73 74 75 76 77 78 79 |
# File 'lib/mongoid/config.rb', line 71 def add_language(language_code = nil) Dir[ File.join( File.dirname(__FILE__), "..", "config", "locales", "#{language_code}.yml" ) ].each do |file| I18n.load_path << File.(file) end end |
#databases ⇒ Hash
Get any extra databases that have been configured.
87 88 89 90 |
# File 'lib/mongoid/config.rb', line 87 def databases configure_extras(@settings["databases"]) unless @databases || !@settings @databases || {} end |
#databases=(databases) ⇒ Object
Durran: There were no tests around the databases setter, not sure
what the exact expectation was. Set with a hash?
94 95 |
# File 'lib/mongoid/config.rb', line 94 def databases=(databases) end |
#default_logger ⇒ Logger
Returns the default logger, which is either a Rails logger of stdout logger
146 147 148 |
# File 'lib/mongoid/config.rb', line 146 def default_logger defined?(Rails) && Rails.respond_to?(:logger) ? Rails.logger : ::Logger.new($stdout) end |
#destructive_fields ⇒ Array<String>
Return field names that could cause destructive things to happen if defined in a Mongoid::Document.
104 105 106 |
# File 'lib/mongoid/config.rb', line 104 def destructive_fields Components.prohibited_methods end |
#from_hash(options = {}) ⇒ Object
Configure mongoid from a hash. This is usually called after parsing a yaml config file such as mongoid.yml.
115 116 117 118 119 120 121 |
# File 'lib/mongoid/config.rb', line 115 def from_hash( = {}) .except("database", "slaves", "databases").each_pair do |name, value| send("#{name}=", value) if respond_to?("#{name}=") end @master, @slaves = configure_databases() configure_extras(["databases"]) end |
#load!(path) ⇒ Object
Load the settings from a compliant mongoid.yml file. This can be used for easy setup with frameworks other than Rails.
132 133 134 135 136 137 138 |
# File 'lib/mongoid/config.rb', line 132 def load!(path) environment = defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : ENV["RACK_ENV"] settings = YAML.load(ERB.new(File.new(path).read).result)[environment] if settings.present? from_hash(settings) end end |
#logger ⇒ Logger
Returns the logger, or defaults to Rails logger or stdout logger.
156 157 158 159 |
# File 'lib/mongoid/config.rb', line 156 def logger @logger = default_logger unless defined?(@logger) @logger end |
#logger=(logger) ⇒ Logger
Sets the logger for Mongoid to use.
167 168 169 170 171 172 173 174 |
# File 'lib/mongoid/config.rb', line 167 def logger=(logger) case logger when false, nil then @logger = nil when true then @logger = default_logger else @logger = logger if logger.respond_to?(:info) end end |
#option(name, options = {}) ⇒ Object
Define a configuration option with a default.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mongoid/config.rb', line 31 def option(name, = {}) defaults[name] = settings[name] = [:default] class_eval <<-RUBY def #{name} settings[#{name.inspect}] end def #{name}=(value) settings[#{name.inspect}] = value end def #{name}? #{name} end RUBY end |
#purge! ⇒ Object
Purge all data in all collections, including indexes.
182 183 184 185 186 |
# File 'lib/mongoid/config.rb', line 182 def purge! master.collections.map do |collection| collection.drop if collection.name !~ /system/ end end |
#reconnect!(now = true) ⇒ Object
Convenience method for connecting to the master database after forking a new process.
251 252 253 254 255 256 257 258 259 |
# File 'lib/mongoid/config.rb', line 251 def reconnect!(now = true) if now master.connection.connect else # We set a @reconnect flag so that #master knows to reconnect the next # time the connection is accessed. @reconnect = true end end |
#reset ⇒ Object
Reset the configuration options to the defaults.
265 266 267 |
# File 'lib/mongoid/config.rb', line 265 def reset settings.replace(defaults) end |
#slaves ⇒ Object
User replica sets instead.
270 271 272 |
# File 'lib/mongoid/config.rb', line 270 def slaves slave_warning! end |
#slaves=(dbs) ⇒ Object
User replica sets instead.
275 276 277 |
# File 'lib/mongoid/config.rb', line 275 def slaves=(dbs) slave_warning! end |