Module: MongoMapper::Connection

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

Constant Summary collapse

@@connection =
nil
@@config =
nil
@@database =
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.



49
50
51
# File 'lib/mongo_mapper/connection.rb', line 49

def config_for_environment(environment)
  config[environment.to_s]
end

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mongo_mapper/connection.rb', line 53

def connect(environment, options={})
  raise 'Set config before connecting. MongoMapper.config = {...}' if config.blank?
  env = config_for_environment(environment).dup
  addresses_or_uri = env.delete('hosts') ||
                     env.delete('uri') ||
                     [env.delete('host')].compact

  if env['options'].is_a?(Hash)
    options = env.delete('options').symbolize_keys.merge(options)
  end
  #database etc are all options to Mongo::Client.new now
  options = env.symbolize_keys.merge(options)

  if options[:port]
    raise "port should be specified as part of the host or uri"
  end

  options[:read] = options[:read].to_sym if options[:read].is_a? String
  MongoMapper.connection = Mongo::Client.new(addresses_or_uri, options)
end

#connectionObject



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

def connection
  @@connection ||= Mongo::Client.new ['127.0.0.1:27017']
end

#connection=(new_connection) ⇒ Object



20
21
22
# File 'lib/mongo_mapper/connection.rb', line 20

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

#connection?Boolean

Returns:



15
16
17
# File 'lib/mongo_mapper/connection.rb', line 15

def connection?
  !!@@connection
end

#databaseObject



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

def database
  @@database ||= connection.database
end

#database=(name) ⇒ Object



30
31
32
# File 'lib/mongo_mapper/connection.rb', line 30

def database=(name)
  @@database = connection.use(name).database
end

#handle_passenger_forkingObject



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

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

#loggerObject



25
26
27
# File 'lib/mongo_mapper/connection.rb', line 25

def logger
  connection.logger
end

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



74
75
76
77
78
# File 'lib/mongo_mapper/connection.rb', line 74

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