Created
November 9, 2009 12:45
-
-
Save etozzato/229914 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Emanuele Tozzato (mailto:etozzato@gmail.com) | |
# | |
# = Description | |
# Execute given block until true for given attempts. | |
# = Usage | |
# exec! { gateway.perform_handshake('2897-XSANI-19827') } | |
module Exec | |
CONFIG = { | |
:default_attempts => 3, | |
:max_attempts => 10, | |
:err => "attempts number is too high", | |
:sleep => 0.5 | |
} | |
def exec!(attempts=CONFIG[:default_attempts]) | |
attempts = attempts.to_i | |
raise StandardError.new(CONFIG[:err]) if attempts > CONFIG[:max_attempts] | |
begin | |
1.upto(attempts) do |i| | |
result = yield | |
return true if result | |
sleep(CONFIG[:sleep]) | |
end | |
false | |
rescue | |
false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment