Sha256: 8ec388d4f407f6b6366294b9feabedd909e0bfc0b6da74f1e51b86159db89fc8

Contents?: true

Size: 780 Bytes

Versions: 8

Compression:

Stored size: 780 Bytes

Contents

module FreshConnection
  class AccessControl
    class << self
      def force_master_access(&block)
        switch_to(:master, &block)
      end

      def access(enable_slave_access, &block)
        if access_db
          block.call
        else
          db = enable_slave_access ? :slave : :master
          switch_to(db, &block)
        end
      end

      def slave_access?
        access_db == :slave
      end

      private

      def switch_to(new_db)
        old_db = access_db
        access_to(new_db)
        yield
      ensure
        access_to(old_db)
      end

      def access_db
        Thread.current[:fresh_connection_access_target]
      end

      def access_to(db)
        Thread.current[:fresh_connection_access_target] = db
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
fresh_connection-0.4.4 lib/fresh_connection/access_control.rb
fresh_connection-1.0.1 lib/fresh_connection/access_control.rb
fresh_connection-1.0.0 lib/fresh_connection/access_control.rb
fresh_connection-0.4.3 lib/fresh_connection/access_control.rb
fresh_connection-0.4.2 lib/fresh_connection/access_control.rb
fresh_connection-0.4.1 lib/fresh_connection/access_control.rb
fresh_connection-0.4.0 lib/fresh_connection/access_control.rb
fresh_connection-0.3.0 lib/fresh_connection/access_control.rb