Class: Purobu::DatabaseConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/purobu/database_connection.rb

Defined Under Namespace

Modules: Helper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ DatabaseConnection

Returns a new instance of DatabaseConnection.



8
9
10
# File 'lib/purobu/database_connection.rb', line 8

def initialize(url)
  @url = url
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/purobu/database_connection.rb', line 6

def url
  @url
end

Instance Method Details

#connectObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/purobu/database_connection.rb', line 12

def connect
  attempts = 0
  begin
    connection = Sequel.connect(url)
  rescue => e
    Purobu.log(connection_failure: "Connection attempt to '#{@url}' failed.")
  end

  if block_given?
    begin
      yield connection
    ensure
      connection.disconnect
    end
  else
    return connection
  end
end