Module: Trusty::Errors::Retry

Includes:
ExceptionHandlers
Defined in:
lib/trusty/errors/retry.rb

Instance Method Summary collapse

Methods included from ExceptionHandlers

#notify_exception, #try_with_data

Instance Method Details

#retry_block(options = {}, &block) ⇒ Object

retry a block of code



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/trusty/errors/retry.rb', line 9

def retry_block(options = {}, &block)
  options = {
    :retry => 1,
    :data => {},
    :type => StandardError
  }.merge(options)
  
  retries = case options[:retry]
    when true
      1
    when Integer
      options[:retry]
    else
      0
    end
  
  types = [ options[:type] ].flatten.compact
  
  begin
    yield
  rescue *types => ex
    if retries > 0
      return self.send(__method__, options.merge(:retry => retries - 1), &block)
    else
      notify_exception(ex, :data => options[:data], :raise => true)
    end
  end
end

#retry_method(method, options = {}) ⇒ Object

helper method to redefine method (a la alias_method_chain, etc)



39
40
41
42
43
44
45
46
# File 'lib/trusty/errors/retry.rb', line 39

def retry_method(method, options = {})
  define_method method do |*args, &block|
    super_method = method(:super)
    retry_block options do
      super_method(*args, &block)
    end
  end
end