Class: ActionView::CompiledTemplates
- Defined in:
- lib/action_view/compiled_templates.rb
Overview
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.
Instance Attribute Summary collapse
-
#method_names ⇒ Object
readonly
Returns the value of attribute method_names.
Instance Method Summary collapse
-
#compile_source(identifier, arg_names, source, initial_line_number = 0, file_name = nil) ⇒ Object
Compile the provided source code for the given argument names and with the given initial line number.
-
#full_key(identifier, arg_names) ⇒ Object
Return the full key for the given identifier and argument names.
-
#initialize ⇒ CompiledTemplates
constructor
A new instance of CompiledTemplates.
-
#mtime(identifier, arg_names) ⇒ Object
Return the time at which the method for the given identifier and argument names was compiled.
-
#selector(identifier, arg_names) ⇒ Object
(also: #compiled?)
Return the selector for this method or nil if it has not been compiled.
-
#wrap_source(name, arg_names, source) ⇒ Object
Wrap the provided source in a def …
Constructor Details
#initialize ⇒ CompiledTemplates
Returns a new instance of CompiledTemplates.
15 16 17 18 19 20 |
# File 'lib/action_view/compiled_templates.rb', line 15 def initialize @method_names = Hash.new do |hash, key| hash[key] = "__compiled_method_#{(hash.length + 1)}" end @mtimes = {} end |
Instance Attribute Details
#method_names ⇒ Object (readonly)
Returns the value of attribute method_names.
13 14 15 |
# File 'lib/action_view/compiled_templates.rb', line 13 def method_names @method_names end |
Instance Method Details
#compile_source(identifier, arg_names, source, initial_line_number = 0, file_name = nil) ⇒ Object
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 provided, the file_name defaults to the identifier.
This method will return the selector for the compiled version of this method.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/action_view/compiled_templates.rb', line 46 def compile_source(identifier, arg_names, source, initial_line_number = 0, file_name = nil) file_name ||= identifier name = method_names[full_key(identifier, arg_names)] arg_desc = arg_names.empty? ? '' : "(#{arg_names * ', '})" fake_file_name = "#{file_name}#{arg_desc}" # Include the arguments for this version (for now) method_def = wrap_source(name, arg_names, source) begin module_eval(method_def, fake_file_name, initial_line_number) @mtimes[full_key(identifier, arg_names)] = Time.now rescue Exception => e # errors from compiled source e.blame_file! identifier raise end name end |
#full_key(identifier, arg_names) ⇒ Object
Return the full key for the given identifier and argument names
23 24 25 |
# File 'lib/action_view/compiled_templates.rb', line 23 def full_key(identifier, arg_names) [identifier, arg_names] end |
#mtime(identifier, arg_names) ⇒ Object
Return the time at which the method for the given identifier and argument names was compiled.
35 36 37 |
# File 'lib/action_view/compiled_templates.rb', line 35 def mtime(identifier, arg_names) @mtimes[full_key(identifier, arg_names)] end |
#selector(identifier, arg_names) ⇒ Object Also known as: compiled?
Return the selector for this method or nil if it has not been compiled
28 29 30 31 |
# File 'lib/action_view/compiled_templates.rb', line 28 def selector(identifier, arg_names) key = full_key(identifier, arg_names) method_names.key?(key) ? method_names[key] : nil end |
#wrap_source(name, arg_names, source) ⇒ Object
Wrap the provided source in a def … end block.
65 66 67 |
# File 'lib/action_view/compiled_templates.rb', line 65 def wrap_source(name, arg_names, source) "def #{name}(#{arg_names * ', '})\n#{source}\nend" end |