Class: Multidb::Candidate

Inherits:
Object
  • Object
show all
Defined in:
lib/multidb/candidate.rb

Constant Summary collapse

SPEC_NAME =
'ActiveRecord::Base'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, target) ⇒ Candidate

Returns a new instance of Candidate.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/multidb/candidate.rb', line 7

def initialize(name, target)
  @name = name

  case target
  when Hash
    target = target.merge(name: 'primary')

    @connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
    @connection_handler.establish_connection(target)
  when ActiveRecord::ConnectionAdapters::ConnectionHandler
    @connection_handler = target
  else
    raise ArgumentError, 'Connection handler not passed to target'
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



35
36
37
# File 'lib/multidb/candidate.rb', line 35

def name
  @name
end

Instance Method Details

#connection(&block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/multidb/candidate.rb', line 23

def connection(&block)
  if block_given?
    @connection_handler.retrieve_connection_pool(SPEC_NAME).with_connection(&block)
  else
    @connection_handler.retrieve_connection(SPEC_NAME)
  end
end

#disconnect!Object



31
32
33
# File 'lib/multidb/candidate.rb', line 31

def disconnect!
  @connection_handler.clear_all_connections!
end