Class: Roda::RodaPlugins::Render::TemplateMtimeWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/plugins/render.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(template_class, path, dependencies, *template_args) ⇒ TemplateMtimeWrapper

Returns a new instance of TemplateMtimeWrapper.



323
324
325
326
327
328
329
330
331
# File 'lib/roda/plugins/render.rb', line 323

def initialize(template_class, path, dependencies, *template_args)
  @template_class = template_class
  @path = path
  @template_args = template_args
  @dependencies = ([path] + Array(dependencies)) if dependencies

  @mtime = template_last_modified if File.file?(path)
  @template = template_class.new(path, *template_args)
end

Instance Method Details

#define_compiled_method(roda_class, method_name, locals_keys = EMPTY_ARRAY) ⇒ Object

Compile a method in the given module with the given name that will call the compiled template method, updating the compiled template method



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/roda/plugins/render.rb', line 372

def define_compiled_method(roda_class, method_name, locals_keys=EMPTY_ARRAY)
  mod = roda_class::RodaCompiledTemplates
  internal_method_name = :"_#{method_name}"
  begin
    mod.send(:define_method, internal_method_name, send(:compiled_method, locals_keys, roda_class))
  rescue ::NotImplementedError
    return false
  end

  mod.send(:private, internal_method_name)
  mod.send(:define_method, method_name, &compiled_method_lambda(self, roda_class, internal_method_name, locals_keys))
  mod.send(:private, method_name)

  method_name
end

#modified?Boolean

If the template file has been updated, return true and update the template object and the modification time. Other return false.

Returns:

  • (Boolean)


353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/roda/plugins/render.rb', line 353

def modified?
  begin
    mtime = template_last_modified
  rescue
    # ignore errors
  else
    if mtime != @mtime
      @mtime = mtime
      @template = @template_class.new(@path, *@template_args)
      return true
    end
  end

  false
end

#render(*args, &block) ⇒ Object

If the template file exists and the modification time has changed, rebuild the template file, then call render on it.



335
336
337
338
# File 'lib/roda/plugins/render.rb', line 335

def render(*args, &block)
  modified?
  @template.render(*args, &block)
end

#template_last_modifiedObject

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.



343
344
345
346
347
348
349
# File 'lib/roda/plugins/render.rb', line 343

def template_last_modified
  if deps = @dependencies
    deps.map{|f| File.mtime(f)}.max
  else
    File.mtime(@path)
  end
end