Sha256: 9334acb121582cfb609cfa6945f5519e3e1ece9ea0ef1308a851ebcbcc609ed0

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/hash/except'

module MongoModel
  class Configuration
    def initialize(options)
      case options
      when Hash
        @options = DEFAULTS.merge(options).stringify_keys
      when String
        super(parse(options))
      end
    end
    
    def host
      @options['host']
    end
    
    def port
      @options['port']
    end
    
    def database
      @options['database']
    end
    
    def username
      @options['username']
    end
    
    def password
      @options['password']
    end
    
    def establish_connection
      @connection ||= Mongo::Connection.new(host, port, connection_options)
      @database = @connection.db(database)
      @database.authenticate(username, password) if username.present?
      @database
    end
    
    def use_database(database)
      @options['database'] = database
      establish_connection
    end
    
    def connection_options
      @options.except('host', 'port', 'database', 'username', 'password').symbolize_keys
    end
    
    DEFAULTS = {
      'host'      => 'localhost',
      'port'      => 27017,
      'database'  => 'mongomodel-default',
      'pool_size' => 5,
      'timeout'   => 5,
      'logger'    => MongoModel.logger
    }
    
    def self.defaults
      new({})
    end
  
  private
    def parse(str)
      uri = URI.parse(str)
      
      {
        'host'     => uri.host,
        'port'     => uri.port,
        'database' => uri.path.gsub(/^\//, ''),
        'username' => uri.user,
        'password' => uri.password
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongomodel-0.2.5 lib/mongomodel/support/configuration.rb
mongomodel-0.2.4 lib/mongomodel/support/configuration.rb