class Knockoff::Base

Attributes

target[R]

Public Class Methods

new(target, check_transaction: true) click to toggle source
# File lib/knockoff/base.rb, line 5
def initialize(target, check_transaction: true)
  @target = decide_with(target, check_transaction)
end

Public Instance Methods

run(&block) click to toggle source
# File lib/knockoff/base.rb, line 9
def run(&block)
  run_on @target, &block
end

Private Instance Methods

decide_with(target, check_transaction) click to toggle source
# File lib/knockoff/base.rb, line 15
def decide_with(target, check_transaction)
  calculated_target =
    if Knockoff.enabled
      target
    else
      :primary
    end

  # Don't allow setting the target to anything other than primary if we are already in a transaction
  if calculated_target != :primary && check_transaction && inside_transaction?
    raise Knockoff::Error.new('on_replica cannot be used inside transaction block!')
  end
  calculated_target
end
inside_transaction?() click to toggle source
# File lib/knockoff/base.rb, line 30
def inside_transaction?
  open_transactions = run_on(:primary) { ActiveRecord::Base.connection.open_transactions }
  open_transactions > Knockoff.base_transaction_depth
end
run_on(target) { || ... } click to toggle source
# File lib/knockoff/base.rb, line 35
def run_on(target)
  backup = Thread.current[:knockoff] # Save for recursive nested calls
  Thread.current[:knockoff] = target
  yield
ensure
  Thread.current[:knockoff] = backup
end