Module: MongoMapper::Connection

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

Instance Method Summary collapse

Instance Method Details

#configObject



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

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

#config=(hash) ⇒ Object



36
37
38
# File 'lib/mongo_mapper/connection.rb', line 36

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:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mongo_mapper/connection.rb', line 46

def config_for_environment(environment)
  env = config[environment]
  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



61
62
63
64
65
66
67
# File 'lib/mongo_mapper/connection.rb', line 61

def connect(environment, options={})
  raise 'Set config before connecting. MongoMapper.config = {...}' if config.blank?
  env = config_for_environment(environment)
  MongoMapper.connection = Mongo::Connection.new(env['host'], env['port'], options)
  MongoMapper.database = env['database']
  MongoMapper.database.authenticate(env['username'], env['password']) if env['username'] && env['password']
end

#connectionObject



7
8
9
# File 'lib/mongo_mapper/connection.rb', line 7

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

#connection=(new_connection) ⇒ Object



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

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

#databaseObject



28
29
30
31
32
33
34
# File 'lib/mongo_mapper/connection.rb', line 28

def database
  if @@database_name.blank?
    raise 'You forgot to set the default database name: MongoMapper.database = "foobar"'
  end

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

#database=(name) ⇒ Object



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

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

#handle_passenger_forkingObject



75
76
77
78
79
80
81
# File 'lib/mongo_mapper/connection.rb', line 75

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

#loggerObject



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

def logger
  connection.logger
end

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



69
70
71
72
73
# File 'lib/mongo_mapper/connection.rb', line 69

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