Sha256: 9c4bb84f0f864eb30d13bb5feb8095a8aa254b1fd46c6f404ca642a7ae928fef

Contents?: true

Size: 1008 Bytes

Versions: 1

Compression:

Stored size: 1008 Bytes

Contents

# frozen_string_literal: true

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)

        # @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

1 entries across 1 versions & 1 rubygems

Version Path
dry-effects-0.5.0 lib/dry/effects/providers/cache.rb