module JamRuby
  class BaseManager

    attr_accessor :pg_conn
    
    def initialize(options={})
      @log = Logging.logger[self]
      @pg_conn = options[:conn]

      unless PG.threadsafe?
        raise Exception, "a non-threadsafe build of libpq is present."
      end
    end

       # Creates a connection manager, and associates the connection created by active_record with ourselves
    def self.active_record_transaction

      manager = self.new
      ActiveRecord::Base.connection_pool.with_connection do |connection|
        # create a transaction, and pass the current connection to ConnectionManager.
        # this lets the entire operation work with the same transaction,
        # across Rails ActiveRecord and the pg-gem based code in ConnectionManager.
        manager.pg_conn = connection.instance_variable_get("@connection")

        connection.transaction do
          yield manager
        end
      end
    end
  end
end