Sha256: e2d3c037ef21c31c284a6a3f3b9ba14949f86c5030fb92652a247b239d2ee5e4

Contents?: true

Size: 978 Bytes

Versions: 9

Compression:

Stored size: 978 Bytes

Contents

module Garterbelt
  class Cache < Renderer
    include ContentRendering
    
    attr_accessor :key, :cache_output, :view_output, :expires_in
    
    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
    
    def cache_options
      expires_in ? {:expires_in => expires_in} : {}
    end
    
    def head
      self.view_output = output
      self.output = cache_output
    end
    
    def foot
      view_output << cache_output
      self.output = view_output
    end
    
    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
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
garterbelt-0.1.1 lib/renderers/cache.rb
garterbelt-0.1.0 lib/renderers/cache.rb
garterbelt-0.0.9 lib/renderers/cache.rb
garterbelt-0.0.8 lib/renderers/cache.rb
garterbelt-0.0.7 lib/renderers/cache.rb
garterbelt-0.0.6 lib/renderers/cache.rb
garterbelt-0.0.5 lib/renderers/cache.rb
garterbelt-0.0.4 lib/renderers/cache.rb
garterbelt-0.0.3 lib/renderers/cache.rb