module RequestLogAnalyzer::Database::Connection

Public Class Methods

from_string(string) click to toggle source
   # File lib/request_log_analyzer/database/connection.rb
 2 def self.from_string(string)
 3   hash = {}
 4   if string =~ /^(?:\w+=(?:[^;])*;)*\w+=(?:[^;])*$/
 5     string.scan(/(\w+)=([^;]*);?/) { |variable, value| hash[variable.to_sym] = value }
 6   elsif string =~ /^(\w+)\:\/\/(?:(?:([^:]+)(?:\:([^:]+))?\@)?([\w\.-]+)\/)?([\w\:\-\.\/]+)$/
 7     hash[:adapter], hash[:username], hash[:password], hash[:host], hash[:database] = Regexp.last_match[1], Regexp.last_match[2], Regexp.last_match[3], Regexp.last_match[4], Regexp.last_match[5]
 8     hash.delete_if { |_k, v| v.nil? }
 9   end
10   hash.empty? ? nil : hash
11 end

Public Instance Methods

connect(connection_identifier) click to toggle source
   # File lib/request_log_analyzer/database/connection.rb
13 def connect(connection_identifier)
14   if connection_identifier.is_a?(Hash)
15     ActiveRecord::Base.establish_connection(connection_identifier)
16   elsif connection_identifier == ':memory:'
17     ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
18   elsif connection_hash = RequestLogAnalyzer::Database::Connection.from_string(connection_identifier)
19     ActiveRecord::Base.establish_connection(connection_hash)
20   elsif connection_identifier.is_a?(String) # Normal SQLite 3 database file
21     ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: connection_identifier)
22   elsif connection_identifier.nil?
23     nil
24   else
25     fail "Cannot connect with this connection_identifier: #{connection_identifier.inspect}"
26   end
27 end
connection() click to toggle source
   # File lib/request_log_analyzer/database/connection.rb
33 def connection
34   RequestLogAnalyzer::Database::Base.connection
35 end
disconnect() click to toggle source
   # File lib/request_log_analyzer/database/connection.rb
29 def disconnect
30   RequestLogAnalyzer::Database::Base.remove_connection
31 end