From 0b76166ad1c761647f9910ca803be90645c59191 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 24 Apr 2013 12:00:40 +1000 Subject: [PATCH] inflector backport --- lib/freedom_patches/inflector_backport.rb | 62 +++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 lib/freedom_patches/inflector_backport.rb diff --git a/lib/freedom_patches/inflector_backport.rb b/lib/freedom_patches/inflector_backport.rb new file mode 100644 index 00000000000..ff2f63fe44d --- /dev/null +++ b/lib/freedom_patches/inflector_backport.rb @@ -0,0 +1,62 @@ +# review per rails release, this speeds up the inflector, we are not inflecting too much at the moment, except in dev +# +# note: I am working with the rails team on including this in official rails + +module ActiveSupport + module Inflector + + LRU_CACHE_SIZE = 200 + LRU_CACHES = [] + + def self.clear_memoize! + LRU_CACHES.each(&:clear) + end + + def self.memoize(*args) + args.each do |method_name| + cache = LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE) + LRU_CACHES << cache + + uncached = "#{method_name}_without_cache" + alias_method uncached, method_name + + define_method(method_name) do |*args| + # this avoids recursive locks + found = true + data = cache.fetch(args){found = false} + unless found + cache[args] = data = send(uncached, *args) + end + data + end + end + end + + memoize :pluralize, :singularize, :camelize, :underscore, :humanize, + :titleize, :tableize, :classify, :foreign_key + end +end + +module ActiveSupport + module Inflector + class Inflections + def self.clear_memoize(*args) + args.each do |method_name| + orig = "#{method_name}_without_clear_memoize" + alias_method orig, method_name + define_method(method_name) do |*args| + ActiveSupport::Inflector.clear_memoize! + send(orig, *args) + end + end + end + + clear_memoize :acronym, :plural, :singular, :irregular, :uncountable, :human, :clear + end + end +end + + + + +