Class: Garterbelt::Cache

Inherits:
Renderer show all
Includes:
ContentRendering
Defined in:
lib/renderers/cache.rb

Instance Attribute Summary collapse

Attributes inherited from Renderer

#escape, #style, #view

Instance Method Summary collapse

Methods included from ContentRendering

included

Methods inherited from Renderer

#indent, #level, #line_end, #output, #output=, #render

Constructor Details

#initialize(opts, &block) ⇒ Cache

Returns a new instance of Cache.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
# File 'lib/renderers/cache.rb', line 7

def initialize(opts, &block)
  super
  self.key = opts[:key]
  self.expires_in = opts[:expires_in]
  raise ArgumentError, ":key option required" unless key
  self.content = block if block_given?
  raise_unless_block_content
  self.cache_output = ""
end

Instance Attribute Details

#cache_outputObject

Returns the value of attribute cache_output.



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

def cache_output
  @cache_output
end

#expires_inObject

Returns the value of attribute expires_in.



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

def expires_in
  @expires_in
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#view_outputObject

Returns the value of attribute view_output.



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

def view_output
  @view_output
end

Instance Method Details

#cache_optionsObject



17
18
19
# File 'lib/renderers/cache.rb', line 17

def cache_options
  expires_in ? {:expires_in => expires_in} : {}
end

#footObject



26
27
28
29
# File 'lib/renderers/cache.rb', line 26

def foot
  view_output << cache_output
  self.output = view_output
end

#headObject



21
22
23
24
# File 'lib/renderers/cache.rb', line 21

def head
  self.view_output = output
  self.output = cache_output
end

#render_contentObject



31
32
33
34
35
36
37
38
# File 'lib/renderers/cache.rb', line 31

def render_content
  if cached = view.cache_store[key]
    self.output << cached
  else
    super # renders block to the diverted output
    view.cache_store.store(key, cache_output, cache_options)  # set the cache
  end
end