Class: Mongoid::Config::Database

Inherits:
Hash show all
Defined in:
lib/mongoid/config/database.rb

Overview

This class handles the configuration and initialization of a mongodb database from options.

Constant Summary collapse

PRIVATE_OPTIONS =

keys to remove from self to not pass through to Mongo::Connection

%w(uri database username password logger)

Instance Method Summary collapse

Methods included from Extensions::Hash::Scoping

#scoped

Methods included from Extensions::Hash::CriteriaHelpers

#expand_complex_criteria, #extract_id

Constructor Details

#initialize(options = {}) ⇒ Database

Create the new db configuration class.

Examples:

Initialize the class.

Config::Database.new(
  false, "uri" => { "mongodb://durran:password@localhost:27017/mongoid" }
)

Parameters:

  • options (Hash) (defaults to: {})

    The configuration options.

Options Hash (options):

  • :database (String)

    The database name.

  • :host (String)

    The database host.

  • :password (String)

    The password for authentication.

  • :port (Integer)

    The port for the database.

  • :uri (String)

    The uri for the database.

  • :username (String)

    The user for authentication.

Since:

  • 2.0.0.rc.1



44
45
46
# File 'lib/mongoid/config/database.rb', line 44

def initialize(options = {})
  merge!(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

Convenience for accessing the hash via dot notation.

Examples:

Access a value in alternate syntax.

db.host

Returns:

  • (Object)

    The value in the hash.

Since:

  • 2.0.0.rc.1



134
135
136
# File 'lib/mongoid/config/database.rb', line 134

def method_missing(name, *args, &block)
  self[name.to_s]
end

Instance Method Details

#configureArray<Mongo::DB, Array<Mongo:DB>>

Configure the database connections. This will return an array containing the master and an array of slaves.

Examples:

Configure the connection.

db.configure

Returns:

  • (Array<Mongo::DB, Array<Mongo:DB>>)

    The Mongo databases.

Since:

  • 2.0.0.rc.1



23
24
25
# File 'lib/mongoid/config/database.rb', line 23

def configure
  [ master.db(name), slaves.map { |slave| slave.db(name) } ]
end