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



32
33
34
# File 'lib/alf/rack/config.rb', line 32

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.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/alf/rack/config.rb', line 41

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

#database=(db) ⇒ Object

Sets the database, coercing it if required



15
16
17
# File 'lib/alf/rack/config.rb', line 15

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.



56
57
58
# File 'lib/alf/rack/config.rb', line 56

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

#viewpointObject

Returns the default viewpoint to use



20
21
22
# File 'lib/alf/rack/config.rb', line 20

def viewpoint
  connection_options[:viewpoint]
end

#viewpoint=(vp) ⇒ Object

Sets the default viewpoint on connection options



25
26
27
# File 'lib/alf/rack/config.rb', line 25

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