Class: Roda::RodaPlugins::Render::TemplateMtimeWrapper
- Inherits:
-
Object
- Object
- Roda::RodaPlugins::Render::TemplateMtimeWrapper
- 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
-
#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.
-
#define_compiled_method_cache_value(roda_class, method_name, locals_keys = EMPTY_ARRAY) ⇒ Object
Returns an appropriate value for the template method cache.
-
#fixed_locals? ⇒ Boolean
Whether the underlying template uses fixed locals.
-
#if_modified ⇒ Object
If the template file has been updated, return true and update the template object and the modification time.
-
#initialize(roda_class, opts, template_opts) ⇒ TemplateMtimeWrapper
constructor
A new instance of TemplateMtimeWrapper.
-
#render(*args, &block) ⇒ Object
If the template file exists and the modification time has changed, rebuild the template file, then call render on it.
-
#template_last_modified ⇒ Object
Return when the template was last modified.
Constructor Details
#initialize(roda_class, opts, template_opts) ⇒ TemplateMtimeWrapper
Returns a new instance of TemplateMtimeWrapper.
426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/roda/plugins/render.rb', line 426 def initialize(roda_class, opts, template_opts) @roda_class = roda_class @opts = opts @template_opts = template_opts reset_template @path = opts[:path] deps = opts[:dependencies] @dependencies = ([@path] + Array(deps)) if deps @mtime = template_last_modified 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
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/roda/plugins/render.rb', line 485 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, 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(roda_class, internal_method_name, locals_keys)) mod.send(:private, method_name) method_name end |
#define_compiled_method_cache_value(roda_class, method_name, locals_keys = EMPTY_ARRAY) ⇒ Object
Returns an appropriate value for the template method cache.
502 503 504 505 506 507 508 |
# File 'lib/roda/plugins/render.rb', line 502 def define_compiled_method_cache_value(roda_class, method_name, locals_keys=EMPTY_ARRAY) if compiled_method = define_compiled_method(roda_class, method_name, locals_keys) [compiled_method, false].freeze else compiled_method end end |
#fixed_locals? ⇒ Boolean
Whether the underlying template uses fixed locals.
479 480 481 |
# File 'lib/roda/plugins/render.rb', line 479 def fixed_locals? Render.tilt_template_fixed_locals?(@template) end |
#if_modified ⇒ Object
If the template file has been updated, return true and update the template object and the modification time. Other return false.
463 464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'lib/roda/plugins/render.rb', line 463 def if_modified begin mtime = template_last_modified rescue # ignore errors else if mtime != @mtime reset_template yield @mtime = mtime end end 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.
440 441 442 443 444 445 446 447 448 |
# File 'lib/roda/plugins/render.rb', line 440 def render(*args, &block) res = nil modified = false if_modified do res = @template.render(*args, &block) modified = true end modified ? res : @template.render(*args, &block) end |
#template_last_modified ⇒ Object
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.
453 454 455 456 457 458 459 |
# File 'lib/roda/plugins/render.rb', line 453 def template_last_modified if deps = @dependencies deps.map{|f| File.mtime(f)}.max else File.mtime(@path) end end |