Class: Cutaneous::CachedTemplate

Inherits:
Template
  • Object
show all
Defined in:
lib/cutaneous/loader.rb

Overview

Provides an additional caching mechanism by writing generated template scripts to a .rb file.

Instance Attribute Summary

Attributes inherited from Template

#lexer, #loader, #path

Instance Method Summary collapse

Methods inherited from Template

#block, #block_order, #compiler, #convert, #initialize, #render, #template_proc, #template_proc_src

Constructor Details

This class inherits a constructor from Cutaneous::Template

Instance Method Details

#cached?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/cutaneous/loader.rb', line 118

def cached?
  File.exist?(script_path) && (File.mtime(script_path) >= File.mtime(template_path))
end

#generate_script_pathObject



130
131
132
133
134
135
# File 'lib/cutaneous/loader.rb', line 130

def generate_script_path
  path = template_path
  return nil if path.nil?
  ext  = File.extname path
  path.gsub(/#{ext}$/, ".rb")
end

#scriptObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cutaneous/loader.rb', line 106

def script
  script = nil
  path = script_path
  if path && cached?
    script = File.read(path)
  else
    script = super
    write_cached_script(script, path) unless path.nil?
  end
  script
end

#script_pathObject



126
127
128
# File 'lib/cutaneous/loader.rb', line 126

def script_path
  @source_path ||= generate_script_path
end

#template_pathObject



122
123
124
# File 'lib/cutaneous/loader.rb', line 122

def template_path
  path
end

#write_cached_script(script, path) ⇒ Object



137
138
139
140
141
# File 'lib/cutaneous/loader.rb', line 137

def write_cached_script(script, path)
  File.open(script_path, "w") do |f|
    f.write(script)
  end
end