class DatabaseSwitcher

Attributes

config_file_path[R]
database_name[R]

Public Class Methods

new(database_name, config_file_path: 'config/database-switcher.yml') click to toggle source
# File lib/database-switcher.rb, line 4
def initialize(database_name, config_file_path: 'config/database-switcher.yml')
  @database_name = database_name
  @config_file_path = config_file_path
  @main_db_params = ActiveRecord::Base.configurations[Rails.env] 
end

Public Instance Methods

perform(&block) click to toggle source
# File lib/database-switcher.rb, line 10
def perform(&block)
  switch_db_connection
  return_value = block.call
  restore_main_db_connection
  return_value
end

Private Instance Methods

config() click to toggle source
# File lib/database-switcher.rb, line 27
def config
  @config ||= YAML::load(config_file)
end
config_file() click to toggle source
# File lib/database-switcher.rb, line 31
def config_file
  @config_file ||= File.read(config_file_path)
end
restore_main_db_connection() click to toggle source
# File lib/database-switcher.rb, line 35
def restore_main_db_connection
  ActiveRecord::Base.establish_connection(@main_db_params)
end
switch_db_connection() click to toggle source
# File lib/database-switcher.rb, line 19
def switch_db_connection
  ActiveRecord::Base.establish_connection(target_db_params)
end
target_db_params() click to toggle source
# File lib/database-switcher.rb, line 23
def target_db_params
  config[database_name]    
end