Commit 18e1c5a2 by Dave Gynn

optimize Interpolations::PluralCache to require few object creations

by using the attachment name (symbol) and Class as keys we reduce the number of Strings created before hitting the cache
parent 993a47b2
......@@ -68,7 +68,7 @@ module Paperclip
# +url_generator+ - the object used to generate URLs, using the interpolator. Defaults to Paperclip::UrlGenerator
# +escape_url+ - Perform URI escaping to URLs. Defaults to true
def initialize(name, instance, options = {})
@name = name
@name = name.to_sym
@instance = instance
options = self.class.default_options.deep_merge(options)
......
......@@ -90,7 +90,7 @@ module Paperclip
# all class names. Calling #class will return the expected class.
def class attachment = nil, style_name = nil
return super() if attachment.nil? && style_name.nil?
plural_cache.underscore_and_pluralize(attachment.instance.class.to_s)
plural_cache.underscore_and_pluralize_class(attachment.instance.class)
end
# Returns the basename of the file. e.g. "file" for "file.jpg"
......@@ -186,7 +186,7 @@ module Paperclip
# Returns the pluralized form of the attachment name. e.g.
# "avatars" for an attachment of :avatar
def attachment attachment, style_name
plural_cache.pluralize(attachment.name.to_s.downcase)
plural_cache.pluralize_symbol(attachment.name)
end
# Returns the style, or the default style if nil is supplied.
......
......@@ -2,15 +2,16 @@ module Paperclip
module Interpolations
class PluralCache
def initialize
@cache = {}
@symbol_cache = {}.compare_by_identity
@klass_cache = {}.compare_by_identity
end
def pluralize(word)
@cache[word] ||= word.pluralize
def pluralize_symbol(symbol)
@symbol_cache[symbol] ||= symbol.to_s.downcase.pluralize
end
def underscore_and_pluralize(word)
@cache[word] ||= word.underscore.pluralize
def underscore_and_pluralize_class(klass)
@klass_cache[klass] ||= klass.name.underscore.pluralize
end
end
end
......
......@@ -24,9 +24,10 @@ describe Paperclip::Interpolations do
end
it "returns the class of the instance" do
class Thing ; end
attachment = mock
attachment.expects(:instance).returns(attachment)
attachment.expects(:class).returns("Thing")
attachment.expects(:class).returns(Thing)
assert_equal "things", Paperclip::Interpolations.class(attachment, :style)
end
......
......@@ -3,34 +3,35 @@ require 'spec_helper'
describe 'Plural cache' do
it 'caches pluralizations' do
cache = Paperclip::Interpolations::PluralCache.new
word = "box"
symbol = :box
word.expects(:pluralize).returns("boxes").once
cache.pluralize(word)
cache.pluralize(word)
first = cache.pluralize_symbol(symbol)
second = cache.pluralize_symbol(symbol)
expect(first).to equal(second)
end
it 'caches pluralizations and underscores' do
class BigBox ; end
cache = Paperclip::Interpolations::PluralCache.new
word = "BigBox"
word.expects(:pluralize).returns(word).once
word.expects(:underscore).returns(word).once
klass = BigBox
cache.underscore_and_pluralize(word)
cache.underscore_and_pluralize(word)
first = cache.underscore_and_pluralize_class(klass)
second = cache.underscore_and_pluralize_class(klass)
expect(first).to equal(second)
end
it 'pluralizes words' do
cache = Paperclip::Interpolations::PluralCache.new
word = "box"
assert_equal "boxes", cache.pluralize(word)
symbol = :box
expect(cache.pluralize_symbol(symbol)).to eq("boxes")
end
it 'pluralizes and underscore words' do
it 'pluralizes and underscore class names' do
class BigBox ; end
cache = Paperclip::Interpolations::PluralCache.new
word = "BigBox"
assert_equal "big_boxes", cache.underscore_and_pluralize(word)
klass = BigBox
expect(cache.underscore_and_pluralize_class(klass)).to eq("big_boxes")
end
end
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