Sha256: cff1c4c64e4005f630e81dab6a5502245079465c6e089ee9a69f6b131655b324

Contents?: true

Size: 881 Bytes

Versions: 2

Compression:

Stored size: 881 Bytes

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)

        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

        def call(stack, cache = EMPTY_HASH.dup)
          @cache = cache
          super(stack)
        end

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

        def represent
          if cache.empty?
            "cache[#{scope} empty]"
          else
            "cache[#{scope} size=#{cache.size}]"
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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