Class: Prato::Internal::LazyLoaderCache

Inherits:
Hash
  • Object
show all
Defined in:
lib/prato/internal/lazy_loader_cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(records) ⇒ LazyLoaderCache

Returns a new instance of LazyLoaderCache.



6
7
8
9
10
# File 'lib/prato/internal/lazy_loader_cache.rb', line 6

def initialize(records)
  super(records)

  @records = records
end

Instance Method Details

#[](key) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/prato/internal/lazy_loader_cache.rb', line 12

def [](key)
  value = super

  if value.is_a?(Proc)
    result = value.call(@records, self)
    self[key] = result # memoize the result
    result
  else
    value
  end
end