Sha256: cadff6d5cb58c46da7a296740c567ec3f3d37776219363e958e990cf31620b0b

Contents?: true

Size: 1.29 KB

Versions: 6

Compression:

Stored size: 1.29 KB

Contents

module CouchPillow

  module MultiDBDirective

    # Set a DB connection. Overrides the default CouchPillow.db connection
    # for the first time this method gets called. Subsequent calls will set
    # secondary connections, which will only be used for write only.
    #
    # Example:
    #    db primary_db # use for both read and write
    #    db backup_db1 # write only
    #    db backup_db2 # write only
    #
    def db conn
      # set the primary db connection
      @primary_db ||= conn

      # insert as backup db connections
      if conn && @primary_db && conn != @primary_db
        _secondary_dbs << conn
      end
    end


    def _default_db
      @default_db ||= (@primary_db || CouchPillow.db)
    end


    def _secondary_dbs
      @secondary_dbs ||= []
    end


    def _write_to_secondary_dbs &block
      unless _secondary_dbs.empty?
        _threads << Thread.new do
          _secondary_dbs.each do |db|
            block.call(db)
          end
        end
      end
    end


    def _threads
      @threads ||= []
    end


    # Blocks until all pending tasks has completed.
    # Returns the result of those tasks in an array.
    #
    def wait
      result = []
      until _threads.empty?
        f = _threads.shift
        result << f.value
      end

      result
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
couchpillow-0.4.12 lib/couchpillow/directives/multi_db.rb
couchpillow-0.4.11 lib/couchpillow/directives/multi_db.rb
couchpillow-0.4.10 lib/couchpillow/directives/multi_db.rb
couchpillow-0.4.9 lib/couchpillow/directives/multi_db.rb
couchpillow-0.4.8 lib/couchpillow/directives/multi_db.rb
couchpillow-0.4.7 lib/couchpillow/directives/multi_db.rb