Sha256: 59382dda9368dbbee3022469c0ed52760c31328d2629ff34c0c24948b3d5115a

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require 'dry/effects/provider'
require 'dry/effects/instructions/execute'

module Dry
  module Effects
    module Providers
      class Cache < Provider[:cache]
        include ::Dry::Equalizer(:scope, :cache, inspect: false)

        param :scope

        attr_reader :cache

        def fetch_or_store(key, block)
          if cache.key?(key)
            cache[key]
          else
            Instructions.Execute { cache[key] = block.call }
          end
        end

        # Yield the block with the handler installed
        #
        # @api private
        def call(cache = EMPTY_HASH.dup)
          @cache = cache
          yield
        end

        # @param [Effect] effect
        # @return [Boolean]
        # @api public
        def provide?(effect)
          super && scope.eql?(effect.scope)
        end

        # @return [String]
        # @api public
        def represent
          if cache.empty?
            "cache[#{scope} empty]"
          else
            "cache[#{scope} size=#{cache.size}]"
          end
        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/cache.rb
dry-effects-0.1.4 lib/dry/effects/providers/cache.rb
dry-effects-0.1.3 lib/dry/effects/providers/cache.rb
dry-effects-0.1.2 lib/dry/effects/providers/cache.rb