Module: MemoizedInflectors

Defined in:
lib/memoized_inflectors.rb,
lib/memoized_inflectors/version.rb,
lib/memoized_inflectors/string_methods.rb,
lib/memoized_inflectors/integer_methods.rb

Defined Under Namespace

Modules: IntegerMethods, StringMethods

Constant Summary collapse

VERSION =
"1.0.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.disabledObject

Returns the value of attribute disabled.



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

def disabled
  @disabled
end

Class Method Details

.cache_for(inflector) ⇒ Object

Returns the cache for the given inflector. Currently inflector names must be globally unique which works because there are no name collisions between the String and Integer inflectors.

Example

MemoizedInflectors.cache_for(:classify)


30
31
32
# File 'lib/memoized_inflectors.rb', line 30

def self.cache_for(inflector)
  caches[inflector]
end

.cachesObject



19
20
21
# File 'lib/memoized_inflectors.rb', line 19

def self.caches
  @caches ||= ::Hash.new { |h,k| h[k] = new_cache_instance }
end

.clear_cache(*inflectors) ⇒ Object

Clears the cache for the specified inflector(s). If no inflector is specified, then all caches are cleared.

Examples

MemoizedInflectors.clear_cache                           # Clears everything.
MemoizedInflectors.clear_cache(:classify)                # Clear only the :classify cache.
MemoizedInflectors.clear_cache(:classify, :underscore)   # Clears both the :classify and :underscore caches.


49
50
51
52
53
54
55
56
57
# File 'lib/memoized_inflectors.rb', line 49

def self.clear_cache(*inflectors)
  if inflectors.any?
    inflectors.each do |inflector|
      cache_for(inflector).clear
    end
  else
    caches.values.each(&:clear)
  end
end

.inflector_methodsObject



59
60
61
# File 'lib/memoized_inflectors.rb', line 59

def self.inflector_methods
  ::ActiveSupport::Inflector.instance_methods
end

.key_for(*args) ⇒ Object

Returns a unique key for the given arguments.



35
36
37
38
39
40
# File 'lib/memoized_inflectors.rb', line 35

def self.key_for(*args)
  # Using Object#hash rather than MD5 or SHA because it works on arrays, it is faster, and it
  # also works correctly with hashes without manipulation. I.e. it is simply easier. This method
  # works since there is no intention of persisting the cache or sharing it across VM instances.
  args.hash
end

.new_cache_instanceObject



15
16
17
# File 'lib/memoized_inflectors.rb', line 15

def self.new_cache_instance
  ::LruRedux::ThreadSafeCache.new(1000)  # TODO: allow users to configure the class (e.g. ThreadSafe vs non-ThreadSafe) and max size *
end