Class: DbGui::Model::DbConfig
- Inherits:
-
Struct
- Object
- Struct
- DbGui::Model::DbConfig
- Defined in:
- app/db_gui/model/db_config.rb
Overview
TODO consider renaming to DB connection
Constant Summary collapse
- FILE_DB_CONFIG =
File.(File.join('~', '.db_gui'))
Instance Attribute Summary collapse
-
#connected ⇒ Object
(also: #connected?)
Returns the value of attribute connected.
-
#db_command ⇒ Object
Returns the value of attribute db_command.
-
#db_command_result ⇒ Object
Returns the value of attribute db_command_result.
-
#dbname ⇒ Object
Returns the value of attribute dbname.
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#port ⇒ Object
Returns the value of attribute port.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #connect ⇒ Object
- #disconnect ⇒ Object
-
#initialize ⇒ DbConfig
constructor
A new instance of DbConfig.
- #io ⇒ Object
- #run_db_command ⇒ Object
- #run_io_command(command) ⇒ Object
- #toggle_connection ⇒ Object
Constructor Details
#initialize ⇒ DbConfig
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
#connected ⇒ Object 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_command ⇒ Object
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_result ⇒ Object
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 |
#dbname ⇒ Object
Returns the value of attribute dbname
6 7 8 |
# File 'app/db_gui/model/db_config.rb', line 6 def dbname @dbname end |
#host ⇒ Object
Returns the value of attribute host
6 7 8 |
# File 'app/db_gui/model/db_config.rb', line 6 def host @host end |
#password ⇒ Object
Returns the value of attribute password
6 7 8 |
# File 'app/db_gui/model/db_config.rb', line 6 def password @password end |
#port ⇒ Object
Returns the value of attribute port
6 7 8 |
# File 'app/db_gui/model/db_config.rb', line 6 def port @port end |
#username ⇒ Object
Returns the value of attribute username
6 7 8 |
# File 'app/db_gui/model/db_config.rb', line 6 def username @username end |
Instance Method Details
#connect ⇒ Object
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 |
#disconnect ⇒ Object
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 |
#io ⇒ Object
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_command ⇒ Object
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_connection ⇒ Object
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 |