class Roda::RodaPlugins::Render::TemplateMtimeWrapper
Wrapper object for the Tilt template, that checks the modified time of the template file, and rebuilds the template if the template file has been modified. This is an internal class and the API is subject to change at any time.
Public Class Methods
Source
# File lib/roda/plugins/render.rb, line 414 def initialize(roda_class, opts, template_opts) @roda_class = roda_class @opts = opts @template_opts = template_opts reset_template @path = opts[:path] deps = opts[:dependencies] @dependencies = ([@path] + Array(deps)) if deps @mtime = template_last_modified end
Public Instance Methods
Source
# File lib/roda/plugins/render.rb, line 501 def compiled_method(locals_keys=EMPTY_ARRAY, roda_class=nil) Render.tilt_template_compiled_method(@template, locals_keys, roda_class) end
Return the compiled method for the current template object.
Source
# File lib/roda/plugins/render.rb, line 508 def compiled_method_lambda(roda_class, method_name, locals_keys=EMPTY_ARRAY) mod = roda_class::RodaCompiledTemplates template = self lambda do |locals, &block| template.if_modified do mod.send(:define_method, method_name, Render.tilt_template_compiled_method(template, locals_keys, roda_class)) mod.send(:private, method_name) end _call_optimized_template_method([method_name, Render.tilt_template_fixed_locals?(template)], locals, &block) end end
Return the lambda used to define the compiled template method. This is separated into its own method so the lambda does not capture any unnecessary local variables
Source
# File lib/roda/plugins/render.rb, line 473 def define_compiled_method(roda_class, method_name, locals_keys=EMPTY_ARRAY) mod = roda_class::RodaCompiledTemplates internal_method_name = :"_#{method_name}" begin mod.send(:define_method, internal_method_name, compiled_method(locals_keys, roda_class)) rescue ::NotImplementedError return false end mod.send(:private, internal_method_name) mod.send(:define_method, method_name, &compiled_method_lambda(roda_class, internal_method_name, locals_keys)) mod.send(:private, method_name) method_name end
Compile a method in the given module with the given name that will call the compiled template method, updating the compiled template method
Source
# File lib/roda/plugins/render.rb, line 490 def define_compiled_method_cache_value(roda_class, method_name, locals_keys=EMPTY_ARRAY) if compiled_method = define_compiled_method(roda_class, method_name, locals_keys) [compiled_method, false].freeze else compiled_method end end
Returns an appropriate value for the template method cache.
Source
# File lib/roda/plugins/render.rb, line 467 def fixed_locals? Render.tilt_template_fixed_locals?(@template) end
Whether the underlying template uses fixed locals.
Source
# File lib/roda/plugins/render.rb, line 451 def if_modified begin mtime = template_last_modified rescue # ignore errors else if mtime != @mtime reset_template yield @mtime = mtime end end end
If the template file has been updated, return true and update the template object and the modification time. Other return false.
Source
# File lib/roda/plugins/render.rb, line 428 def render(*args, &block) res = nil modified = false if_modified do res = @template.render(*args, &block) modified = true end modified ? res : @template.render(*args, &block) end
If the template file exists and the modification time has changed, rebuild the template file, then call render on it.
Source
# File lib/roda/plugins/render.rb, line 441 def template_last_modified if deps = @dependencies deps.map{|f| File.mtime(f)}.max else File.mtime(@path) end end
Return when the template was last modified. If the template depends on any other files, check the modification times of all dependencies and return the maximum.
Private Instance Methods
Source
# File lib/roda/plugins/render.rb, line 526 def reset_template @template = @roda_class.create_template(@opts, @template_opts) end
Reset the template, done every time the template or one of its dependencies is modified.