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
393 def compiled_method(locals_keys=EMPTY_ARRAY, roda_class=nil)
394   Render.tilt_template_compiled_method(@template, locals_keys, roda_class)
395 end
compiled_method_lambda(template, 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
400 def compiled_method_lambda(template, roda_class, method_name, locals_keys=EMPTY_ARRAY)
401   mod = roda_class::RodaCompiledTemplates
402   lambda do |locals, &block|
403     if template.modified?
404       mod.send(:define_method, method_name, Render.tilt_template_compiled_method(template, locals_keys, roda_class))
405       mod.send(:private, method_name)
406     end
407 
408     send(method_name, locals, &block)
409   end
410 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
374 def define_compiled_method(roda_class, method_name, locals_keys=EMPTY_ARRAY)
375   mod = roda_class::RodaCompiledTemplates
376   internal_method_name = :"_#{method_name}"
377   begin
378     mod.send(:define_method, internal_method_name, send(:compiled_method, locals_keys, roda_class))
379   rescue ::NotImplementedError
380     return false
381   end
382 
383   mod.send(:private, internal_method_name)
384   mod.send(:define_method, method_name, &compiled_method_lambda(self, roda_class, internal_method_name, locals_keys))
385   mod.send(:private, method_name)
386 
387   method_name
388 end
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
355 def modified?
356   begin
357     mtime = template_last_modified
358   rescue
359     # ignore errors
360   else
361     if mtime != @mtime
362       @mtime = mtime
363       reset_template
364       return true
365     end
366   end
367 
368   false
369 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   modified?
339   @template.render(*args, &block)
340 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
345 def template_last_modified
346   if deps = @dependencies
347     deps.map{|f| File.mtime(f)}.max
348   else
349     File.mtime(@path)
350   end
351 end