2005-08-26 18:37:36 -04:00
|
|
|
module ActionView
|
|
|
|
|
|
|
|
# CompiledTemplates modules hold methods that have been compiled.
|
|
|
|
# Templates are compiled into these methods so that they do not need to be
|
2007-03-27 10:04:06 -04:00
|
|
|
# read and parsed for each request.
|
2005-08-26 18:37:36 -04:00
|
|
|
#
|
|
|
|
# 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.
|
2007-03-27 10:04:06 -04:00
|
|
|
class CompiledTemplates < Module
|
2005-08-26 18:37:36 -04:00
|
|
|
attr_reader :method_names
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@method_names = Hash.new do |hash, key|
|
|
|
|
hash[key] = "__compiled_method_#{(hash.length + 1)}"
|
|
|
|
end
|
|
|
|
@mtimes = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return the full key for the given identifier and argument names
|
|
|
|
def full_key(identifier, arg_names)
|
|
|
|
[identifier, arg_names]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return the selector for this method or nil if it has not been compiled
|
|
|
|
def selector(identifier, arg_names)
|
|
|
|
key = full_key(identifier, arg_names)
|
|
|
|
method_names.key?(key) ? method_names[key] : nil
|
|
|
|
end
|
|
|
|
alias :compiled? :selector
|
|
|
|
|
|
|
|
# Return the time at which the method for the given identifier and argument names was compiled.
|
|
|
|
def mtime(identifier, arg_names)
|
|
|
|
@mtimes[full_key(identifier, arg_names)]
|
|
|
|
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.
|
|
|
|
#
|
2007-05-06 00:46:11 -04:00
|
|
|
# The file_name, if provided will appear in backtraces. If not provided, the file_name defaults
|
2005-08-26 18:37:36 -04:00
|
|
|
# to the identifier.
|
|
|
|
#
|
|
|
|
# This method will return the selector for the compiled version of this method.
|
|
|
|
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
|
2006-08-30 23:16:28 -04:00
|
|
|
rescue Exception => e # errors from compiled source
|
2005-08-26 18:37:36 -04:00
|
|
|
e.blame_file! identifier
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
name
|
|
|
|
end
|
|
|
|
|
|
|
|
# Wrap the provided source in a def ... end block.
|
|
|
|
def wrap_source(name, arg_names, source)
|
|
|
|
"def #{name}(#{arg_names * ', '})\n#{source}\nend"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|