Class: Jinx::UniquifierCache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/jinx/resource/uniquifier_cache.rb

Overview

A utility class to cache key value qualifiers.

Instance Method Summary collapse

Constructor Details

#initializeUniquifierCache

Returns a new instance of UniquifierCache.



10
11
12
# File 'lib/jinx/resource/uniquifier_cache.rb', line 10

def initialize
  @cache = Jinx::LazyHash.new { Hash.new }
end

Instance Method Details

#clearObject

Clears all cache entries.



30
31
32
# File 'lib/jinx/resource/uniquifier_cache.rb', line 30

def clear
  @cache.clear
end

#get(obj, value) ⇒ String

Returns the unique value generated for the given object and value. Successive calls to this method for domain objects of the same class and a given value return the same result.

Examples:

Jinx::UniquifierCache.instance.get(person, 'Groucho') #=> Groucho_wy874e6e
Jinx::UniquifierCache.instance.get(person.copy, 'Groucho') #=> Groucho_wy87ye6e

Parameters:

  • obj (Resource)

    the domain object containing the value

  • value (String)

    the value to make unique

Returns:

  • (String)

    the unique value



25
26
27
# File 'lib/jinx/resource/uniquifier_cache.rb', line 25

def get(obj, value)
  @cache[obj.class][value] ||= StringUniquifier.uniquify(value)
end