class LittleWeasel::Services::DictionaryCacheService

This class provides methods and attributes that can be used to manage the dictionary cache. The “dictionary cache” is a simple Hash that provides access to informaiton related to dictionaries through a dictionary “key”. A dictionary “key” is a unique key comprised of a locale and optional “tag” (see Modules::Taggable and DictionaryKey for more information). The dictionary cache also provides a way for dictionary objects to share dictionary information, in particular, the dictionary file and dictionary metadata.

Attributes

dictionary_cache[RW]

Public Class Methods

count(dictionary_cache:) click to toggle source

Returns the number of dictionaries currently in the cache.

# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 99
def count(dictionary_cache:)
  dictionary_cache.dig(self::DICTIONARY_CACHE, self::DICTIONARIES)&.keys&.count || 0
end
init(dictionary_cache:) click to toggle source

This method resets dictionary_cache to its initialized state. This class method is different from the init instance method in that ALL dictionary references and ALL dictionaries are initialized.

# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 86
def init(dictionary_cache:)
  Modules::DictionaryCacheKeys.initialize_dictionary_cache dictionary_cache: dictionary_cache
end
init?(dictionary_cache:) click to toggle source

Returns true if the dictionary cache is initialized; that is, if the given dictionary_cache is in the same state the dictionary cache would be in after .init were called.

# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 93
def init?(dictionary_cache:)
  initialized_dictionary_cache = init(dictionary_cache: {})
  dictionary_cache.eql?(initialized_dictionary_cache)
end
new(dictionary_key:, dictionary_cache:) click to toggle source

This class produces the following (example) Hash that represents the dictionary cache structure:

@example This is an example:

{

'dictionary_cache' =>
{
  'dictionary_references' =>
  {
    'en' =>
    {
      'dictionary_id' => 19ec7845
    },
    'en-US' =>
    {
      'dictionary_id' => 0987a3f2
    },
    'en-US-temp' =>
    {
      'dictionary_id' => 9273eac6
    }
  },
  'dictionaries' =>
  {
    19ec7845 =>
      {
        'source' => '/en.txt',
        'dictionary_object' => {}
      },
    0987a3f2 =>
      {
        'source' => '/en-US.txt',
        'dictionary_object' => {}
      },
    9273eac6 =>
      {
        'source' => '*736ed423',
        'dictionary_object' => {}
      }
  }
}

}

# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 71
def initialize(dictionary_key:, dictionary_cache:)
  validate_dictionary_key dictionary_key: dictionary_key
  self.dictionary_key = dictionary_key

  validate_dictionary_cache dictionary_cache: dictionary_cache
  self.dictionary_cache = dictionary_cache

  self.class.init(dictionary_cache: dictionary_cache) unless dictionary_cache[DICTIONARY_CACHE]
end

Public Instance Methods

add_dictionary_source(source:) click to toggle source

Adds a dictionary source. A “dictionary source” specifies the source from which the dictionary ultimately obtains its words.

@param source [String] the dictionary source. This can be a file path or a memory source indicator to signify that the dictionary was created dynamically from memory.

# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 138
def add_dictionary_source(source:)
  validate_dictionary_source_does_not_exist dictionary_cache_service: self

  dictionary_id = dictionary_id_for_dictionary_source(source: source)
  self.dictionary_reference = dictionary_id
  # Only set the dictionary source if it doesn't already exist because settings
  # the dictionary source wipes out the #dictionary_object; dictionary objects
  # can have more than one dictionary reference pointing to them, and we don't
  # want to blow away the #dictionary_object, metadata, or any other data
  # associated with it if it already exists.
  self.dictionary_source = source unless dictionary?
  self
end
dictionary?() click to toggle source

Returns true if a dictionaries Hash key exists for the given dictionary_id in the dictionary cache. This method is only concerned with the existance of the key and has nothing to do with whether or not file/memory sources are present or the presence of a dictionary object.

# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 128
def dictionary?
  dictionary_cache[DICTIONARY_CACHE][DICTIONARIES].key? dictionary_id
end
dictionary_exists?()
Alias for: dictionary_object?
dictionary_file()
Alias for: dictionary_source
dictionary_file!()
Alias for: dictionary_source!
dictionary_id() click to toggle source

Returns the dictionary id if there is a dictionary id in the dictionary cache associated with the given key; nil otherwise.

# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 154
def dictionary_id
  dictionary_cache.dig(DICTIONARY_CACHE, DICTIONARY_REFERENCES, key, DICTIONARY_ID)
end
dictionary_id!() click to toggle source

Returns the dictionary id if there is a dictionary id in the dictionary cache associated with the given key. This method raises an error if the dictionary id cannot be found.

# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 161
def dictionary_id!
  return dictionary_id if dictionary_id?

  raise ArgumentError, "A dictionary id could not be found for key '#{key}'."
end
dictionary_object() click to toggle source
# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 199
def dictionary_object
  dictionary_cache.dig(DICTIONARY_CACHE, DICTIONARIES, dictionary_id, DICTIONARY_OBJECT)
end
dictionary_object!() click to toggle source

Returns the dictionary object from the dictionary cache for the given key. This method raises an error if the dictionary is not in the cache; that is, if the dictionary was not previously loaded from disk or memory.

# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 190
def dictionary_object!
  unless dictionary_object?
    raise ArgumentError,
      "The dictionary object associated with argument key '#{key}' is not in the cache."
  end

  dictionary_object
end
dictionary_object=(object) click to toggle source
# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 203
def dictionary_object=(object)
  raise ArgumentError, 'Argument object is not a Dictionary object' unless object.is_a? Dictionary

  unless dictionary_reference?
    raise ArgumentError,
      "The dictionary reference associated with key '#{key}' could not be found."
  end
  return if object.equal? dictionary_object

  if dictionary_exists?
    raise ArgumentError,
      "The dictionary is already loaded/cached for key '#{key}'; use #unload or #kill first."
  end

  dictionary_cache[DICTIONARY_CACHE][DICTIONARIES][dictionary_id!][DICTIONARY_OBJECT] = object
end
dictionary_object?() click to toggle source

This method returns true if the dictionary associated with the given dictionary key is loaded/cached. If this is the case, a dictionary object is available in the dictionary cache.

# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 182
def dictionary_object?
  dictionary_object.present?
end
Also aliased as: dictionary_exists?
dictionary_reference?() click to toggle source

Returns true if the dictionary reference exists for the given key; false otherwise. This method is only concerned with the dictionary reference and has nothing to do with whether or not the associated dictionary is actually loaded into the dictionary cache.

# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 120
def dictionary_reference?
  dictionary_reference&.present? || false
end
dictionary_source() click to toggle source
# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 174
def dictionary_source
  dictionary_cache.dig(DICTIONARY_CACHE, DICTIONARIES, dictionary_id, SOURCE)
end
Also aliased as: dictionary_file
dictionary_source!() click to toggle source
# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 167
def dictionary_source!
  raise ArgumentError, "A dictionary source could not be found for key '#{key}'." unless dictionary_reference?

  dictionary_cache[DICTIONARY_CACHE][DICTIONARIES][dictionary_id!][SOURCE]
end
Also aliased as: dictionary_file!
init() click to toggle source

This method resets the dictionary cache for the given key. This method is different from the .init class method in that ONLY the dictionary reference and dictionary specific to the given key is initialized.

# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 107
def init
  # TODO: Do not delete the dictionary if it is being pointed to by
  # another dictionary reference.
  dictionary_cache_hash = dictionary_cache[DICTIONARY_CACHE]
  dictionary_cache_hash[DICTIONARIES]&.delete(dictionary_id)
  dictionary_cache_hash[DICTIONARY_REFERENCES]&.delete(key)
  self
end

Private Instance Methods

dictionary_id?() click to toggle source
# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 257
def dictionary_id?
  dictionary_id.present?
end
dictionary_id_for_dictionary_source(source:) click to toggle source

Returns the dictionary_id for the source if it exists in dictionaries; otherwise, returns the new dictionary id that should be used.

# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 236
def dictionary_id_for_dictionary_source(source:)
  dictionary_source?(source: source) || SecureRandom.uuid[0..7]
end
dictionary_reference() click to toggle source
# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 224
def dictionary_reference
  dictionary_cache.dig(DICTIONARY_CACHE, DICTIONARY_REFERENCES, key)
end
dictionary_reference=(dictionary_id) click to toggle source
# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 228
def dictionary_reference=(dictionary_id)
  dictionary_cache[DICTIONARY_CACHE][DICTIONARY_REFERENCES][key] = {
    DICTIONARY_ID => dictionary_id
  }
end
dictionary_source=(source) click to toggle source
# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 250
def dictionary_source=(source)
  dictionary_cache[DICTIONARY_CACHE][DICTIONARIES][dictionary_id!] = {
    SOURCE => source,
    DICTIONARY_OBJECT => {}
  }
end
dictionary_source?(source:) click to toggle source

Returns the dictionary_id associated with source if source exists; nil otherwise.

# File lib/LittleWeasel/services/dictionary_cache_service.rb, line 242
def dictionary_source?(source:)
  dictionaries = dictionary_cache.dig(DICTIONARY_CACHE, DICTIONARIES)
  dictionaries&.each_pair do |dictionary_id, dictionary_hash|
    return dictionary_id if source == dictionary_hash[SOURCE]
  end
  nil
end