Commit 993a47b2 by Dave Gynn

speed up interpolation by consolidating interpolator caches

parent 3def1e1e
...@@ -21,7 +21,7 @@ module Paperclip ...@@ -21,7 +21,7 @@ module Paperclip
# Returns a sorted list of all interpolations. # Returns a sorted list of all interpolations.
def self.all def self.all
self.instance_methods(false).sort self.instance_methods(false).sort!
end end
# Perform the actual interpolation. Takes the pattern to interpolate # Perform the actual interpolation. Takes the pattern to interpolate
...@@ -31,18 +31,14 @@ module Paperclip ...@@ -31,18 +31,14 @@ module Paperclip
def self.interpolate pattern, *args def self.interpolate pattern, *args
pattern = args.first.instance.send(pattern) if pattern.kind_of? Symbol pattern = args.first.instance.send(pattern) if pattern.kind_of? Symbol
result = pattern.dup result = pattern.dup
interpolators_cache.each do |tag| interpolators_cache.each do |method, token|
result.gsub!(tag_pattern_cache[tag]) { send(tag, *args) } result.gsub!(token) { send(method, *args) } if result.include?(token)
end end
result result
end end
def self.tag_pattern_cache
@tag_pattern_cache ||= Hash.new { |hash, key| hash[key] = ":#{key}" }
end
def self.interpolators_cache def self.interpolators_cache
@interpolators_cache ||= all.reverse! @interpolators_cache ||= all.reverse!.map! { |method| [method, ":#{method}"] }
end end
def self.plural_cache def self.plural_cache
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment