Sha256: fba42ef25c994e51396c51e1445f9a378a5e0e1a216b3d62fb98e8593c9b2b72

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

# * George Moschovitis  <gm@navel.gr>
# (c) 2004-2005 Navel, all rights reserved.
# $Id: output.rb 9 2005-04-13 00:08:20Z nasis $

require 'fileutils'

module Nitro

# Adds support for caching.

module Caching

	# Output caching.

	module Output

		def self.append_features(base) # :nodoc: 
			super
			base.extend(ClassMethods)
			base.module_eval do
				cattr_accessor :output_cache_root, 'public'
			end
		end

		module ClassMethods

			def do_cache_output(path, content)
				filepath = output_cache_path(path)
				FileUtils.makedirs(File.dirname(filepath))
				File.open(filepath, 'w+') { |f| f.write(content) }
				Logger.debug "Cached page: #{filepath}" if $DBG
			end

			# Enable output caching for the given actions.

			def cache_output(*actions)
				return unless caching_enabled

				str = actions.collect { |a| ":#{a}" }.join(', ')

				module_eval %{
					after_filter "do_cache_output", :only => [ #{str} ]
				}
			end

			private

			def output_cache_path(path)
				filename = ((path.empty? || path == '/') ? '/index' : path)
				filename << '.html' unless (name.split('/').last || name).include? '.'
				return output_cache_root + filename 										
			end
			
		end

		private

		def do_cache_output
			if caching_enabled and caching_allowed?
				self.class.do_cache_output(@request.path, @out)
			end
		end

		def caching_allowed?
			!@request.post?
		end

	end

end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nitro-0.16.0 lib/nitro/caching/output.rb
nitro-0.17.0 lib/nitro/caching/output.rb