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
403 def initialize(roda_class, opts, template_opts)
404   @roda_class = roda_class
405   @opts = opts
406   @template_opts = template_opts
407   reset_template
408 
409   @path = opts[:path]
410   deps = opts[:dependencies]
411   @dependencies = ([@path] + Array(deps)) if deps
412   @mtime = template_last_modified
413 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
490 def compiled_method(locals_keys=EMPTY_ARRAY, roda_class=nil)
491   Render.tilt_template_compiled_method(@template, locals_keys, roda_class)
492 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
497 def compiled_method_lambda(roda_class, method_name, locals_keys=EMPTY_ARRAY)
498   mod = roda_class::RodaCompiledTemplates
499   template = self
500   lambda do |locals, &block|
501     template.if_modified do
502       mod.send(:define_method, method_name, Render.tilt_template_compiled_method(template, locals_keys, roda_class))
503       mod.send(:private, method_name)
504     end
505 
506     _call_optimized_template_method([method_name, Render.tilt_template_fixed_locals?(template)], locals, &block)
507   end
508 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
462 def define_compiled_method(roda_class, method_name, locals_keys=EMPTY_ARRAY)
463   mod = roda_class::RodaCompiledTemplates
464   internal_method_name = :"_#{method_name}"
465   begin
466     mod.send(:define_method, internal_method_name, compiled_method(locals_keys, roda_class))
467   rescue ::NotImplementedError
468     return false
469   end
470 
471   mod.send(:private, internal_method_name)
472   mod.send(:define_method, method_name, &compiled_method_lambda(roda_class, internal_method_name, locals_keys))
473   mod.send(:private, method_name)
474 
475   method_name
476 end
define_compiled_method_cache_value(roda_class, method_name, locals_keys=EMPTY_ARRAY)

Returns an appropriate value for the template method cache.

[show source]
    # File lib/roda/plugins/render.rb
479 def define_compiled_method_cache_value(roda_class, method_name, locals_keys=EMPTY_ARRAY)
480   if compiled_method = define_compiled_method(roda_class, method_name, locals_keys)
481     [compiled_method, false].freeze
482   else
483     compiled_method
484   end
485 end
fixed_locals?()

Whether the underlying template uses fixed locals.

[show source]
    # File lib/roda/plugins/render.rb
456 def fixed_locals?
457   Render.tilt_template_fixed_locals?(@template)
458 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
440 def if_modified
441   begin
442     mtime = template_last_modified
443   rescue
444     # ignore errors
445   else
446     if mtime != @mtime
447       reset_template
448       yield
449       @mtime = mtime
450     end
451   end
452 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
417 def render(*args, &block)
418   res = nil
419   modified = false
420   if_modified do
421     res = @template.render(*args, &block)
422     modified = true
423   end
424   modified ? res : @template.render(*args, &block)
425 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
430 def template_last_modified
431   if deps = @dependencies
432     deps.map{|f| File.mtime(f)}.max
433   else
434     File.mtime(@path)
435   end
436 end