Class: CacheKeeper::CachedMethod

Inherits:
Object
  • Object
show all
Includes:
Refreshable
Defined in:
app/models/cache_keeper/cached_method.rb

Defined Under Namespace

Modules: Refreshable

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Refreshable

#refresh, #refresh_later

Constructor Details

#initialize(klass, method_name, options = {}) ⇒ CachedMethod

Returns a new instance of CachedMethod.



6
7
8
9
10
# File 'app/models/cache_keeper/cached_method.rb', line 6

def initialize(klass, method_name, options = {})
  self.klass = klass
  self.method_name = method_name
  self.options = options.with_indifferent_access
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



4
5
6
# File 'app/models/cache_keeper/cached_method.rb', line 4

def klass
  @klass
end

#method_nameObject

Returns the value of attribute method_name.



4
5
6
# File 'app/models/cache_keeper/cached_method.rb', line 4

def method_name
  @method_name
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'app/models/cache_keeper/cached_method.rb', line 4

def options
  @options
end

Instance Method Details

#alias_for_original_methodObject



12
13
14
# File 'app/models/cache_keeper/cached_method.rb', line 12

def alias_for_original_method
  :"__#{method_name}__hooked__"
end

#call(instance) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/cache_keeper/cached_method.rb', line 16

def call(instance)
  if cache_entry.blank?
    refresh instance
  elsif cache_entry.expired?
    if must_revalidate?
      refresh instance
    else
      refresh_later instance

      cache_entry.value
    end
  else
    cache_entry.value
  end
end