Class: ConAir::ConnectionHandler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(swap_class, hijacked_spec, pools = {}) ⇒ ConnectionHandler

Returns a new instance of ConnectionHandler.



7
8
9
10
11
12
13
14
15
# File 'lib/con_air/connection_handler.rb', line 7

def initialize(swap_class, hijacked_spec, pools = {})
  super(pools)

  @hijacked_spec = hijacked_spec
  @swap_class = swap_class

  # Init all connections after switching handler
  ActiveRecord::Base.establish_connection
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



3
4
5
# File 'lib/con_air/connection_handler.rb', line 3

def active
  @active
end

#hijacked_specObject (readonly)

Returns the value of attribute hijacked_spec.



4
5
6
# File 'lib/con_air/connection_handler.rb', line 4

def hijacked_spec
  @hijacked_spec
end

#swap_classObject (readonly)

Returns the value of attribute swap_class.



5
6
7
# File 'lib/con_air/connection_handler.rb', line 5

def swap_class
  @swap_class
end

Instance Method Details

#establish_connection(name, spec) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/con_air/connection_handler.rb', line 17

def establish_connection(name, spec)
  if name == swap_class.name
    @connection_pools[@hijacked_spec] ||= ActiveRecord::ConnectionAdapters::ConnectionPool.new(@hijacked_spec)
    @class_to_pool[name] = @connection_pools[@hijacked_spec]
  else
    super
  end
end

#exist?(config, klass) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/con_air/connection_handler.rb', line 26

def exist?(config, klass)
  hijacked_spec.config == config && swap_class == klass
end