module Temporality::Transaction

Constants

QUEUE

Public Instance Methods

active_transaction?() click to toggle source
# File lib/temporality/transaction.rb, line 24
def active_transaction?
  !!Thread.current[QUEUE]
end
cleanup_queue() click to toggle source
# File lib/temporality/transaction.rb, line 36
def cleanup_queue
  Thread.current[QUEUE] = nil
end
defer(key, &block) click to toggle source
# File lib/temporality/transaction.rb, line 20
def defer(key, &block)
  Thread.current[QUEUE][key] = block
end
init_queue() click to toggle source
# File lib/temporality/transaction.rb, line 32
def init_queue
  Thread.current[QUEUE] = Hash.new
end
process_queue() click to toggle source
# File lib/temporality/transaction.rb, line 28
def process_queue
  Thread.current[QUEUE].values.each(&:call)
end
transaction(*args, &block) click to toggle source
# File lib/temporality/transaction.rb, line 6
def transaction(*args, &block)
  raise RuntimeError.new("There already is a currently active temporality transaction") if active_transaction?

  ActiveRecord::Base.transaction(*args) do
    begin
      init_queue
      block.call
      process_queue
    ensure
      cleanup_queue
    end
  end
end