Sha256: 3ca89e5ab2d1268670f44792acc4bee75f2db4cc195e80aa227502982fcd39fb

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require 'dry/effects/provider'
require 'dry/effects/halt'

module Dry
  module Effects
    module Providers
      class Retry < Provider[:retry]
        include Dry::Equalizer(:scope, :limit, :attempts, inspect: false)

        param :scope

        attr_reader :attempts

        attr_reader :limit

        # Yield the block with the handler installed
        #
        # @api private
        def call(limit)
          @limit = limit
          @attempts = 0

          loop do
            begin
              return attempt { yield }
            rescue halt
            end
          end
        end

        def retry
          Instructions.Raise(halt.new)
        end

        def attempt
          if attempts_exhausted?
            nil
          else
            @attempts += 1
            yield
          end
        end

        def attempts_exhausted?
          attempts.equal?(limit)
        end

        def halt
          Halt[scope]
        end

        def provide?(effect)
          super && scope.equal?(effect.scope)
        end

        # @return [String]
        # @api public
        def represent
          "retry[#{scope} #{attempts}/#{limit}]"
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-effects-0.1.5 lib/dry/effects/providers/retry.rb
dry-effects-0.1.4 lib/dry/effects/providers/retry.rb
dry-effects-0.1.3 lib/dry/effects/providers/retry.rb
dry-effects-0.1.2 lib/dry/effects/providers/retry.rb