Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/actionpack/lib/action_view/compiled_templates.rb

Revision 6679, 2.5 kB (checked in by marcel, 1 year ago)

Fix various documentation typos throughout ActionPack. Closes #8111. [Henrik N]

Line 
1 module ActionView
2
3   # CompiledTemplates modules hold methods that have been compiled.
4   # Templates are compiled into these methods so that they do not need to be
5   # read and parsed for each request.
6   #
7   # Each template may be compiled into one or more methods. Each method accepts a given
8   # set of parameters which is used to implement local assigns passing.
9   #
10   # To use a compiled template module, create a new instance and include it into the class
11   # in which you want the template to be rendered.
12   class CompiledTemplates < Module
13     attr_reader :method_names
14
15     def initialize
16       @method_names = Hash.new do |hash, key|
17         hash[key] = "__compiled_method_#{(hash.length + 1)}"
18       end
19       @mtimes = {}
20     end
21    
22     # Return the full key for the given identifier and argument names
23     def full_key(identifier, arg_names)
24       [identifier, arg_names]
25     end
26
27     # Return the selector for this method or nil if it has not been compiled
28     def selector(identifier, arg_names)
29       key = full_key(identifier, arg_names)
30       method_names.key?(key) ? method_names[key] : nil
31     end
32     alias :compiled? :selector
33
34     # Return the time at which the method for the given identifier and argument names was compiled.
35     def mtime(identifier, arg_names)
36       @mtimes[full_key(identifier, arg_names)]
37     end
38    
39     # Compile the provided source code for the given argument names and with the given initial line number.
40     # The identifier should be unique to this source.
41     #
42     # The file_name, if provided will appear in backtraces. If not provided, the file_name defaults
43     # to the identifier.
44     #
45     # This method will return the selector for the compiled version of this method.
46     def compile_source(identifier, arg_names, source, initial_line_number = 0, file_name = nil)
47       file_name ||= identifier
48       name = method_names[full_key(identifier, arg_names)]
49       arg_desc = arg_names.empty? ? '' : "(#{arg_names * ', '})"
50       fake_file_name = "#{file_name}#{arg_desc}" # Include the arguments for this version (for now)
51      
52       method_def = wrap_source(name, arg_names, source)
53      
54       begin
55         module_eval(method_def, fake_file_name, initial_line_number)
56         @mtimes[full_key(identifier, arg_names)] = Time.now
57       rescue Exception => e  # errors from compiled source
58         e.blame_file! identifier
59         raise
60       end
61       name
62     end
63    
64     # Wrap the provided source in a def ... end block.
65     def wrap_source(name, arg_names, source)
66       "def #{name}(#{arg_names * ', '})\n#{source}\nend"
67     end
68   end
69 end
Note: See TracBrowser for help on using the browser.