Module: FailRetry::ClassMethods

Defined in:
lib/fail_retry.rb

Instance Method Summary collapse

Instance Method Details

#fail_retry(method_name, exception: Fail, max_tries: 1, on_retry: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fail_retry.rb', line 11

def fail_retry(method_name, exception: Fail, max_tries: 1, on_retry: nil)
  define_method("#{method_name}_with_retry") do |*args, &block|
    trial = 0
    begin
      send("#{method_name}_without_retry", *args, &block)
    rescue => e
      raise if !e.kind_of?(exception) || trial >= max_tries
      on_retry.call(self) if on_retry
      trial += 1
      retry
    end
  end

  alias_method "#{method_name}_without_retry", method_name
  alias_method method_name, "#{method_name}_with_retry"
end