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
426 def initialize(roda_class, opts, template_opts)
427   @roda_class = roda_class
428   @opts = opts
429   @template_opts = template_opts
430   reset_template
431 
432   @path = opts[:path]
433   deps = opts[:dependencies]
434   @dependencies = ([@path] + Array(deps)) if deps
435   @mtime = template_last_modified
436 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
513 def compiled_method(locals_keys=EMPTY_ARRAY, roda_class=nil)
514   Render.tilt_template_compiled_method(@template, locals_keys, roda_class)
515 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
520 def compiled_method_lambda(roda_class, method_name, locals_keys=EMPTY_ARRAY)
521   mod = roda_class::RodaCompiledTemplates
522   template = self
523   lambda do |locals, &block|
524     template.if_modified do
525       mod.send(:define_method, method_name, Render.tilt_template_compiled_method(template, locals_keys, roda_class))
526       mod.send(:private, method_name)
527     end
528 
529     _call_optimized_template_method([method_name, Render.tilt_template_fixed_locals?(template)], locals, &block)
530   end
531 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
485 def define_compiled_method(roda_class, method_name, locals_keys=EMPTY_ARRAY)
486   mod = roda_class::RodaCompiledTemplates
487   internal_method_name = :"_#{method_name}"
488   begin
489     mod.send(:define_method, internal_method_name, compiled_method(locals_keys, roda_class))
490   rescue ::NotImplementedError
491     return false
492   end
493 
494   mod.send(:private, internal_method_name)
495   mod.send(:define_method, method_name, &compiled_method_lambda(roda_class, internal_method_name, locals_keys))
496   mod.send(:private, method_name)
497 
498   method_name
499 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
502 def define_compiled_method_cache_value(roda_class, method_name, locals_keys=EMPTY_ARRAY)
503   if compiled_method = define_compiled_method(roda_class, method_name, locals_keys)
504     [compiled_method, false].freeze
505   else
506     compiled_method
507   end
508 end
fixed_locals?()

Whether the underlying template uses fixed locals.

[show source]
    # File lib/roda/plugins/render.rb
479 def fixed_locals?
480   Render.tilt_template_fixed_locals?(@template)
481 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
463 def if_modified
464   begin
465     mtime = template_last_modified
466   rescue
467     # ignore errors
468   else
469     if mtime != @mtime
470       reset_template
471       yield
472       @mtime = mtime
473     end
474   end
475 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
440 def render(*args, &block)
441   res = nil
442   modified = false
443   if_modified do
444     res = @template.render(*args, &block)
445     modified = true
446   end
447   modified ? res : @template.render(*args, &block)
448 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
453 def template_last_modified
454   if deps = @dependencies
455     deps.map{|f| File.mtime(f)}.max
456   else
457     File.mtime(@path)
458   end
459 end