Module: ActiveSupport::Inflector

Defined in:
lib/freedom_patches/inflector_backport.rb,
lib/freedom_patches/inflector_backport.rb

Defined Under Namespace

Classes: Inflections

Constant Summary collapse

LRU_CACHE_SIZE =
200
LRU_CACHES =
[]

Class Method Summary collapse

Class Method Details

.clear_memoize!Object



12
13
14
# File 'lib/freedom_patches/inflector_backport.rb', line 12

def self.clear_memoize!
  LRU_CACHES.each(&:clear)
end

.memoize(*args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/freedom_patches/inflector_backport.rb', line 16

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

    m =
      define_method(method_name) do |*arguments|
        # this avoids recursive locks
        found = true
        data = cache.fetch(arguments) { found = false }
        cache[arguments] = data = public_send(uncached, *arguments) unless found
        # so cache is never corrupted
        data.dup
      end

    # https://bugs.ruby-lang.org/issues/16897
    ruby2_keywords(m) if Module.respond_to?(:ruby2_keywords, true)
  end
end