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
- #config ⇒ Object
- #config=(hash) ⇒ Object
- #config_for_environment(environment) ⇒ Object private
- #connect(environment, options = {}) ⇒ Object
- #connection ⇒ Object
- #connection=(new_connection) ⇒ Object
- #connection? ⇒ Boolean
- #database ⇒ Object
- #database=(name) ⇒ Object
- #handle_passenger_forking ⇒ Object
- #logger ⇒ Object
- #setup(config, environment, options = {}) ⇒ Object
Instance Method Details
#config ⇒ Object
47 48 49 50 |
# File 'lib/mongo_mapper/connection.rb', line 47 def config raise 'Set config before connecting. MongoMapper.config = {...}' unless defined?(@@config) @@config end |
#config=(hash) ⇒ Object
43 44 45 |
# File 'lib/mongo_mapper/connection.rb', line 43 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.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mongo_mapper/connection.rb', line 53 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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/mongo_mapper/connection.rb', line 68 def connect(environment, ={}) raise 'Set config before connecting. MongoMapper.config = {...}' if config.blank? env = config_for_environment(environment) if env['options'].is_a?(Hash) = env['options'].symbolize_keys.merge() end [:read] = [:read].to_sym if [:read].is_a? String if env.key?('ssl') [:ssl] = env['ssl'] end MongoMapper.connection = if env.key?('hosts') klass = (env.key?("mongos") || env.key?("sharded")) ? Mongo::MongoShardedClient : Mongo::MongoReplicaSetClient if env['hosts'].first.is_a?(String) klass.new( env['hosts'], ) else klass.new( *env['hosts'].push() ) end else Mongo::MongoClient.new(env['host'], env['port'], ) end MongoMapper.database = env['database'] MongoMapper.database.authenticate(env['username'], env['password']) if env['username'] && env['password'] end |
#connection ⇒ Object
12 13 14 |
# File 'lib/mongo_mapper/connection.rb', line 12 def connection @@connection ||= Mongo::MongoClient.new end |
#connection=(new_connection) ⇒ Object
21 22 23 |
# File 'lib/mongo_mapper/connection.rb', line 21 def connection=(new_connection) @@connection = new_connection end |
#connection? ⇒ Boolean
16 17 18 |
# File 'lib/mongo_mapper/connection.rb', line 16 def connection? !!@@connection end |
#database ⇒ Object
37 38 39 40 41 |
# File 'lib/mongo_mapper/connection.rb', line 37 def database return nil if @@database_name.blank? @@database ||= MongoMapper.connection.db(@@database_name) end |
#database=(name) ⇒ Object
31 32 33 34 |
# File 'lib/mongo_mapper/connection.rb', line 31 def database=(name) @@database = nil @@database_name = name end |
#handle_passenger_forking ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/mongo_mapper/connection.rb', line 102 def handle_passenger_forking # :nocov: if defined?(PhusionPassenger) PhusionPassenger.on_event(:starting_worker_process) do |forked| connection.connect if forked end end # :nocov: end |
#logger ⇒ Object
26 27 28 |
# File 'lib/mongo_mapper/connection.rb', line 26 def logger connection.logger end |
#setup(config, environment, options = {}) ⇒ Object
96 97 98 99 100 |
# File 'lib/mongo_mapper/connection.rb', line 96 def setup(config, environment, ={}) handle_passenger_forking self.config = config connect(environment, ) end |