Class: Dry::Effects::Providers::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/effects/providers/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



11
12
13
# File 'lib/dry/effects/providers/cache.rb', line 11

def cache
  @cache
end

Instance Method Details

#call(cache = EMPTY_HASH.dup) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yield the block with the handler installed



24
25
26
27
# File 'lib/dry/effects/providers/cache.rb', line 24

def call(cache = EMPTY_HASH.dup)
  @cache = cache
  yield
end

#fetch_or_store(key, block) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/dry/effects/providers/cache.rb', line 13

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

#provide?(effect) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


32
33
34
# File 'lib/dry/effects/providers/cache.rb', line 32

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

#representString

Returns:

  • (String)


38
39
40
41
42
43
44
# File 'lib/dry/effects/providers/cache.rb', line 38

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