Class: ActionView::ReloadableTemplate

Inherits:
Template show all
Defined in:
lib/action_view/reloadable_template.rb

Defined Under Namespace

Modules: Unfreezable Classes: ReloadablePath, TemplateDeleted

Instance Attribute Summary collapse

Attributes inherited from Template

#base_path, #extension, #filename, #format, #load_path, #locale, #name, #template_path

Instance Method Summary collapse

Methods inherited from Template

#accessible_paths, #content_type, exempt_from_layout, #exempt_from_layout?, #format_and_extension, #load!, #method_segment, #mime_type, #multipart?, #path, #path_without_extension, #path_without_format_and_extension, #relative_path, #render_template, #source

Methods included from TemplateHandlers

extended, #handler_class_for_extension, #register_default_template_handler, #register_template_handler, #registered_template_handler, #template_handler_extensions

Methods included from Renderable

#compiled_source, #filename, #handler, #method_name, #method_name_without_locals, #render, #render_without_template_tracking

Constructor Details

#initialize(*args) ⇒ ReloadableTemplate

Returns a new instance of ReloadableTemplate.



80
81
82
83
84
85
# File 'lib/action_view/reloadable_template.rb', line 80

def initialize(*args)
  super
  
  # we don't ever want to get frozen
  extend Unfreezable
end

Instance Attribute Details

#previously_last_modifiedObject

Returns the value of attribute previously_last_modified.



91
92
93
# File 'lib/action_view/reloadable_template.rb', line 91

def previously_last_modified
  @previously_last_modified
end

Instance Method Details

#mtimeObject



87
88
89
# File 'lib/action_view/reloadable_template.rb', line 87

def mtime
  File.mtime(filename)
end

#reset_cache_if_stale!Object



100
101
102
103
104
105
106
107
# File 'lib/action_view/reloadable_template.rb', line 100

def reset_cache_if_stale!
  if stale?
    flush_cache 'source', 'compiled_source'
    undef_my_compiled_methods!
    @previously_last_modified = mtime
  end
  self
end

#stale?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'lib/action_view/reloadable_template.rb', line 93

def stale?
  previously_last_modified.nil? || previously_last_modified < mtime
rescue Errno::ENOENT => e
  undef_my_compiled_methods!
  raise TemplateDeleted
end

#undef_my_compiled_methods!Object

remove any compiled methods that look like they might belong to me



110
111
112
113
114
# File 'lib/action_view/reloadable_template.rb', line 110

def undef_my_compiled_methods!
  ActionView::Base::CompiledTemplates.public_instance_methods.grep(/#{Regexp.escape(method_name_without_locals)}(?:_locals_)?/).each do |m|
    ActionView::Base::CompiledTemplates.send(:remove_method, m)
  end
end