Commit 3def1e1e by Dave Gynn

improve interpolation speed and memory usage

parent e7814861
...@@ -10,6 +10,7 @@ module Paperclip ...@@ -10,6 +10,7 @@ module Paperclip
# and is not intended for normal use. # and is not intended for normal use.
def self.[]= name, block def self.[]= name, block
define_method(name, &block) define_method(name, &block)
@interpolators_cache = nil
end end
# Hash access of interpolations. Included only for compatibility, # Hash access of interpolations. Included only for compatibility,
...@@ -29,11 +30,19 @@ module Paperclip ...@@ -29,11 +30,19 @@ module Paperclip
# an interpolation pattern for Paperclip to use. # an interpolation pattern for Paperclip to use.
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
all.reverse.inject(pattern) do |result, tag| result = pattern.dup
result.gsub(/:#{tag}/) do |match| interpolators_cache.each do |tag|
send( tag, *args ) result.gsub!(tag_pattern_cache[tag]) { send(tag, *args) }
end
end end
result
end
def self.tag_pattern_cache
@tag_pattern_cache ||= Hash.new { |hash, key| hash[key] = ":#{key}" }
end
def self.interpolators_cache
@interpolators_cache ||= all.reverse!
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