Class: Alf::Rack::Config
- Inherits:
-
Support::Config
- Object
- Support::Config
- Alf::Rack::Config
- Defined in:
- lib/alf/rack/config.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
The current database connection.
Instance Method Summary collapse
-
#connect(&bl) ⇒ Object
Connects to the database, starts a transaction if required, then yields the block with the connection object.
-
#database=(db) ⇒ Object
Sets the database, coercing it if required.
-
#reconnect(opts) ⇒ Object
Reconnect with new options.
-
#viewpoint ⇒ Object
Returns the default viewpoint to use.
-
#viewpoint=(vp) ⇒ Object
Sets the default viewpoint on connection options.
Instance Attribute Details
#connection ⇒ Object (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() 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 |
#viewpoint ⇒ Object
Returns the default viewpoint to use
20 21 22 |
# File 'lib/alf/rack/config.rb', line 20 def viewpoint [: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) [:viewpoint] = vp end |