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
336 def initialize(roda_class, opts, template_opts)
337   @roda_class = roda_class
338   @opts = opts
339   @template_opts = template_opts
340   reset_template
341 
342   @path = opts[:path]
343   deps = opts[:dependencies]
344   @dependencies = ([@path] + Array(deps)) if deps
345   @mtime = template_last_modified
346 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
409 def compiled_method(locals_keys=EMPTY_ARRAY, roda_class=nil)
410   Render.tilt_template_compiled_method(@template, locals_keys, roda_class)
411 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
416 def compiled_method_lambda(roda_class, method_name, locals_keys=EMPTY_ARRAY)
417   mod = roda_class::RodaCompiledTemplates
418   template = self
419   lambda do |locals, &block|
420     template.if_modified do
421       mod.send(:define_method, method_name, Render.tilt_template_compiled_method(template, locals_keys, roda_class))
422       mod.send(:private, method_name)
423     end
424 
425     send(method_name, locals, &block)
426   end
427 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
390 def define_compiled_method(roda_class, method_name, locals_keys=EMPTY_ARRAY)
391   mod = roda_class::RodaCompiledTemplates
392   internal_method_name = :"_#{method_name}"
393   begin
394     mod.send(:define_method, internal_method_name, compiled_method(locals_keys, roda_class))
395   rescue ::NotImplementedError
396     return false
397   end
398 
399   mod.send(:private, internal_method_name)
400   mod.send(:define_method, method_name, &compiled_method_lambda(roda_class, internal_method_name, locals_keys))
401   mod.send(:private, method_name)
402 
403   method_name
404 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
373 def if_modified
374   begin
375     mtime = template_last_modified
376   rescue
377     # ignore errors
378   else
379     if mtime != @mtime
380       reset_template
381       yield
382       @mtime = mtime
383     end
384   end
385 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
350 def render(*args, &block)
351   res = nil
352   modified = false
353   if_modified do
354     res = @template.render(*args, &block)
355     modified = true
356   end
357   modified ? res : @template.render(*args, &block)
358 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
363 def template_last_modified
364   if deps = @dependencies
365     deps.map{|f| File.mtime(f)}.max
366   else
367     File.mtime(@path)
368   end
369 end