Module: Purview::Mixins::Connection

Included in:
Databases::Base, Pullers::BaseSQL
Defined in:
lib/purview/mixins/connection.rb

Instance Method Summary collapse

Instance Method Details

#connectObject



4
5
6
# File 'lib/purview/mixins/connection.rb', line 4

def connect
  connection_type.connect(connection_opts)
end

#connection_optsObject



8
9
10
11
12
13
14
15
16
# File 'lib/purview/mixins/connection.rb', line 8

def connection_opts
  {
    :database => database_name,
    :host => database_host,
    :password => database_password,
    :port => database_port,
    :username => database_username,
  }
end

#with_new_connectionObject



18
19
20
21
22
# File 'lib/purview/mixins/connection.rb', line 18

def with_new_connection
  yield connection = connect
ensure
  connection.disconnect if connection
end

#with_new_or_existing_connection(opts = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/purview/mixins/connection.rb', line 24

def with_new_or_existing_connection(opts={})
  if existing_connection = opts[:connection]
    yield existing_connection
  else
    with_new_connection { |connection| yield connection }
  end
end

#with_transaction(connection) ⇒ Object



32
33
34
# File 'lib/purview/mixins/connection.rb', line 32

def with_transaction(connection)
  connection.with_transaction { yield }
end