Class: Praxis::Mapper::ConnectionManager
- Inherits:
-
Object
- Object
- Praxis::Mapper::ConnectionManager
- Defined in:
- lib/praxis-mapper/connection_manager.rb
Class Attribute Summary collapse
-
.repositories ⇒ Object
Returns the value of attribute repositories.
Class Method Summary collapse
Instance Method Summary collapse
- #checkout(name) ⇒ Object
-
#initialize ⇒ ConnectionManager
constructor
A new instance of ConnectionManager.
- #release(name = nil) ⇒ Object
- #release_one(name) ⇒ Object
- #repositories ⇒ Object
- #repository(repository_name) ⇒ Object
- #thread ⇒ Object
Constructor Details
#initialize ⇒ ConnectionManager
Returns a new instance of ConnectionManager.
45 46 47 48 |
# File 'lib/praxis-mapper/connection_manager.rb', line 45 def initialize @connections = {} @thread = Thread.current end |
Class Attribute Details
.repositories ⇒ Object
Returns the value of attribute repositories.
6 7 8 |
# File 'lib/praxis-mapper/connection_manager.rb', line 6 def repositories @repositories end |
Class Method Details
.repository(repository_name, **data, &block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/praxis-mapper/connection_manager.rb', line 15 def self.repository(repository_name, **data, &block) return repositories[repository_name] if data.empty? && !block_given? query = data[:query] || Praxis::Mapper::Query::Sql factory_class = data[:factory] || ConnectionFactories::Simple opts = data[:opts] || {} if query.kind_of? String query = query.constantize end if factory_class.kind_of? String factory_class = factory_class.constantize end repositories[repository_name] = { query: query, factory: factory_class.new(**opts, &block) } end |
.setup(&block) ⇒ Object
9 10 11 12 13 |
# File 'lib/praxis-mapper/connection_manager.rb', line 9 def self.setup(&block) if block_given? self.instance_eval(&block) end end |
Instance Method Details
#checkout(name) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/praxis-mapper/connection_manager.rb', line 55 def checkout(name) connection = @connections[name] return connection if connection factory = repositories[name][:factory] connection = factory.checkout(self) @connections[name] = connection end |
#release(name = nil) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/praxis-mapper/connection_manager.rb', line 71 def release(name=nil) if name release_one(name) else names = @connections.keys names.each { |n| release_one(n) } end end |
#release_one(name) ⇒ Object
65 66 67 68 69 |
# File 'lib/praxis-mapper/connection_manager.rb', line 65 def release_one(name) if (connection = @connections.delete(name)) repositories[name][:factory].release(self, connection) end end |
#repositories ⇒ Object
37 38 39 |
# File 'lib/praxis-mapper/connection_manager.rb', line 37 def repositories self.class.repositories end |
#repository(repository_name) ⇒ Object
41 42 43 |
# File 'lib/praxis-mapper/connection_manager.rb', line 41 def repository(repository_name) self.repositories[repository_name] end |
#thread ⇒ Object
50 51 52 53 |
# File 'lib/praxis-mapper/connection_manager.rb', line 50 def thread return @thread if @thread == Thread.current raise 'threading violation in ConnectionManager. Calling Thread is different from Thread that owns this instance.' end |