class Perforated::Cache

Attributes

enumerable[RW]
strategy[RW]

Public Class Methods

new(enumerable, strategy = Perforated::Strategy) click to toggle source
# File lib/perforated/cache.rb, line 9
def initialize(enumerable, strategy = Perforated::Strategy)
  @enumerable = enumerable
  @strategy   = strategy
end

Public Instance Methods

to_json(rooted: false, &block) click to toggle source
# File lib/perforated/cache.rb, line 14
def to_json(rooted: false, &block)
  keyed   = key_mapped(enumerable, &block)
  results = fetch_multi(keyed) { |key| keyed[key].to_json }.values

  rebuild(results, rooted)
end

Private Instance Methods

fetch_multi(keyed, &block) click to toggle source
# File lib/perforated/cache.rb, line 30
def fetch_multi(keyed, &block)
  keys = keyed.keys.map(&:dup)

  return {} unless keys.any?

  Perforated::Compatibility.fetch_multi(*keys, &block)
end
key_mapped(subset) { |object| ... } click to toggle source
# File lib/perforated/cache.rb, line 23
def key_mapped(subset)
  subset.each_with_object({}) do |object, memo|
    object = yield(object) if block_given?
    memo[strategy.expand_cache_key(object)] = object
  end
end
rebuild(results, rooted) click to toggle source
# File lib/perforated/cache.rb, line 38
def rebuild(results, rooted)
  Perforated::Rebuilder.new(results).rebuild(rooted: rooted)
end