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
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() 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
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 |
#viewpoint ⇒ Object
Returns the default viewpoint to use
23 24 25 |
# File 'lib/alf/rack/config.rb', line 23 def viewpoint [: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) [:viewpoint] = vp end |