Sha256: d4f3fb7b5725d30ac2f8f3bdfe323d227ca47a921ad9f33bde0ea30e9b2b4fed

Contents?: true

Size: 575 Bytes

Versions: 2

Compression:

Stored size: 575 Bytes

Contents

require 'tries/version'

class Integer
  def tries(options = {}, &block)
    attempts          = 1
    exception_classes = Array(options[:on] || StandardError)
    delay             = options[:delay]
    incremental       = options[:incremental]

    begin
      return yield
    rescue *exception_classes
      Kernel.sleep calculate_delay(delay, attempts, incremental) if delay
      retry if (attempts += 1) <= self
    end

    yield
  end

  private
  def calculate_delay(delay, attempts, incremental)
    return delay unless incremental

    delay * attempts
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tries-0.2.1 lib/tries.rb
tries-0.2.0 lib/tries.rb