Skip to content

Commit

Permalink
added retry_with: parameter to Integer#times_try:retry_with:
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkdoor committed Apr 9, 2012
1 parent 6e06162 commit 3f4248c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/integer.fy
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ class Integer {
}
}

def times_try: block {
def times_try: block retry_with: retry_block ({}) {
"""
@block @Block@ to be called and retries @self amount of times if exceptions get thrown.
@retry_block @Block@ to be called before retrying execution of @block. Defaults to an empty @Block@.
@return Return value of calling @block or raises an exception after @self tries.
Returns @nil if @self <= 0.
Expand All @@ -32,7 +33,7 @@ class Integer {
# if it succeeds the first time, simply return its value, otherwise try once more.
# if it still fails after 2 attempts, raises the exception thrown (in this case probably an IOError).
@connection readln
}
} retry_with: { @connection reconnect }
"""

max_retries = self
Expand All @@ -43,6 +44,7 @@ class Integer {
} catch Exception => e {
max_retries = max_retries - 1
{ e raise! } unless: $ max_retries > 0
retry_block call
retry
} finally {
return value
Expand Down
3 changes: 3 additions & 0 deletions tests/fixnum.fy
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,8 @@ FancySpec describe: Fixnum with: {
tries is: 2

2 times_try: { 2 } . is: 2

i = 0
2 times_try: { 2 / i } retry_with: { i = 1 } . is: 2
}
}

0 comments on commit 3f4248c

Please sign in to comment.