Sha256: 77db3a482804623954a7f0168fc76e3f7c5ff7207c19a5b8480a59cb80e7ba76
Contents?: true
Size: 798 Bytes
Versions: 1
Compression:
Stored size: 798 Bytes
Contents
module Slavery class Base def initialize(target) @target = decide_with(target) end def run(&block) run_on @target, &block end private def decide_with(target) raise Slavery::Error.new('on_slave cannot be used inside transaction block!') if inside_transaction? if Slavery.disabled :master else target end end def inside_transaction? open_transactions = run_on(:master) { ActiveRecord::Base.connection.open_transactions } open_transactions > Slavery.base_transaction_depth end def run_on(target) backup = Thread.current[:slavery] # Save for recursive nested calls Thread.current[:slavery] = target yield ensure Thread.current[:slavery] = backup end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
slavery-2.0.0 | lib/slavery/base.rb |