Class: Djoini::Connection
- Inherits:
-
Object
- Object
- Djoini::Connection
- Includes:
- Singleton
- Defined in:
- lib/djoini/connection.rb
Overview
Class to manage db connetion to postgres
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.load_database(db_name = 'postgres') ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/djoini/connection.rb', line 30 def self.load_database(db_name = 'postgres') _db_config_path = File.join(Dir.pwd, '/config/database.yml') fail unless File.file?(_db_config_path) _conn_config = YAML.load(File.read(_db_config_path)) Djoini::Connection.instance.establish_connection(_conn_config[db_name]) end |
Instance Method Details
#conn ⇒ Object
15 16 17 |
# File 'lib/djoini/connection.rb', line 15 def conn db || load_database end |
#establish_connection(params) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/djoini/connection.rb', line 19 def establish_connection(params) _adapter = params.fetch('adapter', 'postgres') _username = params.fetch('username') _password = params.fetch('password', '') _host = params.fetch('host', 'localhost') _port = params.fetch('port', '5432') _db_name = params.fetch('db_name') self.db = PG.connect("#{_adapter}://#{_username}:#{_password}@#{_host}:#{_port}/#{_db_name}") end |