module Trusty::Errors::Retry

Public Instance Methods

retry_block(options = {}) { || ... } click to toggle source

retry a block of code

# File lib/trusty/errors/retry.rb, line 9
def retry_block(options = {}, &block)
  options = {
    :retry => 1,
    :data => {},
    :type => StandardError
  }.merge(options)
  
  retries = case options[:retry]
    when true
      1
    when Integer
      options[:retry]
    else
      0
    end
  
  types = [ options[:type] ].flatten.compact
  
  begin
    yield
  rescue *types => ex
    if retries > 0
      return self.send(__method__, options.merge(:retry => retries - 1), &block)
    else
      notify_exception(ex, :data => options[:data], :raise => true)
    end
  end
end
retry_method(method, options = {}) click to toggle source

helper method to redefine method (a la alias_method_chain, etc)

# File lib/trusty/errors/retry.rb, line 39
def retry_method(method, options = {})
  define_method method do |*args, &block|
    super_method = method(:super)
    retry_block options do
      super_method(*args, &block)
    end
  end
end