Class: Dry::Core::Container::Item::Memoizable

Inherits:
Dry::Core::Container::Item show all
Defined in:
lib/dry/core/container/item/memoizable.rb

Overview

Memoizable class to store and execute item calls

Instance Attribute Summary collapse

Attributes inherited from Dry::Core::Container::Item

#item, #options

Instance Method Summary collapse

Methods inherited from Dry::Core::Container::Item

#callable?, #map, #value?

Constructor Details

#initialize(item, options = {}) ⇒ Dry::Core::Container::Item::Base

Returns a new Memoizable instance

Parameters:

  • item (Mixed)
  • options (Hash) (defaults to: {})

Raises:



23
24
25
26
27
28
# File 'lib/dry/core/container/item/memoizable.rb', line 23

def initialize(item, options = {})
  super
  raise_not_supported_error unless callable?

  @memoize_mutex = ::Mutex.new
end

Instance Attribute Details

#memoize_mutexMutex (readonly)

Returns the stored mutex.

Returns:

  • (Mutex)

    the stored mutex



13
14
15
# File 'lib/dry/core/container/item/memoizable.rb', line 13

def memoize_mutex
  @memoize_mutex
end

Instance Method Details

#callDry::Core::Container::Item::Base

Returns the result of item call using a syncronized mutex

Returns:

  • (Dry::Core::Container::Item::Base)


33
34
35
36
37
# File 'lib/dry/core/container/item/memoizable.rb', line 33

def call
  memoize_mutex.synchronize do
    @memoized_item ||= item.call
  end
end