Sha256: a064d27f3b2aec8dabe749e879d0afe859a42632e8996d49f4da4125eab68ee4

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 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)

        param :scope

        attr_reader :attempts

        attr_reader :limit

        def call(_, limit)
          @limit = limit
          @attempts = 0

          loop do
            return attempt { yield }
          rescue halt
          end
        end

        def repeat
          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

        def represent
          "retry[#{scope} #{attempts}/#{limit}]"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-effects-0.1.0.alpha lib/dry/effects/providers/retry.rb