Sha256: 53461c2d01a3396d36acfa868dce0fb221c0385ebc138bc5a3443daa8351ecb9

Contents?: true

Size: 1.43 KB

Versions: 20

Compression:

Stored size: 1.43 KB

Contents

module DbCharmer
  module DbMagic
    module ClassMethods
      def db_magic(opt = {})
        # Make sure we could use our connections management here
        hijack_connection!

        # Should requested connections exist in the config?
        should_exist = opt[:should_exist] || DbCharmer.connections_should_exist?

        # Main connection management
        db_magic_connection(opt[:connection], should_exist) if opt[:connection]

        # Set up slaves pool
        opt[:slaves] ||= []
        opt[:slaves] << opt[:slave] if opt[:slave]
        db_magic_slaves(opt[:slaves], should_exist) if opt[:slaves].any?
        
        # Setup inheritance magic
        setup_children_magic(opt)
      end

    private

      def setup_children_magic(opt)
        self.db_charmer_opts = opt.clone
        
        def self.inherited(child)
          child.db_magic(self.db_charmer_opts)
          super
        end
      end

      def db_magic_connection(conn, should_exist = false)
        switch_connection_to(conn, should_exist)
      end

      def db_magic_slaves(slaves, should_exist = false)
        self.db_charmer_slaves = slaves.collect do |slave|
          coerce_to_connection_proxy(slave, should_exist)
        end

        self.extend(DbCharmer::FinderOverrides::ClassMethods)
        self.send(:include, DbCharmer::FinderOverrides::InstanceMethods)
        self.extend(DbCharmer::MultiDbProxy::MasterSlaveClassMethods)
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 3 rubygems

Version Path
glebpom-db-charmer-1.1.5 lib/db_charmer/db_magic.rb
glebpom-db-charmer-1.3.0 lib/db_charmer/db_magic.rb
kovyrin-db-charmer-1.0.1 lib/db_charmer/db_magic.rb
kovyrin-db-charmer-1.1.0 lib/db_charmer/db_magic.rb
kovyrin-db-charmer-1.1.2 lib/db_charmer/db_magic.rb
kovyrin-db-charmer-1.1.3 lib/db_charmer/db_magic.rb
kovyrin-db-charmer-1.2.0 lib/db_charmer/db_magic.rb
db-charmer-1.4.6 lib/db_charmer/db_magic.rb
db-charmer-1.4.5 lib/db_charmer/db_magic.rb
db-charmer-1.4.4 lib/db_charmer/db_magic.rb
db-charmer-1.4.3 lib/db_charmer/db_magic.rb
db-charmer-1.4.1 lib/db_charmer/db_magic.rb
db-charmer-1.4.0 lib/db_charmer/db_magic.rb
db-charmer-1.3.4 lib/db_charmer/db_magic.rb
db-charmer-1.3.3 lib/db_charmer/db_magic.rb
db-charmer-1.3.2 lib/db_charmer/db_magic.rb
db-charmer-1.3.1 lib/db_charmer/db_magic.rb
glebpom-db-charmer-1.4.3 lib/db_charmer/db_magic.rb
glebpom-db-charmer-1.4.2 lib/db_charmer/db_magic.rb
glebpom-db-charmer-1.4.0 lib/db_charmer/db_magic.rb