Module: MongoMapper::Connection

Included in:
MongoMapper
Defined in:
lib/mongo_mapper/connection.rb

Constant Summary collapse

@@connection =
nil
@@database =
nil
@@database_name =
nil
@@config =
nil

Instance Method Summary collapse

Instance Method Details

#configObject



43
44
45
46
# File 'lib/mongo_mapper/connection.rb', line 43

def config
  raise 'Set config before connecting. MongoMapper.config = {...}' unless defined?(@@config)
  @@config
end

#config=(hash) ⇒ Object



39
40
41
# File 'lib/mongo_mapper/connection.rb', line 39

def config=(hash)
  @@config = hash
end

#config_for_environment(environment) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mongo_mapper/connection.rb', line 49

def config_for_environment(environment)
  env = config[environment.to_s] || {}
  return env if env['uri'].blank?

  uri = URI.parse(env['uri'])
  raise InvalidScheme.new('must be mongodb') unless uri.scheme == 'mongodb'
  {
    'host'     => uri.host,
    'port'     => uri.port,
    'database' => uri.path.gsub(/^\//, ''),
    'username' => uri.user,
    'password' => uri.password,
  }
end

#connect(environment, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mongo_mapper/connection.rb', line 64

def connect(environment, options={})
  raise 'Set config before connecting. MongoMapper.config = {...}' if config.blank?
  env = config_for_environment(environment)

  if env['options'].is_a? Hash
    options = env['options'].symbolize_keys.merge(options)
  end

  MongoMapper.connection = if env['hosts']
    Mongo::ReplSetConnection.new( *env['hosts'].push(options) )
  else
    Mongo::Connection.new(env['host'], env['port'], options)
  end

  MongoMapper.database = env['database']
  MongoMapper.database.authenticate(env['username'], env['password']) if env['username'] && env['password']
end

#connectionObject



12
13
14
# File 'lib/mongo_mapper/connection.rb', line 12

def connection
  @@connection ||= Mongo::Connection.new
end

#connection=(new_connection) ⇒ Object



17
18
19
# File 'lib/mongo_mapper/connection.rb', line 17

def connection=(new_connection)
  @@connection = new_connection
end

#databaseObject



33
34
35
36
37
# File 'lib/mongo_mapper/connection.rb', line 33

def database
  return nil if @@database_name.blank?

  @@database ||= MongoMapper.connection.db(@@database_name)
end

#database=(name) ⇒ Object



27
28
29
30
# File 'lib/mongo_mapper/connection.rb', line 27

def database=(name)
  @@database = nil
  @@database_name = name
end

#handle_passenger_forkingObject



88
89
90
91
92
93
94
# File 'lib/mongo_mapper/connection.rb', line 88

def handle_passenger_forking
  if defined?(PhusionPassenger)
    PhusionPassenger.on_event(:starting_worker_process) do |forked|
      connection.connect if forked
    end
  end
end

#loggerObject



22
23
24
# File 'lib/mongo_mapper/connection.rb', line 22

def logger
  connection.logger
end

#setup(config, environment, options = {}) ⇒ Object



82
83
84
85
86
# File 'lib/mongo_mapper/connection.rb', line 82

def setup(config, environment, options={})
  handle_passenger_forking
  self.config = config
  connect(environment, options)
end