Class: DbGui::Model::DbConfig

Inherits:
Struct
  • Object
show all
Defined in:
app/db_gui/model/db_config.rb

Overview

TODO consider renaming to DB connection

Constant Summary collapse

FILE_DB_CONFIG =
File.expand_path(File.join('~', '.db_gui'))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDbConfig

Returns a new instance of DbConfig.



14
15
16
17
18
19
# File 'app/db_gui/model/db_config.rb', line 14

def initialize
  self.port = 5432 # PostgreSQL default port
  self.db_command_result = ''
  load_db_config
  connect if to_a.none? {|value| value.nil? || (value.respond_to?(:empty?) && value.empty?) }
end

Instance Attribute Details

#connectedObject Also known as: connected?

Returns the value of attribute connected.



9
10
11
# File 'app/db_gui/model/db_config.rb', line 9

def connected
  @connected
end

#db_commandObject

Returns the value of attribute db_command.



12
13
14
# File 'app/db_gui/model/db_config.rb', line 12

def db_command
  @db_command
end

#db_command_resultObject

Returns the value of attribute db_command_result.



11
12
13
# File 'app/db_gui/model/db_config.rb', line 11

def db_command_result
  @db_command_result
end

#dbnameObject

Returns the value of attribute dbname

Returns:

  • (Object)

    the current value of dbname



6
7
8
# File 'app/db_gui/model/db_config.rb', line 6

def dbname
  @dbname
end

#hostObject

Returns the value of attribute host

Returns:

  • (Object)

    the current value of host



6
7
8
# File 'app/db_gui/model/db_config.rb', line 6

def host
  @host
end

#passwordObject

Returns the value of attribute password

Returns:

  • (Object)

    the current value of password



6
7
8
# File 'app/db_gui/model/db_config.rb', line 6

def password
  @password
end

#portObject

Returns the value of attribute port

Returns:

  • (Object)

    the current value of port



6
7
8
# File 'app/db_gui/model/db_config.rb', line 6

def port
  @port
end

#usernameObject

Returns the value of attribute username

Returns:

  • (Object)

    the current value of username



6
7
8
# File 'app/db_gui/model/db_config.rb', line 6

def username
  @username
end

Instance Method Details

#connectObject



29
30
31
32
33
# File 'app/db_gui/model/db_config.rb', line 29

def connect
  io
  self.connected = true
  save_db_config
end

#disconnectObject



35
36
37
38
39
# File 'app/db_gui/model/db_config.rb', line 35

def disconnect
  io.close
  @io = nil
  self.connected = false
end

#ioObject



41
42
43
# File 'app/db_gui/model/db_config.rb', line 41

def io
  @io ||= IO.popen("PGPASSWORD=\"#{password}\" psql --host=#{host} --port=#{port} --username=#{username} --dbname=#{dbname}", 'r+')
end

#run_db_commandObject



55
56
57
# File 'app/db_gui/model/db_config.rb', line 55

def run_db_command
  run_io_command(db_command)
end

#run_io_command(command) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'app/db_gui/model/db_config.rb', line 45

def run_io_command(command)
  @io_command_try ||= 0
  @io_command_try += 1
  io.puts(command)
  read_io_into_db_command_result
rescue Errno::EPIPE => e
  @io = nil
  run_io_command(command) unless @io_command_try > 1
end

#toggle_connectionObject



21
22
23
24
25
26
27
# File 'app/db_gui/model/db_config.rb', line 21

def toggle_connection
  if connected?
    disconnect
  else
    connect
  end
end