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

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

Overview

Memoizable class to store and execute item calls

Instance Attribute Summary collapse

Attributes inherited from Dry::Container::Item

#item, #options

Instance Method Summary collapse

Methods inherited from Dry::Container::Item

#callable?, #map, #value?

Constructor Details

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

Returns a new Memoizable instance

Parameters:

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

Raises:



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

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



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

def memoize_mutex
  @memoize_mutex
end

Instance Method Details

#callDry::Container::Item::Base

Returns the result of item call using a syncronized mutex

Returns:

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


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

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