Skip to content
This repository
Browse code

Include Memoizable in ActionView::Template

  • Loading branch information...
commit dd41f66af577947ad420fbd2a44184344ad5c983 1 parent 8a9934a
Joshua Peek authored July 14, 2008
15  actionpack/lib/action_view/renderable.rb
@@ -7,20 +7,17 @@ def self.included(base)
7 7
       @@mutex = Mutex.new
8 8
     end
9 9
 
  10
+    include ActiveSupport::Memoizable
  11
+
10 12
     def handler
11  
-      @handler ||= Template.handler_class_for_extension(extension)
  13
+      Template.handler_class_for_extension(extension)
12 14
     end
  15
+    memorize :handler
13 16
 
14 17
     def compiled_source
15  
-      @compiled_source ||= handler.new(nil).compile(self) if handler.compilable?
16  
-    end
17  
-
18  
-    def freeze
19  
-      # Eager load and freeze memoized methods
20  
-      handler.freeze
21  
-      compiled_source.freeze
22  
-      super
  18
+      handler.new(nil).compile(self) if handler.compilable?
23 19
     end
  20
+    memorize :compiled_source
24 21
 
25 22
     def render(view, local_assigns = {})
26 23
       view._first_render ||= self
15  actionpack/lib/action_view/renderable_partial.rb
@@ -3,20 +3,17 @@ module RenderablePartial
3 3
     # NOTE: The template that this mixin is beening include into is frozen
4 4
     # So you can not set or modify any instance variables
5 5
 
  6
+    include ActiveSupport::Memoizable
  7
+
6 8
     def variable_name
7  
-      @variable_name ||= name.sub(/\A_/, '').to_sym
  9
+      name.sub(/\A_/, '').to_sym
8 10
     end
  11
+    memorize :variable_name
9 12
 
10 13
     def counter_name
11  
-      @counter_name ||= "#{variable_name}_counter".to_sym
12  
-    end
13  
-
14  
-    def freeze
15  
-      # Eager load and freeze memoized methods
16  
-      variable_name.freeze
17  
-      counter_name.freeze
18  
-      super
  14
+      "#{variable_name}_counter".to_sym
19 15
     end
  16
+    memorize :counter_name
20 17
 
21 18
     def render(view, local_assigns = {})
22 19
       ActionController::Base.benchmark("Rendered #{path_without_format_and_extension}", Logger::DEBUG, false) do
40  actionpack/lib/action_view/template.rb
... ...
@@ -1,6 +1,7 @@
1 1
 module ActionView #:nodoc:
2 2
   class Template
3 3
     extend TemplateHandlers
  4
+    include ActiveSupport::Memoizable
4 5
     include Renderable
5 6
 
6 7
     attr_accessor :filename, :load_path, :base_path, :name, :format, :extension
@@ -16,48 +17,37 @@ def initialize(template_path, load_paths = [])
16 17
       extend RenderablePartial if @name =~ /^_/
17 18
     end
18 19
 
19  
-    def freeze
20  
-      # Eager load and freeze memoized methods
21  
-      format_and_extension.freeze
22  
-      path.freeze
23  
-      path_without_extension.freeze
24  
-      path_without_format_and_extension.freeze
25  
-      source.freeze
26  
-      method_segment.freeze
27  
-
28  
-      super
29  
-    end
30  
-
31 20
     def format_and_extension
32  
-      @format_and_extension ||= (extensions = [format, extension].compact.join(".")).blank? ? nil : extensions
  21
+      (extensions = [format, extension].compact.join(".")).blank? ? nil : extensions
33 22
     end
  23
+    memorize :format_and_extension
34 24
 
35 25
     def path
36  
-      @path ||= [base_path, [name, format, extension].compact.join('.')].compact.join('/')
  26
+      [base_path, [name, format, extension].compact.join('.')].compact.join('/')
37 27
     end
  28
+    memorize :path
38 29
 
39 30
     def path_without_extension
40  
-      @path_without_extension ||= [base_path, [name, format].compact.join('.')].compact.join('/')
  31
+      [base_path, [name, format].compact.join('.')].compact.join('/')
41 32
     end
  33
+    memorize :path_without_extension
42 34
 
43 35
     def path_without_format_and_extension
44  
-      @path_without_format_and_extension ||= [base_path, name].compact.join('/')
  36
+      [base_path, name].compact.join('/')
45 37
     end
  38
+    memorize :path_without_format_and_extension
46 39
 
47 40
     def source
48  
-      @source ||= File.read(filename)
  41
+      File.read(filename)
49 42
     end
  43
+    memorize :source
50 44
 
51 45
     def method_segment
52  
-      unless @method_segment
53  
-        segment = File.expand_path(filename)
54  
-        segment.sub!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}/, '') if defined?(RAILS_ROOT)
55  
-        segment.gsub!(/([^a-zA-Z0-9_])/) { $1.ord }
56  
-        @method_segment = segment
57  
-      end
58  
-
59  
-      @method_segment
  46
+      segment = File.expand_path(filename)
  47
+      segment.sub!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}/, '') if defined?(RAILS_ROOT)
  48
+      segment.gsub!(/([^a-zA-Z0-9_])/) { $1.ord }
60 49
     end
  50
+    memorize :method_segment
61 51
 
62 52
     def render_template(view, local_assigns = {})
63 53
       render(view, local_assigns)

0 notes on commit dd41f66

Please sign in to comment.
Something went wrong with that request. Please try again.