Module: ActiveRecord::SimpleSlave
- Defined in:
- lib/activerecord/simple_slave.rb,
lib/activerecord/simple_slave/version.rb
Constant Summary collapse
- VERSION =
'1.0.2'
Instance Method Summary collapse
- #connection ⇒ Object
- #simple_slave_configuration ⇒ Object
- #simple_slave_connection_pool ⇒ Object
- #simple_slave_connection_spec ⇒ Object
- #simple_slave_url ⇒ Object
- #with_slave(&_block) ⇒ Object
Instance Method Details
#connection ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/activerecord/simple_slave.rb', line 57 def connection if !Thread.current[:simple_slave_connection].nil? Thread.current[:simple_slave_connection] else super end end |
#simple_slave_configuration ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/activerecord/simple_slave.rb', line 9 def simple_slave_configuration @simple_slave_configuration ||= connection_config.dup.tap do |config| if simple_slave_url.nil? Rails.logger.warn 'simple slave disabled (no configuration provided)' next end uri = URI.parse(simple_slave_url) config[:host] = uri.host config[:port] = uri.port unless uri.port.nil? config[:username] = uri.user unless uri.user.nil? config[:password] = uri.password unless uri.password.nil? if !uri.path.nil? && uri.path.length > 1 config[:database] = uri.path[1..-1] end config.delete(:simple_slave_url) end end |
#simple_slave_connection_pool ⇒ Object
37 38 39 40 41 42 |
# File 'lib/activerecord/simple_slave.rb', line 37 def simple_slave_connection_pool @simple_slave_connection_pool ||= \ ActiveRecord::ConnectionAdapters::ConnectionPool.new( simple_slave_connection_spec ) end |
#simple_slave_connection_spec ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/activerecord/simple_slave.rb', line 28 def simple_slave_connection_spec return @simple_slave_spec unless @simple_slave_spec.nil? @simple_slave_spec = \ ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new( 'slave' => simple_slave_configuration ).spec(:slave) end |
#simple_slave_url ⇒ Object
3 4 5 6 7 |
# File 'lib/activerecord/simple_slave.rb', line 3 def simple_slave_url ENV.fetch( 'DATABASE_SIMPLE_SLAVE_URL', connection_config[:simple_slave_url] ) end |
#with_slave(&_block) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/activerecord/simple_slave.rb', line 44 def with_slave(&_block) retvalue = nil simple_slave_connection_pool.with_connection do |connection| begin Thread.current[:simple_slave_connection] = connection retvalue = yield(connection) ensure Thread.current[:simple_slave_connection] = nil end end retvalue end |