Module: ConnectionManager::ConnectionBuilder

Defined in:
lib/connection_manager/connection_builder.rb

Instance Method Summary collapse

Instance Method Details

#ar_envObject

Get the current environment if defined Check for Rails, check for RACK_ENV, default to ‘development’



7
8
9
10
11
12
# File 'lib/connection_manager/connection_builder.rb', line 7

def ar_env
  return Rails.env if defined?(Rails)
  return RACK_ENV if defined?(RACK_ENV)
  return ENV["AR_ENV"] if ENV["AR_ENV"]
  "development"
end

#build_connection_class(class_name, connection_key) ⇒ Object

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



48
49
50
51
52
53
54
55
56
57
# File 'lib/connection_manager/connection_builder.rb', line 48

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.establish_managed_connection(connection_key)
    new_connection_class.use_database(new_connection_class.current_database_name)
  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.



40
41
42
43
44
# File 'lib/connection_manager/connection_builder.rb', line 40

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) 
  end
end

#configuration_keysObject

Grab only those connections that correspond to the current env. If env is blank it grabs all the connection keys

If you current environment valid database keys can be:

  • development

  • other_database_development

  • slave_database_development



21
22
23
# File 'lib/connection_manager/connection_builder.rb', line 21

def configuration_keys
  ActiveRecord::Base.configurations.keys.select{|n| n.match(ar_env_regex)}
end

#database_keys_for_auto_buildObject

Returns currently loaded configurations where :build_connection is true



31
32
33
34
35
36
37
# File 'lib/connection_manager/connection_builder.rb', line 31

def database_keys_for_auto_build
  ab_configs = []
  configuration_keys.each do |key|    
    ab_configs << key if (database_yml_attributes(key)[:build_connection_class] == true)
  end
  ab_configs
end

#database_yml_attributes(name_from_yml) ⇒ Object



25
26
27
28
# File 'lib/connection_manager/connection_builder.rb', line 25

def database_yml_attributes(name_from_yml)
  found = ActiveRecord::Base.configurations[name_from_yml]
  ActiveRecord::Base.configurations[name_from_yml].symbolize_keys if found
end