Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_retry/core.rb

Overview

Extend the Numeric class with a #tries method

Instance Method Summary collapse

Instance Method Details

#tries(rescue_from: [StandardError], delay: EasyRetry.delay_algorithm) ⇒ Object Also known as: try

rubocop:disable Metrics/MethodLength

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/easy_retry/core.rb', line 6

def tries(rescue_from: [StandardError], delay: EasyRetry.delay_algorithm)
  raise ArgumentError, 'No block given' unless block_given?

  rescue_from = Array(rescue_from)
  max_retry = self
  current_try = 1
  result = nil

  loop do
    result = yield(current_try)

    break
  rescue *rescue_from => e
    log_failed_try(e, current_try, max_retry)

    call_delay(delay, current_try)

    current_try += 1
  end

  result
end