<commit_before>
require "run_after_commit/version"

module RunAfterCommit
  def self.included(base)
    base.after_commit :run_after_commit_queue
    base.after_rollback :clear_after_commit_queue
  end

  def run_after_commit(method = nil, &block)
    transaction do
      self.class.connection.add_transaction_record self

      after_commit_queue << Proc.new { self.send(method) } if method
      after_commit_queue << block if block
    end

    true
  end

  protected

  def run_after_commit_queue
    after_commit_queue.each do |action|
      self.instance_eval(&action)
    end
  ensure
    clear_after_commit_queue
  end

  def after_commit_queue
    self.class.connection.instance_variable_get("@run_after_commit_queue") || self.class.connection.instance_variable_set("@run_after_commit_queue", [])
  end

  def clear_after_commit_queue
    self.class.connection.instance_variable_set "@run_after_commit_queue", []

    true<commit_msg>Avoid side effects when outside of transactions<commit_after>
require "run_after_commit/version"

module RunAfterCommit
  def self.included(base)
    base.after_commit :run_after_commit_queue
    base.after_rollback :clear_after_commit_queue
  end

  def run_after_commit(method = nil, &block)
    after_commit_queue << Proc.new { send method } if method
    after_commit_queue << block if block

    run_after_commit_queue if self.class.connection.open_transactions.zero?

    true
  end

  protected

  def run_after_commit_queue
    after_commit_queue.each do |action|
      self.instance_eval(&action)
    end
  ensure
    clear_after_commit_queue
  end

  def after_commit_queue
    self.class.connection.instance_variable_get("@run_after_commit_queue") || self.class.connection.instance_variable_set("@run_after_commit_queue", [])
  end

  def clear_after_commit_queue
    self.class.connection.instance_variable_set "@run_after_commit_queue", []

    true
{'fraction_non_alphanumeric': 0.07079181961195595, 'fraction_numerical': 0.0, 'mean_word_length': 4.451428571428571, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7451395', 'n_tokens_mistral': 608, 'n_tokens_neox': 589, 'n_words': 110}