Module: Bonehead

Extended by:
Bonehead
Included in:
Bonehead
Defined in:
lib/bonehead.rb,
lib/bonehead/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#insist(tries = Float::Infinity, *exceptions, &block) ⇒ Object

Public: Executes a block of code and retries it up to ‘tries` times if an exception was raised.

tries - An Integer (default: Float::Infinity). exceptions - A list of Exceptions (default: StandardError).

Examples

class Wrapper
  include Bonehead

  def (username, password)
    insist(5, HTTPError) do
      HTTP.post "..."
    end
  end
end

Returns the return value of the block.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bonehead.rb', line 23

def insist(tries = Float::Infinity, *exceptions, &block)
  exceptions << StandardError if exceptions.empty?

  catch :__BONEHEAD__ do
    tries.times do |i|
      begin
        throw :__BONEHEAD__, yield(i.succ)
      rescue *exceptions
        tries.pred == i ? raise : next
      end
    end
  end
end