Class: Noiseless::ConnectionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/noiseless/connection_manager.rb

Instance Method Summary collapse

Constructor Details

#initializeConnectionManager

Returns a new instance of ConnectionManager.



5
6
7
8
# File 'lib/noiseless/connection_manager.rb', line 5

def initialize
  @clients = {}
  @configs = {}
end

Instance Method Details

#client(name = :primary) ⇒ Object

Retrieve a client; defaults to :primary



16
17
18
19
20
21
22
23
24
# File 'lib/noiseless/connection_manager.rb', line 16

def client(name = :primary)
  name = name.to_sym

  # Lazy-load the adapter only when actually used
  @clients[name] ||= begin
    config = @configs.fetch(name) { raise "Unknown connection: #{name}" }
    Adapters.lookup(config[:adapter], hosts: config[:hosts])
  end
end

#register(name, adapter:, hosts:) ⇒ Object

Register a named client statically from YAML (boot-time only)



11
12
13
# File 'lib/noiseless/connection_manager.rb', line 11

def register(name, adapter:, hosts:)
  @configs[name.to_sym] = { adapter: adapter, hosts: hosts }
end