class Monga::Client

Constants

VALID_OPTS

Public Class Methods

new(opts = {}) click to toggle source

Following options are allowed

  • host - host of server to connect, default 127.0.0.1

  • port - port of server to connect, default 27017

  • server - host:port of server to connect (you can pass server or host/port pair)

  • type - :em/:sync/:block - socket type, asynchronouse on EventMachine, Fibered or blocking TCP

  • pool_size - connection pool size

  • servers - array of server names (host:port) to connect or array of hashes host/port (for Replica Set connection)

  • read_pref - read preference for Replica Set (:primary, :secondary, :primary_preferred, :secondary_preferred), default :primary

  • timeout - client will try to reconnect till timout (in seconds) if connection failed, default 10 seconds

# File lib/monga/client.rb, line 18
def initialize(opts = {})
  @opts = opts
  @opts[:type] ||= :block
  @opts[:timeout] ||= 10

  sanitize_opts!
  create_client
end

Public Instance Methods

[](db_name)
Alias for: get_database
get_database(db_name) click to toggle source

Choose database by it's name

# File lib/monga/client.rb, line 28
def get_database(db_name)
  Monga::Database.new(@client, db_name)
end
Also aliased as: []

Private Instance Methods

create_client() click to toggle source

If servers options is defined it will create ReplicaSetClient, otherwise SingleInstanceClient will be created

# File lib/monga/client.rb, line 46
def create_client
  @client = if @opts[:servers]
    Monga::Clients::ReplicaSetClient.new(@opts)
  else
    Monga::Clients::SingleInstanceClient.new(@opts)
  end
end
sanitize_opts!() click to toggle source

Validates incoming options to prevent missunderstanding

# File lib/monga/client.rb, line 36
def sanitize_opts!
  @opts.each_key do |key|
    unless VALID_OPTS.include? key
      raise Monga::Exceptions::InvalidClientOption, "`#{key}` is invalid option for Client. Following options are valid: #{VALID_OPTS * ', '}"
    end
  end
end