Class: Wye::ActiveRecord::ConnectionHandler
- Inherits:
-
ActiveRecord::ConnectionAdapters::ConnectionHandler
- Object
- ActiveRecord::ConnectionAdapters::ConnectionHandler
- Wye::ActiveRecord::ConnectionHandler
- 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
-
#connection_pools ⇒ Object
readonly
Returns the value of attribute connection_pools.
-
#switch ⇒ Object
readonly
Returns the value of attribute switch.
Instance Method Summary collapse
- #alternates ⇒ Object
- #distributor(type) ⇒ Object
- #establish_connection(name, spec) ⇒ Object
-
#initialize(*arguments) ⇒ ConnectionHandler
constructor
A new instance of ConnectionHandler.
- #remove_connection(klass) ⇒ Object
- #retrieve_alternate_connection_pool(klass, alternate) ⇒ Object
- #retrieve_connection_pool(klass) ⇒ Object
- #retrieve_main_connection_pool(klass) ⇒ Object
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_pools ⇒ Object (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 |
#switch ⇒ Object (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
#alternates ⇒ Object
18 19 20 |
# File 'lib/wye/active_record/connection_handler.rb', line 18 def alternates @class_to_alternate_pools.map { |(klass,atp)| atp.map(&:first) }.flatten.uniq end |
#distributor(type) ⇒ Object
22 23 24 |
# File 'lib/wye/active_record/connection_handler.rb', line 22 def distributor(type) Distributor.new(type, alternates << nil) end |
#establish_connection(name, spec) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/wye/active_record/connection_handler.rb', line 26 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.to_sym] = @connection_pools[spec] end end end |
#remove_connection(klass) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/wye/active_record/connection_handler.rb', line 37 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
49 50 51 52 53 54 |
# File 'lib/wye/active_record/connection_handler.rb', line 49 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
63 64 65 66 67 68 69 |
# File 'lib/wye/active_record/connection_handler.rb', line 63 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
56 57 58 59 60 61 |
# File 'lib/wye/active_record/connection_handler.rb', line 56 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 |