Class: Oprah::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/oprah/cache.rb

Overview

A cache store to keep Object-to-Presenter mappings. This class is thread-safe.

Since:

  • 0.0.1

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.

Since:

  • 0.0.1


5
6
7
8
# File 'lib/oprah/cache.rb', line 5

def initialize
  @mutex = Mutex.new
  @mapping = {}
end

Instance Method Details

#clear!Boolean

Clears the presenter cache.

Since:

  • 0.0.1


28
29
30
31
32
33
34
35
36
# File 'lib/oprah/cache.rb', line 28

def clear!
  @mutex.synchronize do
    @mapping = {}
  end

  Rails.logger.debug "Oprah cache cleared." if Oprah.debug?

  true
end

#lookup(object) ⇒ Array

Looks up presenters matching to `object` and stores them in the cache.

Since:

  • 0.0.1


14
15
16
17
18
19
20
21
22
23
# File 'lib/oprah/cache.rb', line 14

def lookup(object)
  @mutex.synchronize do
    key = class_name_for(object)

    cached = @mapping[key]
    return cached if cached

    @mapping[key] = presenter_classes_for(object)
  end
end