Sha256: e950177b04f2b33786344b239ff9f4dc3f84ad3e7c83dea46ac97e3d755fb7e2
Contents?: true
Size: 1004 Bytes
Versions: 1
Compression:
Stored size: 1004 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) if Slavery.disabled || target == :master :master elsif inside_transaction? raise Slavery::Error.new('on_slave cannot be used inside transaction block!') elsif target == :null_state :slave elsif target.present? "slave_#{target}".to_sym else raise Slavery::Error.new('on_slave cannot be used with a nil target!') end end def inside_transaction? open_transactions = run_on(:master) { ActiveRecord::Base.connection.open_transactions } open_transactions > Slavery::Transaction.base_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-3.0.0 | lib/slavery/base.rb |