Class: Wye::ActiveRecord::ConnectionHandler

Inherits:
ActiveRecord::ConnectionAdapters::ConnectionHandler
  • Object
show all
Defined in:
lib/wye/active_record/connection_handler.rb

Overview

Implements an identical interface to the standard AR connection handler, but with support for alternate connection pools for each spec.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arguments) ⇒ ConnectionHandler

Returns a new instance of ConnectionHandler.



12
13
14
15
16
# File 'lib/wye/active_record/connection_handler.rb', line 12

def initialize(*arguments)
  super
  @class_to_alternate_pools = {}
  @switch = Switch.new(::ActiveRecord::Base)
end

Instance Attribute Details

#connection_poolsObject (readonly)

Returns the value of attribute connection_pools.



9
10
11
# File 'lib/wye/active_record/connection_handler.rb', line 9

def connection_pools
  @connection_pools
end

#switchObject (readonly)

Returns the value of attribute switch.



10
11
12
# File 'lib/wye/active_record/connection_handler.rb', line 10

def switch
  @switch
end

Instance Method Details

#establish_connection(name, spec) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/wye/active_record/connection_handler.rb', line 18

def establish_connection(name, spec)
  super.tap do
    @class_to_alternate_pools[name] ||= {}

    alternate_specs(spec).each do |alternate,spec|
      @connection_pools[spec] ||= ::ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
      @class_to_alternate_pools[name][alternate] = @connection_pools[spec]
    end
  end
end

#remove_connection(klass) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wye/active_record/connection_handler.rb', line 29

def remove_connection(klass)
  super.tap do
    if pools = @class_to_alternate_pools.delete(klass.name)
      pools.each do |(alternate,pool)|
        @connection_pools.delete pool.spec
        pool.automatic_reconnect = false
        pool.disconnect!
      end
    end
  end
end

#retrieve_alternate_connection_pool(klass, alternate) ⇒ Object



41
42
43
44
45
46
# File 'lib/wye/active_record/connection_handler.rb', line 41

def retrieve_alternate_connection_pool(klass, alternate)
  pools = @class_to_alternate_pools[klass.name]
  return pools[alternate] if pools && pools.include?(alternate)
  return nil if ::ActiveRecord::Base == klass
  retrieve_alternate_connection_pool(klass.superclass, alternate)
end

#retrieve_connection_pool(klass) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/wye/active_record/connection_handler.rb', line 55

def retrieve_connection_pool(klass)
  if alternate = switch.current_alternate(klass)
    retrieve_alternate_connection_pool(klass, alternate)
  else
    retrieve_main_connection_pool(klass)
  end
end

#retrieve_main_connection_pool(klass) ⇒ Object



48
49
50
51
52
53
# File 'lib/wye/active_record/connection_handler.rb', line 48

def retrieve_main_connection_pool(klass)
  pool = @class_to_pool[klass.name]
  return pool if pool
  return nil if ::ActiveRecord::Base == klass
  retrieve_main_connection_pool(klass.superclass)
end