Class: NeverBlock::DB::Connection
- Inherits:
-
Object
- Object
- NeverBlock::DB::Connection
- Defined in:
- lib/neverblock/io/db/connection.rb
Overview
a proxy for pooled fibered connections
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(size = 4, &block) ⇒ Connection
constructor
Requires a block with connection parameters and a pool size (defaults to 4).
-
#method_missing(method, *args) ⇒ Object
Pass unknown methods to the connection.
-
#query(query) ⇒ Object
(also: #exec)
A proxy for the connection’s query method quries the pool to get a connection first.
-
#replace_acquired_connection ⇒ Object
Replaces the current connection with a brand new one.
-
#respond_to?(method) ⇒ Boolean
Pass method queries to the connection.
Constructor Details
#initialize(size = 4, &block) ⇒ Connection
Requires a block with connection parameters and a pool size (defaults to 4)
10 11 12 13 14 |
# File 'lib/neverblock/io/db/connection.rb', line 10 def initialize(size=4, &block) @pool = NB::DB::Pool.new(:size=>size, :eager=>true) do yield end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Pass unknown methods to the connection
32 33 34 35 36 |
# File 'lib/neverblock/io/db/connection.rb', line 32 def method_missing(method, *args) @pool.hold do |conn| conn.send(method, *args) end end |
Instance Method Details
#query(query) ⇒ Object Also known as: exec
A proxy for the connection’s query method quries the pool to get a connection first
18 19 20 21 22 |
# File 'lib/neverblock/io/db/connection.rb', line 18 def query(query) @pool.hold do |conn| conn.query(query) end end |
#replace_acquired_connection ⇒ Object
Replaces the current connection with a brand new one
27 28 29 |
# File 'lib/neverblock/io/db/connection.rb', line 27 def replace_acquired_connection @pool.replace_acquired_connection end |
#respond_to?(method) ⇒ Boolean
Pass method queries to the connection
39 40 41 42 43 |
# File 'lib/neverblock/io/db/connection.rb', line 39 def respond_to?(method) @pool.hold do |conn| conn.respond_to?(method) end end |