Class: Perpetuity::Postgres::ConnectionPool
- Inherits:
-
Object
- Object
- Perpetuity::Postgres::ConnectionPool
- Defined in:
- lib/perpetuity/postgres/connection_pool.rb
Instance Attribute Summary collapse
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #execute(sql) ⇒ Object
-
#initialize(options = {}) ⇒ ConnectionPool
constructor
A new instance of ConnectionPool.
- #lend_connection ⇒ Object
- #tables ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ConnectionPool
Returns a new instance of ConnectionPool.
9 10 11 12 13 14 15 |
# File 'lib/perpetuity/postgres/connection_pool.rb', line 9 def initialize ={} @connections = Queue.new @size = .delete(:pool_size) { 5 } @size.times do connections << Connection.new() end end |
Instance Attribute Details
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
7 8 9 |
# File 'lib/perpetuity/postgres/connection_pool.rb', line 7 def connections @connections end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
7 8 9 |
# File 'lib/perpetuity/postgres/connection_pool.rb', line 7 def size @size end |
Instance Method Details
#execute(sql) ⇒ Object
26 27 28 29 30 |
# File 'lib/perpetuity/postgres/connection_pool.rb', line 26 def execute sql lend_connection do |connection| connection.execute sql end end |
#lend_connection ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/perpetuity/postgres/connection_pool.rb', line 17 def lend_connection if block_given? connection = connections.pop yield connection end ensure connections << connection end |
#tables ⇒ Object
32 33 34 35 36 |
# File 'lib/perpetuity/postgres/connection_pool.rb', line 32 def tables lend_connection do |connection| connection.tables end end |