Class: MongoModel::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/mongomodel/support/configuration.rb

Constant Summary

DEFAULTS =
{
  'host'         => 'localhost',
  'port'         => 27017,
  'database'     => 'mongomodel-default',
  'pool_size'    => 5,
  'pool_timeout' => 5
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Configuration



16
17
18
# File 'lib/mongomodel/support/configuration.rb', line 16

def initialize(options)
  @options = DEFAULTS.merge(options).stringify_keys
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options



14
15
16
# File 'lib/mongomodel/support/configuration.rb', line 14

def options
  @options
end

Class Method Details

.defaultsObject



67
68
69
# File 'lib/mongomodel/support/configuration.rb', line 67

def self.defaults
  new({})
end

Instance Method Details

#connectionObject



55
56
57
58
59
60
61
# File 'lib/mongomodel/support/configuration.rb', line 55

def connection
  if replicas.any?
    @connection ||= Mongo::MongoReplicaSetClient.new(replicas, connection_options)
  else
    @connection ||= Mongo::MongoClient.new(host, port, connection_options)
  end
end

#connection_optionsObject



63
64
65
# File 'lib/mongomodel/support/configuration.rb', line 63

def connection_options
  options.except('host', 'port', 'database', 'username', 'password', 'replicas').symbolize_keys
end

#databaseObject



28
29
30
# File 'lib/mongomodel/support/configuration.rb', line 28

def database
  options['database']
end

#establish_connectionObject



44
45
46
47
48
# File 'lib/mongomodel/support/configuration.rb', line 44

def establish_connection
  @database = connection.db(database)
  @database.authenticate(username, password) if username.present?
  @database
end

#hostObject



20
21
22
# File 'lib/mongomodel/support/configuration.rb', line 20

def host
  options['host']
end

#passwordObject



36
37
38
# File 'lib/mongomodel/support/configuration.rb', line 36

def password
  options['password']
end

#portObject



24
25
26
# File 'lib/mongomodel/support/configuration.rb', line 24

def port
  options['port']
end

#replicasObject



40
41
42
# File 'lib/mongomodel/support/configuration.rb', line 40

def replicas
  options['replicas'] || []
end

#use_database(database) ⇒ Object



50
51
52
53
# File 'lib/mongomodel/support/configuration.rb', line 50

def use_database(database)
  options['database'] = database
  establish_connection
end

#usernameObject



32
33
34
# File 'lib/mongomodel/support/configuration.rb', line 32

def username
  options['username']
end