Class: MethodDecorators::Retry

Inherits:
Decorator show all
Defined in:
lib/method_decorators/retry.rb

Instance Method Summary collapse

Methods inherited from Decorator

#+@, +@, current_decorators

Constructor Details

#initialize(max) ⇒ Retry

Returns a new instance of Retry.



5
6
7
# File 'lib/method_decorators/retry.rb', line 5

def initialize(max)
  @max = max
end

Instance Method Details

#call(orig, this, *args, &blk) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/method_decorators/retry.rb', line 9

def call(orig, this, *args, &blk)
  attempts = 0
  begin
    attempts += 1
    orig.call(*args, &blk)
  rescue
    retry  if attempts < @max
    raise
  end
end