Class: ConnectionManager::Builder

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

Class Method Summary collapse

Class Method Details

.build_connection_class(class_name, connection_key) ⇒ Object

Build connection classes base on the supplied class name and connection key from database.yml



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/connection_manager/builder.rb', line 19

def build_connection_class(class_name,connection_key)
  begin
    class_name.constantize
  rescue NameError
    klass = Class.new(ActiveRecord::Base)
    new_connection_class = Object.const_set(class_name, klass)
    new_connection_class.abstract_class = true
    new_connection_class.establish_connection(connection_key.to_sym)
    ConnectionManager.logger.info "Connection::Manager built: #{class_name} for #{connection_key}" if ConnectionManager.logger
    new_connection_class
  end
end

.build_connection_classes(database_keys_to_use = database_keys_for_auto_build) ⇒ Object

Builds connection classes using the database keys provided; expects an array.



11
12
13
14
15
# File 'lib/connection_manager/builder.rb', line 11

def build_connection_classes(database_keys_to_use=database_keys_for_auto_build)
  database_keys_to_use.each do |key|
    build_connection_class(connection_class_name(key),key.to_sym)
  end
end

.database_keys_for_auto_buildObject

Grab only those configurations that correspond to the current env (If env is blank it grabs all the connection keys) and where :build_connection_class is true



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

def database_keys_for_auto_build
  ActiveRecord::Base.configurations.select{ |k,v| v[:build_connection_class] && k.match(env_regex)}.keys
end

.env_regexObject



32
33
34
35
36
# File 'lib/connection_manager/builder.rb', line 32

def env_regex
  return @env_regex if @env_regex
  s = "#{ConnectionManager.env}$"
  @env_regex = Regexp.new("(#{s})")
end