Sha256: 328ccb2af41ede0f3595736c95951db34e604ab1c7899512554e8271669453d0

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

module Erector
  module Caching
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def cacheable(value = true)
        @cachable = value

        if value && value != true
          @cache_version = value
        end
      end

      alias_method :cachable, :cacheable

      def cachable?
        if @cachable.nil?
          superclass.respond_to?(:cachable?) && superclass.cachable?
        else
          @cachable
        end
      end

      def cache_version
        @cache_version || nil
      end

      def cache
        Erector::Cache.instance
      end
    end

    def cache
      self.class.cache
    end

    def should_cache?
      if block.nil? && self.class.cachable?
        true
      else
        false
      end
    end

    protected
    def _emit(options = {})
      if should_cache?
        cache[self.class, self.class.cache_version, assigns, options[:content_method_name]] ||= super
      else
        super
      end
    end

    def _emit_via(parent, options = {})
      if should_cache?
        parent.output << cache[self.class, self.class.cache_version, assigns, options[:content_method_name]] ||= parent.capture_content { super }
        parent.output.widgets << self.class # todo: test!!!
      else
        super
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
erector-rails4-0.0.4 lib/erector/caching.rb
erector-rails4-0.0.3 lib/erector/caching.rb
erector-rails4-0.0.2 lib/erector/caching.rb
erector-rails4-0.0.1 lib/erector/caching.rb