Module: Fabes::ConnectionHandling

Extended by:
ConnectionHandling
Included in:
ConnectionHandling
Defined in:
lib/fabes/connection_adapters/redis_adapter.rb,
lib/fabes/connection_adapters/abstract_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.redis_connection(db) ⇒ Object



3
4
5
6
7
# File 'lib/fabes/connection_adapters/redis_adapter.rb', line 3

def self.redis_connection(db)
  require 'redis'
  redis = ::Redis.new db
  ConnectionAdapters::RedisAdapter.new redis
end

Instance Method Details

#connection_url_to_hash(url) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fabes/connection_adapters/abstract_adapter.rb', line 16

def connection_url_to_hash(url)
  url ||= ''
  uri = URI.parse url
  spec = {
    host:     uri.host,
    port:     uri.port,
    adapter:  uri.scheme,
    username: uri.user,
    password: uri.password,
    database: uri.path.sub(%r{^/}, '')
  }
  spec.reject! {|_, value| !value}
  {adapter: 'redis'}.merge spec
end

#establish_connection(db) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/fabes/connection_adapters/abstract_adapter.rb', line 6

def establish_connection(db)
  database = connection_url_to_hash(db)
  adapter = database.delete :adapter
  adapter_method = "#{adapter}_connection"
  send adapter_method, database
rescue
  raise "Could not find #{adapter} adapter!"
end