Class: PSQLCM::Connection
- Inherits:
-
Delegator
- Object
- Delegator
- PSQLCM::Connection
- Defined in:
- lib/psql-cm/database.rb
Instance Method Summary collapse
-
#__getobj__ ⇒ Object
Delegator to PG::Connection.
- #__setobj__(object) ⇒ Object
- #close! ⇒ Object
- #connect! ⇒ Object
- #db ⇒ Object
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
-
#pgpass ⇒ Object
Ensure a pgpass entry exists for this connection.
-
#psql_args ⇒ Object
def pgpass.
- #reconnect!(name = ) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Connection
Returns a new instance of Connection.
5 6 7 8 9 10 11 |
# File 'lib/psql-cm/database.rb', line 5 def initialize( = {}) @config = ::PSQLCM.config.connection.merge() @config[:dbname] = [:dbname] if [:dbname] super # For delegator pattern: @delegated_object = db end |
Instance Method Details
#__getobj__ ⇒ Object
Delegator to PG::Connection
14 |
# File 'lib/psql-cm/database.rb', line 14 def __getobj__ ; @db end |
#__setobj__(object) ⇒ Object
15 |
# File 'lib/psql-cm/database.rb', line 15 def __setobj__(object) end |
#close! ⇒ Object
36 37 38 |
# File 'lib/psql-cm/database.rb', line 36 def close! @db.close end |
#connect! ⇒ Object
25 26 27 28 29 |
# File 'lib/psql-cm/database.rb', line 25 def connect! @db = PG.connect(@config) ObjectSpace.define_finalizer(self, proc { @db.close }) @db end |
#db ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/psql-cm/database.rb', line 17 def db unless @config[:dbname] == 'postgres' ::PSQLCM.sh "createdb #{psql_args} #{@config[:dbname]}" end @db || connect! end |
#pgpass ⇒ Object
Ensure a pgpass entry exists for this connection.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/psql-cm/database.rb', line 40 def pgpass # Ensure a pgpass entry exists for this connection. pgpass_file = File.join(ENV['HOME'], '.pgpass') FileUtils.touch(pgpass_file) unless File.exists?(pgpass_file) pgpass_line = [ @config[:host], @config[:port], @config[:dbname], @config[:user], @config[:password] ].join(':') content = File.read(pgpass_file).split("\n") unless content.detect{ |line| line == pgpass_line } File.open(pgpass_file, 'w') do |file| content << pgpass_line file.write(content.join("\n") + "\n") end File.chmod(0600, pgpass_file) end pgpass_line end |
#psql_args ⇒ Object
def pgpass
59 60 61 62 |
# File 'lib/psql-cm/database.rb', line 59 def psql_args pgpass "-h #{@config[:host]} -p #{@config[:port]} -U #{@config[:user]}" end |
#reconnect!(name = ) ⇒ Object
31 32 33 34 |
# File 'lib/psql-cm/database.rb', line 31 def reconnect!(name = @config[:dbname]) close! connect! end |