Class: Utopia::TemplateCache

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/etanni.rb

Constant Summary collapse

CACHE_PREFIX =
".cache."
CACHE_ENABLED =
true

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, template_class = Etanni) ⇒ TemplateCache

Returns a new instance of TemplateCache.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/utopia/etanni.rb', line 69

def initialize(path, template_class = Etanni)
	@path = path
	@cache_path = TemplateCache.cache_path(@path)
	
	if !File.exist?(@cache_path) || (TemplateCache.mtime(@path) > TemplateCache.mtime(@cache_path))
		@template = template_class.new(File.read(@path))
		File.open(@cache_path, "w") { |f| f.write(@template.compiled) }
	else
		@template = template_class.new(File.read(@cache_path), true)
	end
end

Class Method Details

.cache_path(path) ⇒ Object



61
62
63
# File 'lib/utopia/etanni.rb', line 61

def self.cache_path(path)
	File.join(File.dirname(path), CACHE_PREFIX + File.basename(path))
end

.mtime(path) ⇒ Object



65
66
67
# File 'lib/utopia/etanni.rb', line 65

def self.mtime(path)
	File.symlink?(path) ? File.lstat(file_name).mtime : File.mtime(path)
end

Instance Method Details

#result(binding) ⇒ Object



81
82
83
# File 'lib/utopia/etanni.rb', line 81

def result(binding)
	@template.result(binding, @cache_path)
end