Module: RequestLogAnalyzer::Database::Connection

Included in:
RequestLogAnalyzer::Database
Defined in:
lib/request_log_analyzer/database/connection.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_string(string) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/request_log_analyzer/database/connection.rb', line 3

def self.from_string(string)
  hash = {}
  if string =~ /^(?:\w+=(?:[^;])*;)*\w+=(?:[^;])*$/
    string.scan(/(\w+)=([^;]*);?/) { |variable, value| hash[variable.to_sym] = value }
  elsif string =~ /^(\w+)\:\/\/(?:(?:([^:]+)(?:\:([^:]+))?\@)?([\w\.-]+)\/)?([\w\:\-\.\/]+)$/
    hash[:adapter], hash[:username], hash[:password], hash[:host], hash[:database] = $1, $2, $3, $4, $5
    hash.delete_if { |k, v| v.nil? }
  end
  return hash.empty? ? nil : hash
end

Instance Method Details

#connect(connection_identifier) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/request_log_analyzer/database/connection.rb', line 14

def connect(connection_identifier)
  if connection_identifier.kind_of?(Hash)
    ActiveRecord::Base.establish_connection(connection_identifier)
  elsif connection_identifier == ':memory:'
    ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
  elsif connection_hash = RequestLogAnalyzer::Database::Connection.from_string(connection_identifier)
    ActiveRecord::Base.establish_connection(connection_hash)
  elsif connection_identifier.kind_of?(String) # Normal SQLite 3 database file
    ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => connection_identifier)
  elsif connection_identifier.nil?
    nil
  else
    raise "Cannot connect with this connection_identifier: #{connection_identifier.inspect}"
  end
end

#connectionObject



34
35
36
# File 'lib/request_log_analyzer/database/connection.rb', line 34

def connection
  RequestLogAnalyzer::Database::Base.connection
end

#disconnectObject



30
31
32
# File 'lib/request_log_analyzer/database/connection.rb', line 30

def disconnect
  RequestLogAnalyzer::Database::Base.remove_connection
end