Skip to content
This repository was archived by the owner on Jul 10, 2018. It is now read-only.

Commit 993a47b

Browse files
committed
speed up interpolation by consolidating interpolator caches
1 parent 3def1e1 commit 993a47b

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

lib/paperclip/interpolations.rb

+4-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def self.[] name
2121

2222
# Returns a sorted list of all interpolations.
2323
def self.all
24-
self.instance_methods(false).sort
24+
self.instance_methods(false).sort!
2525
end
2626

2727
# Perform the actual interpolation. Takes the pattern to interpolate
@@ -31,18 +31,14 @@ def self.all
3131
def self.interpolate pattern, *args
3232
pattern = args.first.instance.send(pattern) if pattern.kind_of? Symbol
3333
result = pattern.dup
34-
interpolators_cache.each do |tag|
35-
result.gsub!(tag_pattern_cache[tag]) { send(tag, *args) }
34+
interpolators_cache.each do |method, token|
35+
result.gsub!(token) { send(method, *args) } if result.include?(token)
3636
end
3737
result
3838
end
3939

40-
def self.tag_pattern_cache
41-
@tag_pattern_cache ||= Hash.new { |hash, key| hash[key] = ":#{key}" }
42-
end
43-
4440
def self.interpolators_cache
45-
@interpolators_cache ||= all.reverse!
41+
@interpolators_cache ||= all.reverse!.map! { |method| [method, ":#{method}"] }
4642
end
4743

4844
def self.plural_cache

0 commit comments

Comments
 (0)