Class | ActionView::CompiledTemplates |
In: |
vendor/rails/actionpack/lib/action_view/compiled_templates.rb
|
Parent: | Module |
CompiledTemplates modules hold methods that have been compiled. Templates are compiled into these methods so that they do not need to be read and parsed for each request.
Each template may be compiled into one or more methods. Each method accepts a given set of parameters which is used to implement local assigns passing.
To use a compiled template module, create a new instance and include it into the class in which you want the template to be rendered.
method_names | [R] |
# File vendor/rails/actionpack/lib/action_view/compiled_templates.rb, line 16 16: def initialize 17: @method_names = Hash.new do |hash, key| 18: hash[key] = "__compiled_method_#{(hash.length + 1)}" 19: end 20: @mtimes = {} 21: end
Compile the provided source code for the given argument names and with the given initial line number. The identifier should be unique to this source.
The file_name, if provided will appear in backtraces. If not provded, the file_name defaults to the identifier.
This method will return the selector for the compiled version of this method.
# File vendor/rails/actionpack/lib/action_view/compiled_templates.rb, line 47 47: def compile_source(identifier, arg_names, source, initial_line_number = 0, file_name = nil) 48: file_name ||= identifier 49: name = method_names[full_key(identifier, arg_names)] 50: arg_desc = arg_names.empty? ? '' : "(#{arg_names * ', '})" 51: fake_file_name = "#{file_name}#{arg_desc}" # Include the arguments for this version (for now) 52: 53: method_def = wrap_source(name, arg_names, source) 54: 55: begin 56: module_eval(method_def, fake_file_name, initial_line_number) 57: @mtimes[full_key(identifier, arg_names)] = Time.now 58: rescue Exception => e # errors from compiled source 59: e.blame_file! identifier 60: raise 61: end 62: name 63: end
Return the full key for the given identifier and argument names
# File vendor/rails/actionpack/lib/action_view/compiled_templates.rb, line 24 24: def full_key(identifier, arg_names) 25: [identifier, arg_names] 26: end
Return the time at which the method for the given identifier and argument names was compiled.
# File vendor/rails/actionpack/lib/action_view/compiled_templates.rb, line 36 36: def mtime(identifier, arg_names) 37: @mtimes[full_key(identifier, arg_names)] 38: end
Return the selector for this method or nil if it has not been compiled
# File vendor/rails/actionpack/lib/action_view/compiled_templates.rb, line 29 29: def selector(identifier, arg_names) 30: key = full_key(identifier, arg_names) 31: method_names.key?(key) ? method_names[key] : nil 32: end