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
414 def initialize(roda_class, opts, template_opts)
415   @roda_class = roda_class
416   @opts = opts
417   @template_opts = template_opts
418   reset_template
419 
420   @path = opts[:path]
421   deps = opts[:dependencies]
422   @dependencies = ([@path] + Array(deps)) if deps
423   @mtime = template_last_modified
424 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
501 def compiled_method(locals_keys=EMPTY_ARRAY, roda_class=nil)
502   Render.tilt_template_compiled_method(@template, locals_keys, roda_class)
503 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
508 def compiled_method_lambda(roda_class, method_name, locals_keys=EMPTY_ARRAY)
509   mod = roda_class::RodaCompiledTemplates
510   template = self
511   lambda do |locals, &block|
512     template.if_modified do
513       mod.send(:define_method, method_name, Render.tilt_template_compiled_method(template, locals_keys, roda_class))
514       mod.send(:private, method_name)
515     end
516 
517     _call_optimized_template_method([method_name, Render.tilt_template_fixed_locals?(template)], locals, &block)
518   end
519 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
473 def define_compiled_method(roda_class, method_name, locals_keys=EMPTY_ARRAY)
474   mod = roda_class::RodaCompiledTemplates
475   internal_method_name = :"_#{method_name}"
476   begin
477     mod.send(:define_method, internal_method_name, compiled_method(locals_keys, roda_class))
478   rescue ::NotImplementedError
479     return false
480   end
481 
482   mod.send(:private, internal_method_name)
483   mod.send(:define_method, method_name, &compiled_method_lambda(roda_class, internal_method_name, locals_keys))
484   mod.send(:private, method_name)
485 
486   method_name
487 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
490 def define_compiled_method_cache_value(roda_class, method_name, locals_keys=EMPTY_ARRAY)
491   if compiled_method = define_compiled_method(roda_class, method_name, locals_keys)
492     [compiled_method, false].freeze
493   else
494     compiled_method
495   end
496 end
fixed_locals?()

Whether the underlying template uses fixed locals.

[show source]
    # File lib/roda/plugins/render.rb
467 def fixed_locals?
468   Render.tilt_template_fixed_locals?(@template)
469 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
451 def if_modified
452   begin
453     mtime = template_last_modified
454   rescue
455     # ignore errors
456   else
457     if mtime != @mtime
458       reset_template
459       yield
460       @mtime = mtime
461     end
462   end
463 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
428 def render(*args, &block)
429   res = nil
430   modified = false
431   if_modified do
432     res = @template.render(*args, &block)
433     modified = true
434   end
435   modified ? res : @template.render(*args, &block)
436 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
441 def template_last_modified
442   if deps = @dependencies
443     deps.map{|f| File.mtime(f)}.max
444   else
445     File.mtime(@path)
446   end
447 end