Module: Wonkavision::Mongo
Defined Under Namespace
Classes: Connection
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
- #database ⇒ Object
- #database=(name) ⇒ Object
- #handle_passenger_forking ⇒ Object
- #initialize(connection = nil) ⇒ Object
- #logger ⇒ Object
- #setup(config, environment, options = {}) ⇒ Object
Instance Method Details
#config ⇒ Object
48 49 50 51 |
# File 'lib/wonkavision/persistence/mongo.rb', line 48 def config raise 'Set config before connecting. config = {...}' unless defined?(@config) @config end |
#config=(hash) ⇒ Object
44 45 46 |
# File 'lib/wonkavision/persistence/mongo.rb', line 44 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.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/wonkavision/persistence/mongo.rb', line 54 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
69 70 71 72 73 74 75 |
# File 'lib/wonkavision/persistence/mongo.rb', line 69 def connect(environment, ={}) raise 'Set config before connecting. config = {...}' if config.blank? env = config_for_environment(environment) self.connection = ::Mongo::Connection.new(env['host'], env['port'], ) self.database = env['database'] database.authenticate(env['username'], env['password']) if env['username'] && env['password'] end |
#connection ⇒ Object
15 16 17 |
# File 'lib/wonkavision/persistence/mongo.rb', line 15 def connection @connection ||= ::Mongo::Connection.new end |
#connection=(new_connection) ⇒ Object
20 21 22 |
# File 'lib/wonkavision/persistence/mongo.rb', line 20 def connection=(new_connection) @connection = new_connection end |
#database ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/wonkavision/persistence/mongo.rb', line 36 def database if @database_name.blank? raise 'You forgot to set the default database name: database = "foobar"' end @database ||= connection.db(@database_name) end |
#database=(name) ⇒ Object
30 31 32 33 |
# File 'lib/wonkavision/persistence/mongo.rb', line 30 def database=(name) @database = nil @database_name = name end |
#handle_passenger_forking ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/wonkavision/persistence/mongo.rb', line 83 def handle_passenger_forking if defined?(PhusionPassenger) PhusionPassenger.on_event(:starting_worker_process) do |forked| connection.connect if forked end end end |
#initialize(connection = nil) ⇒ Object
10 11 12 |
# File 'lib/wonkavision/persistence/mongo.rb', line 10 def initialize(connection=nil) @connection = connection end |
#logger ⇒ Object
25 26 27 |
# File 'lib/wonkavision/persistence/mongo.rb', line 25 def logger connection.logger end |
#setup(config, environment, options = {}) ⇒ Object
77 78 79 80 81 |
# File 'lib/wonkavision/persistence/mongo.rb', line 77 def setup(config, environment, ={}) handle_passenger_forking self.config = config connect(environment, ) end |