class Roda::RodaPlugins::Render::TemplateMtimeWrapper

  1. lib/roda/plugins/render.rb
Superclass: Object

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

new(roda_class, opts, template_opts)
[show source]
    # File lib/roda/plugins/render.rb
323 def initialize(roda_class, opts, template_opts)
324   @roda_class = roda_class
325   @opts = opts
326   @template_opts = template_opts
327   reset_template
328 
329   @path = opts[:path]
330   deps = opts[:dependencies]
331   @dependencies = ([@path] + Array(deps)) if deps
332   @mtime = template_last_modified
333 end

Public Instance methods

compiled_method(locals_keys=EMPTY_ARRAY, roda_class=nil)

Return the compiled method for the current template object.

[show source]
    # File lib/roda/plugins/render.rb
396 def compiled_method(locals_keys=EMPTY_ARRAY, roda_class=nil)
397   Render.tilt_template_compiled_method(@template, locals_keys, roda_class)
398 end
compiled_method_lambda(roda_class, method_name, locals_keys=EMPTY_ARRAY)

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

[show source]
    # File lib/roda/plugins/render.rb
403 def compiled_method_lambda(roda_class, method_name, locals_keys=EMPTY_ARRAY)
404   mod = roda_class::RodaCompiledTemplates
405   template = self
406   lambda do |locals, &block|
407     template.if_modified do
408       mod.send(:define_method, method_name, Render.tilt_template_compiled_method(template, locals_keys, roda_class))
409       mod.send(:private, method_name)
410     end
411 
412     send(method_name, locals, &block)
413   end
414 end
define_compiled_method(roda_class, method_name, locals_keys=EMPTY_ARRAY)

Compile a method in the given module with the given name that will call the compiled template method, updating the compiled template method

[show source]
    # File lib/roda/plugins/render.rb
377 def define_compiled_method(roda_class, method_name, locals_keys=EMPTY_ARRAY)
378   mod = roda_class::RodaCompiledTemplates
379   internal_method_name = :"_#{method_name}"
380   begin
381     mod.send(:define_method, internal_method_name, compiled_method(locals_keys, roda_class))
382   rescue ::NotImplementedError
383     return false
384   end
385 
386   mod.send(:private, internal_method_name)
387   mod.send(:define_method, method_name, &compiled_method_lambda(roda_class, internal_method_name, locals_keys))
388   mod.send(:private, method_name)
389 
390   method_name
391 end
if_modified()

If the template file has been updated, return true and update the template object and the modification time. Other return false.

[show source]
    # File lib/roda/plugins/render.rb
360 def if_modified
361   begin
362     mtime = template_last_modified
363   rescue
364     # ignore errors
365   else
366     if mtime != @mtime
367       reset_template
368       yield
369       @mtime = mtime
370     end
371   end
372 end
render(*args, &block)

If the template file exists and the modification time has changed, rebuild the template file, then call render on it.

[show source]
    # File lib/roda/plugins/render.rb
337 def render(*args, &block)
338   res = nil
339   modified = false
340   if_modified do
341     res = @template.render(*args, &block)
342     modified = true
343   end
344   modified ? res : @template.render(*args, &block)
345 end
template_last_modified()

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.

[show source]
    # File lib/roda/plugins/render.rb
350 def template_last_modified
351   if deps = @dependencies
352     deps.map{|f| File.mtime(f)}.max
353   else
354     File.mtime(@path)
355   end
356 end