Class: HuggORM::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hugg_orm/connection.rb

Constant Summary collapse

RETRIES =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/hugg_orm/connection.rb', line 44

def method_missing(meth, *args, &block)
  if connection.respond_to?(meth)
    method_retry(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#options(opts = {}) ⇒ Object (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/hugg_orm/connection.rb', line 10

def options
  @options
end

Class Method Details

.dbObject



12
13
14
# File 'lib/hugg_orm/connection.rb', line 12

def self.db
  @_db ||= self.new
end

Instance Method Details

#connect(opts = {}) ⇒ Object



26
27
28
29
30
# File 'lib/hugg_orm/connection.rb', line 26

def connect(opts = {})
  @options = opts
  @connection = PG.connect(opts)
  self
end

#connectionObject



16
17
18
19
20
# File 'lib/hugg_orm/connection.rb', line 16

def connection
  return @connection unless @connection.nil?
  connect(@options)
  @connection
end

#disconnectObject



32
33
34
35
36
# File 'lib/hugg_orm/connection.rb', line 32

def disconnect
  connection.finish if @connection
  @connection = nil
  self
end

#exec(*args) ⇒ Object



63
64
65
# File 'lib/hugg_orm/connection.rb', line 63

def exec(*args)
  method_retry(:exec, *args)
end

#exec_params(*args) ⇒ Object



67
68
69
# File 'lib/hugg_orm/connection.rb', line 67

def exec_params(*args)
  method_retry(:exec_params, *args)
end

#method_retry(method, *args, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/hugg_orm/connection.rb', line 52

def method_retry(method, *args, &block)
  tries ||= RETRIES + 1
  connection.send(method, *args, &block)
rescue PG::ConnectionBad, PG::AdminShutdown
  if (tries -= 1) > 0
    reconnect
    retry
  end
  raise
end

#reconnectObject



38
39
40
41
42
# File 'lib/hugg_orm/connection.rb', line 38

def reconnect
  disconnect
  connect(@options)
  self
end