Module: Autobots::Helpers::Caching

Extended by:
ActiveSupport::Concern
Included in:
Assembler
Defined in:
lib/autobots/helpers/caching.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(klass) ⇒ Object



11
12
13
14
15
# File 'lib/autobots/helpers/caching.rb', line 11

def self.prepended(klass)
  klass.class_eval do
    class_attribute :cache
  end
end

Instance Method Details

#dataObject



17
18
19
20
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/autobots/helpers/caching.rb', line 17

def data
  return @data if defined?(@data)

  if cache
    key_proc = options.fetch(:cache_key) do
      method(:cache_key)
    end
    identifiers = objects.inject({}) do |acc, obj|
      acc[key_proc.call(obj, self)] = obj
      acc
    end

    if options[:force_reload]
      identifiers.each do |key, _|
        cache.delete key
      end
    end

    # misses: { key => obj }
    @data = BulkCacheFetcher.new(cache).fetch(identifiers) do |misses|
      roll_out(misses.values)
    end
  else
    @data = super
  end
  @data
end

#initialize(_, options = {}) ⇒ Object



6
7
8
9
# File 'lib/autobots/helpers/caching.rb', line 6

def initialize(_, options = {})
  super
  self.cache = options[:cache] if options.has_key?(:cache)
end