Sha256: 60e82374c446be67fe7c465962771fcde534a9740651729c265f67b873863c98

Contents?: true

Size: 780 Bytes

Versions: 1

Compression:

Stored size: 780 Bytes

Contents

require 'active_record'
require 'active_record/base'

# Retries an operation on an ActiveRecord until no StaleObjectError is being raised.
#
# == Example
#
#   class User < ActiveRecord::Base
#     include RetryableRecord
#   end
#
#   user = User.first
#
#   user.retryable do
#     user.username = "foo"
#     user.save!
#   end
#
module RetryableRecord
  # Retryable operations on an ActiveRecord +record+.
  #
  # == Example
  #
  #   RetryableRecord.retry(user) do
  #     user.username = "foo"
  #     user.save!
  #   end
  #
  def retry(record)
    yield
  rescue ActiveRecord::StaleObjectError
    record.reload
    retry
  end
  module_function :retry

  # Retries operations on an ActiveRecord.
  def retryable(&block)
    RetryableRecord.retry(self, &block)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
retryable_record-0.2.0 lib/retryable_record.rb