module MultipleDbs

MultipleDbs The MultipleDbs module.

Constants

VERSION

Public Class Methods

run_setup() click to toggle source
# File lib/multiple_dbs/multi_connectable.rb, line 155
def run_setup
  Object.class_eval do
    def self.const_missing(c)
      matches = []
      db_matches = []
      MultipleDbs::DBS.each do |db|
        matches << c.to_s.scan(
        Regexp.new('(([A-Z]){1}([a-zA-Z]|[0-9])*)+' + db.to_s.camelize + '$')
        )
        db_matches << db
        break if matches.flatten.any?
      end
      const_temp = Object.const_get(matches.first).mdb(db_matches.last) if matches.flatten!.any?
      MultipleDbs.validate_connection(db_matches.last) if matches.any? and const_temp.to_s.eql?(c.to_s)
      return const_temp if matches.any? and const_temp.to_s.eql?(c.to_s)
      super
    end
  end if Rails.env.development? and defined? MultipleDbs and defined? MultipleDbs::DBS

  DBS.each do |db|
    Object.const_set("DBConnection" + db.to_s.camelize , Class.new do
      attr_accessor :connection
      @connection = YAML::load(ERB.new(File.read(Rails.root.join("config/multiple_dbs", db.to_s.downcase  + "_database.yml"))).result)[Rails.env]
      def self.connection
        @connection
      end
    end)
  end

  DBS.each do |db|
    conn_config = const_get("DBConnection" + db.to_s.camelize).connection
    if conn_config["database"] != ActiveRecord::Base.connection_config[:database]
      conn_klass = Object.const_set("Conn" + db.to_s.camelize, Class.new(ApplicationRecord))
      conn_klass.establish_connection conn_config
      conn_klass.connection
    else
      Object.const_set("Conn" + db.to_s.camelize, ActiveRecord::Base)
    end
  end

  ActiveRecord::Base.clear_all_connections!
end
validate_connection(db) click to toggle source
# File lib/multiple_dbs/multi_connectable.rb, line 3
def validate_connection(db)
  conn_klass = const_get("Conn#{db.to_s.camelize}")
  if conn_klass
    ActiveRecord::Base.connection_handler.connection_pools.each do |pool|
      Rails.logger.info "CONN FOUND" and return true if pool.spec.name == conn_klass.to_s
    end
    Rails.logger.info " CONN NOT FOUND. CONNECTION TO #{conn_klass}"
    conn_klass.connection and return true
  else
    raise Exception.new("Undefined constant #{conn_klass}")
  end
end

Private Class Methods

connection() click to toggle source
# File lib/multiple_dbs/multi_connectable.rb, line 178
def self.connection
  @connection
end
const_missing(c) click to toggle source
Calls superclass method
# File lib/multiple_dbs/multi_connectable.rb, line 157
def self.const_missing(c)
  matches = []
  db_matches = []
  MultipleDbs::DBS.each do |db|
    matches << c.to_s.scan(
    Regexp.new('(([A-Z]){1}([a-zA-Z]|[0-9])*)+' + db.to_s.camelize + '$')
    )
    db_matches << db
    break if matches.flatten.any?
  end
  const_temp = Object.const_get(matches.first).mdb(db_matches.last) if matches.flatten!.any?
  MultipleDbs.validate_connection(db_matches.last) if matches.any? and const_temp.to_s.eql?(c.to_s)
  return const_temp if matches.any? and const_temp.to_s.eql?(c.to_s)
  super
end

Private Instance Methods

run_setup() click to toggle source
# File lib/multiple_dbs/multi_connectable.rb, line 155
def run_setup
  Object.class_eval do
    def self.const_missing(c)
      matches = []
      db_matches = []
      MultipleDbs::DBS.each do |db|
        matches << c.to_s.scan(
        Regexp.new('(([A-Z]){1}([a-zA-Z]|[0-9])*)+' + db.to_s.camelize + '$')
        )
        db_matches << db
        break if matches.flatten.any?
      end
      const_temp = Object.const_get(matches.first).mdb(db_matches.last) if matches.flatten!.any?
      MultipleDbs.validate_connection(db_matches.last) if matches.any? and const_temp.to_s.eql?(c.to_s)
      return const_temp if matches.any? and const_temp.to_s.eql?(c.to_s)
      super
    end
  end if Rails.env.development? and defined? MultipleDbs and defined? MultipleDbs::DBS

  DBS.each do |db|
    Object.const_set("DBConnection" + db.to_s.camelize , Class.new do
      attr_accessor :connection
      @connection = YAML::load(ERB.new(File.read(Rails.root.join("config/multiple_dbs", db.to_s.downcase  + "_database.yml"))).result)[Rails.env]
      def self.connection
        @connection
      end
    end)
  end

  DBS.each do |db|
    conn_config = const_get("DBConnection" + db.to_s.camelize).connection
    if conn_config["database"] != ActiveRecord::Base.connection_config[:database]
      conn_klass = Object.const_set("Conn" + db.to_s.camelize, Class.new(ApplicationRecord))
      conn_klass.establish_connection conn_config
      conn_klass.connection
    else
      Object.const_set("Conn" + db.to_s.camelize, ActiveRecord::Base)
    end
  end

  ActiveRecord::Base.clear_all_connections!
end
validate_connection(db) click to toggle source
# File lib/multiple_dbs/multi_connectable.rb, line 3
def validate_connection(db)
  conn_klass = const_get("Conn#{db.to_s.camelize}")
  if conn_klass
    ActiveRecord::Base.connection_handler.connection_pools.each do |pool|
      Rails.logger.info "CONN FOUND" and return true if pool.spec.name == conn_klass.to_s
    end
    Rails.logger.info " CONN NOT FOUND. CONNECTION TO #{conn_klass}"
    conn_klass.connection and return true
  else
    raise Exception.new("Undefined constant #{conn_klass}")
  end
end