Class: Mongoid::Config::ReplsetDatabase

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

Instance Method Summary collapse

Methods included from Extensions::Hash::Scoping

#as_conditions

Methods included from Extensions::Hash::CriteriaHelpers

#expand_complex_criteria, #extract_id

Methods included from Extensions::Hash::DeepCopy

#_deep_copy

Constructor Details

#initialize(options = {}) ⇒ ReplsetDatabase

Create the new db configuration class.

replSet does not supports auth

Examples:

Initialize the class.

Config::ReplsetDatabase.new(
  "hosts" => [[host1,port1],[host2,port2]]
)

Parameters:

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

    The configuration options.

Options Hash (options):

  • :hosts (Array)

    The database host.

  • :database (String)

    The database name.

  • :read_secondary (Boolean)

    Tells the driver to read from secondaries.

See Also:

  • for all options

Since:

  • 2.0.0.rc.5



75
76
77
# File 'lib/mongoid/config/replset_database.rb', line 75

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

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.2



52
53
54
# File 'lib/mongoid/config/replset_database.rb', line 52

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

Instance Method Details

#authenticating?true, false

Do we need to authenticate against the database?

Examples:

Are we authenticating?

db.authenticating?

Returns:

  • (true, false)

    True if auth is needed, false if not.

Since:

  • 2.0.2



40
41
42
# File 'lib/mongoid/config/replset_database.rb', line 40

def authenticating?
  username || password
end

#configureArray<Mongo::DB, nil ] The Mongo databases.

Configure the database connections. This will return an array containing one Mongo::DB and nil (to keep compatibility with Mongoid::Config::Database) If you want the reads to go to a secondary node use the :read_secondary(true): option

Examples:

Configure the connection.

db.configure

Returns:

  • (Array<Mongo::DB, nil ] The Mongo databases.)

    Array<Mongo::DB, nil ] The Mongo databases.

Since:

  • 2.0.0.rc.5



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mongoid/config/replset_database.rb', line 16

def configure
  # yes, construction is weird but the driver wants
  # "A list of host-port pairs ending with a hash containing any options"
  # mongo likes symbols
  options = reject{ |key, value| Config.blacklisted_options.include?(key.to_s) }
  options["logger"] = Mongoid::Logger.new
  connection = Mongo::ReplSetConnection.new(hosts.clone, options.symbolize_keys)

  if authenticating?
    connection.add_auth(database, username, password, nil)
    connection.apply_saved_authentication
  end

  [ connection.db(database), nil ]
end