Class: Erector::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/erector/caching.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache


3
4
5
# File 'lib/erector/caching.rb', line 3

def initialize
  @stores = {}
end

Instance Method Details

#[](klass, params = {}, content_method = nil) ⇒ Object


19
20
21
# File 'lib/erector/caching.rb', line 19

def [](klass, params = {}, content_method = nil)
  store_for(klass)[key(params)][content_method]
end

#[]=(*args) ⇒ Object


11
12
13
14
15
16
17
# File 'lib/erector/caching.rb', line 11

def []=(*args)
  value = args.pop
  klass = args.shift
  params = args.first.is_a?(Hash) ? args.first : {}
  content_method = args.last.is_a?(Symbol) ? args.last : nil
  store_for(klass)[key(params)][content_method] = value
end

#delete(klass, params = {}) ⇒ Object


23
24
25
# File 'lib/erector/caching.rb', line 23

def delete(klass, params = {})
  store_for(klass).delete(key(params))
end

#delete_all(klass) ⇒ Object


27
28
29
# File 'lib/erector/caching.rb', line 27

def delete_all(klass)
  @stores.delete(klass)
end

#key(params) ⇒ Object

convert hash-key to array-key for compatibility with 1.8.6


32
33
34
# File 'lib/erector/caching.rb', line 32

def key(params)
  params.to_a
end

#store_for(klass) ⇒ Object


7
8
9
# File 'lib/erector/caching.rb', line 7

def store_for(klass)
  @stores[klass] ||= Hash.new {|h,k| h[k] = {}}
end