Skip to content

Commit

Permalink
memorize typo
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Jul 15, 2008
1 parent dd41f66 commit 001c8be
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/renderable.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ def self.included(base)
def handler def handler
Template.handler_class_for_extension(extension) Template.handler_class_for_extension(extension)
end end
memorize :handler memoize :handler


def compiled_source def compiled_source
handler.new(nil).compile(self) if handler.compilable? handler.new(nil).compile(self) if handler.compilable?
end end
memorize :compiled_source memoize :compiled_source


def render(view, local_assigns = {}) def render(view, local_assigns = {})
view._first_render ||= self view._first_render ||= self
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/renderable_partial.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ module RenderablePartial
def variable_name def variable_name
name.sub(/\A_/, '').to_sym name.sub(/\A_/, '').to_sym
end end
memorize :variable_name memoize :variable_name


def counter_name def counter_name
"#{variable_name}_counter".to_sym "#{variable_name}_counter".to_sym
end end
memorize :counter_name memoize :counter_name


def render(view, local_assigns = {}) def render(view, local_assigns = {})
ActionController::Base.benchmark("Rendered #{path_without_format_and_extension}", Logger::DEBUG, false) do ActionController::Base.benchmark("Rendered #{path_without_format_and_extension}", Logger::DEBUG, false) do
Expand Down
12 changes: 6 additions & 6 deletions actionpack/lib/action_view/template.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,34 +20,34 @@ def initialize(template_path, load_paths = [])
def format_and_extension def format_and_extension
(extensions = [format, extension].compact.join(".")).blank? ? nil : extensions (extensions = [format, extension].compact.join(".")).blank? ? nil : extensions
end end
memorize :format_and_extension memoize :format_and_extension


def path def path
[base_path, [name, format, extension].compact.join('.')].compact.join('/') [base_path, [name, format, extension].compact.join('.')].compact.join('/')
end end
memorize :path memoize :path


def path_without_extension def path_without_extension
[base_path, [name, format].compact.join('.')].compact.join('/') [base_path, [name, format].compact.join('.')].compact.join('/')
end end
memorize :path_without_extension memoize :path_without_extension


def path_without_format_and_extension def path_without_format_and_extension
[base_path, name].compact.join('/') [base_path, name].compact.join('/')
end end
memorize :path_without_format_and_extension memoize :path_without_format_and_extension


def source def source
File.read(filename) File.read(filename)
end end
memorize :source memoize :source


def method_segment def method_segment
segment = File.expand_path(filename) segment = File.expand_path(filename)
segment.sub!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}/, '') if defined?(RAILS_ROOT) segment.sub!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}/, '') if defined?(RAILS_ROOT)
segment.gsub!(/([^a-zA-Z0-9_])/) { $1.ord } segment.gsub!(/([^a-zA-Z0-9_])/) { $1.ord }
end end
memorize :method_segment memoize :method_segment


def render_template(view, local_assigns = {}) def render_template(view, local_assigns = {})
render(view, local_assigns) render(view, local_assigns)
Expand Down
6 changes: 3 additions & 3 deletions activesupport/lib/active_support/memoizable.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def self.included(base) #:nodoc:
end end


module ClassMethods module ClassMethods
def memorize(symbol) def memoize(symbol)
original_method = "_unmemorized_#{symbol}" original_method = "_unmemoized_#{symbol}"
alias_method original_method, symbol alias_method original_method, symbol
class_eval <<-EOS, __FILE__, __LINE__ class_eval <<-EOS, __FILE__, __LINE__
def #{symbol} def #{symbol}
Expand All @@ -22,7 +22,7 @@ def #{symbol}


def freeze def freeze
methods.each do |method| methods.each do |method|
if m = method.to_s.match(/^_unmemorized_(.*)/) if m = method.to_s.match(/^_unmemoized_(.*)/)
send(m[1]).freeze send(m[1]).freeze
end end
end end
Expand Down
4 changes: 2 additions & 2 deletions activesupport/test/memoizable_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class Person
def name def name
fetch_name_from_floppy fetch_name_from_floppy
end end
memorize :name memoize :name


def age def age
nil nil
end end
memorize :age memoize :age


private private
def fetch_name_from_floppy def fetch_name_from_floppy
Expand Down

1 comment on commit 001c8be

@humanzz
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have previously implemented the memoize feature as method_cache plugin at http://github.com/humanzz/method_cache. But method_cache had a couple more features: in addition to caching results in instance variables it also stored the result in the cache store. It’d be great if memoize adds such feature.

Please sign in to comment.