Class: Alf::Rack::Config

Inherits:
Support::Config
  • Object
show all
Defined in:
lib/alf/rack/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject (readonly)

The current database connection



35
36
37
# File 'lib/alf/rack/config.rb', line 35

def connection
  @connection
end

Instance Method Details

#connect(&bl) ⇒ Object

Connects to the database, starts a transaction if required, then yields the block with the connection object.

The connection is kept under an instance variable and can be later obtained through the connection accessor. Do NOT use this method without having called dup on the config object as it relies on shared mutable state.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/alf/rack/config.rb', line 44

def connect(&bl)
  return yield unless database
  database.connect(connection_options) do |conn|
    @connection = conn
    if transactional?
      conn.in_transaction(transaction_options){ yield(conn) }
    else
      yield(conn)
    end
  end
end

#database=(db) ⇒ Object

Sets the database, coercing it if required



18
19
20
# File 'lib/alf/rack/config.rb', line 18

def database=(db)
  @database = db.is_a?(Database) ? db : Alf.database(db)
end

#reconnect(opts) ⇒ Object

Reconnect with new options. This method is provided if you want to use another viewpoint than the original one in the middle of the request treatment.



59
60
61
# File 'lib/alf/rack/config.rb', line 59

def reconnect(opts)
  connection.reconnect(opts)
end

#viewpointObject

Returns the default viewpoint to use



23
24
25
# File 'lib/alf/rack/config.rb', line 23

def viewpoint
  connection_options[:viewpoint]
end

#viewpoint=(vp) ⇒ Object

Sets the default viewpoint on connection options



28
29
30
# File 'lib/alf/rack/config.rb', line 28

def viewpoint=(vp)
  connection_options[:viewpoint] = vp
end