class Unparser::Adamantium::Memory

Storage for memoized methods

Public Class Methods

new(values) click to toggle source

Initialize the memory storage for memoized methods

@return [undefined]

@api private

# File lib/unparser/adamantium.rb, line 44
def initialize(values)
  @values  = values
  @monitor = Monitor.new
  freeze
end

Public Instance Methods

fetch(name) { || ... } click to toggle source

Fetch the value from memory, or evaluate if it does not exist

@param [Symbol] name

@yieldreturn [Object]

the value to memoize

@api public

# File lib/unparser/adamantium.rb, line 58
def fetch(name)
  @values.fetch(name) do      # check for the key
    @monitor.synchronize do   # acquire a lock if the key is not found
      @values.fetch(name) do  # recheck under lock
        @values[name] = yield # set the value
      end
    end
  end
end