Sha256: c55d9efd319e743c89244ca2526d1f3ea305af0a8b3ade7fd070a166506430dd

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

module Alf
  module Sequel
    class Connection
      module ConnectionMethods

        def connection_uri(with_password = false)
          if conn_spec.is_a?(Hash)
            cs = Tuple(self.conn_spec)
            adapter, host, port, database, user, password = cs.values_at(:adapter, :host, :port, :database, :user, :password)
            user = "#{user}:#{password}" if user and password and with_password
            user = "#{user}@"            if user
            host = "localhost"           if host.nil? and not(adapter =~ /sqlite/)
            host = "#{host}:#{port}"     if host and port
            host = "/#{user}#{host}"     if host or user
            "#{adapter}:/#{host}/#{database}"
          elsif conn_spec.is_a?(String)
            conn_spec
          else
            "Alf::Sequel::Connection(#{conn_spec})"
          end
        end

        def ping
          sequel_db.test_connection
        end

        def close
          @sequel_db.disconnect if @sequel_db && @sequel_db != conn_spec
        end

        def close!
          @sequel_db.disconnect if @sequel_db
        end

        def with_sequel_db
          yield(sequel_db)
        end

        # Yields a Sequel::Database object
        def sequel_db
          @sequel_db ||= Adapter.sequel_db(conn_spec)
          block_given? ? yield(@sequel_db) : @sequel_db
        end

      end # module ConnectionMethods
    end # module Connection
  end # module Sequel
end # module Alf

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alf-sequel-0.16.3 lib/alf/sequel/connection/connection_methods.rb
alf-sequel-0.16.2 lib/alf/sequel/connection/connection_methods.rb
alf-sequel-0.16.1 lib/alf/sequel/connection/connection_methods.rb
alf-sequel-0.16.0 lib/alf/sequel/connection/connection_methods.rb
alf-sequel-0.15.0 lib/alf/sequel/connection/connection_methods.rb