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, options = {}) ⇒ Retry

Returns a new instance of Retry.



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

def initialize(max, options = {})
  @max = max
  @options = options
  @exceptions = options[:exceptions] || [StandardError]
end

Instance Method Details

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



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/method_decorators/retry.rb', line 11

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