Module: Keynote::Cache

Defined in:
lib/keynote/cache.rb

Overview

Keynote::Cache memoizes presenter instances, reducing the overhead of calling Keynote.present repeatedly with the same parameters.

Class Method Summary collapse

Class Method Details

.fetch(name, view, *objects) ⇒ Keynote::Presenter

Return a cached presenter for the given parameters, or yield and cache the block's return value for next time.

The cached presenters are stored in an instance variable on the current view context, so they'll be garbage-collected when the view context goes out of scope.

Parameters:

  • name (Symbol)
  • view (ActionView::Base)
  • objects (Array)

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/keynote/cache.rb', line 21

def fetch(name, view, *objects)
  # If we don't have a view, just bail out and return a fresh presenter
  # every time.
  if view.nil?
    return yield
  end

  # Initialize our cache on the view context if it doesn't already exist.
  if view.instance_variable_defined?(:@_keynote_cache)
    cache = view.instance_variable_get(:@_keynote_cache)
  else
    cache = {}
    view.instance_variable_set(:@_keynote_cache, cache)
  end

  # Key each entry by the name of the presenter and the object_id of each
  # of the objects involved.
  key = [name, *objects.map(&:object_id)]

  # If we have a cached presenter, return it; if not, yield, store the
  # result, and return that.
  cache[key] or (cache[key] = yield)
end