Class: LittleWeasel::Services::DictionaryCacheService
- Inherits:
-
Object
- Object
- LittleWeasel::Services::DictionaryCacheService
- Includes:
- Modules::DictionaryCacheKeys, Modules::DictionaryCacheValidatable, Modules::DictionaryKeyable, Modules::DictionarySourceable
- Defined in:
- lib/LittleWeasel/services/dictionary_cache_service.rb
Overview
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.
Constant Summary
Constants included from Modules::DictionarySourceable
Modules::DictionarySourceable::MEMORY_SOURCE
Constants included from Modules::DictionaryCacheKeys
Modules::DictionaryCacheKeys::DICTIONARIES, Modules::DictionaryCacheKeys::DICTIONARY_CACHE, Modules::DictionaryCacheKeys::DICTIONARY_ID, Modules::DictionaryCacheKeys::DICTIONARY_OBJECT, Modules::DictionaryCacheKeys::DICTIONARY_REFERENCES, Modules::DictionaryCacheKeys::SOURCE
Instance Attribute Summary collapse
-
#dictionary_cache ⇒ Object
Returns the value of attribute dictionary_cache.
Attributes included from Modules::DictionaryKeyable
Class Method Summary collapse
-
.count(dictionary_cache:) ⇒ Object
Returns the number of dictionaries currently in the cache.
-
.init(dictionary_cache:) ⇒ Object
This method resets dictionary_cache to its initialized state.
-
.init?(dictionary_cache:) ⇒ Boolean
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.
Instance Method Summary collapse
-
#dictionary? ⇒ Boolean
Returns true if a dictionaries Hash key exists for the given dictionary_id in the dictionary cache.
-
#dictionary_exists? ⇒ Boolean
DEPRECATED: Use
dictionary_exist?
instead. -
#dictionary_id ⇒ Object
Returns the dictionary id if there is a dictionary id in the dictionary cache associated with the given key; nil otherwise.
-
#dictionary_id! ⇒ Object
Returns the dictionary id if there is a dictionary id in the dictionary cache associated with the given key.
- #dictionary_id? ⇒ Boolean private
- #dictionary_object ⇒ Object
-
#dictionary_object! ⇒ Object
Returns the dictionary object from the dictionary cache for the given key.
- #dictionary_object=(object) ⇒ Object
-
#dictionary_object? ⇒ Boolean
(also: #dictionary_exist?)
This method returns true if the dictionary associated with the given dictionary key is loaded/cached.
- #dictionary_reference ⇒ Object private
-
#dictionary_reference? ⇒ Boolean
Returns true if the dictionary reference exists for the given key; false otherwise.
-
#init ⇒ Object
This method resets the dictionary cache for the given key.
-
#initialize(dictionary_key:, dictionary_cache:) ⇒ DictionaryCacheService
constructor
This class produces the following (example) Hash that represents the dictionary cache structure:.
- #set_dictionary_reference(dictionary_id:) ⇒ Object private
Methods included from Modules::DictionarySourceable
#add_dictionary_source, #dictionary_id_associated_with, #dictionary_source, #dictionary_source!, file_source?, memory_source, #memory_source, memory_source?, #set_dictionary_source
Methods included from Modules::DictionaryValidatable
#validate_dictionary_source_does_not_exist, validate_dictionary_source_does_not_exist
Methods included from Modules::DictionarySourceValidatable
#validate_dictionary_source, validate_dictionary_source
Methods included from Modules::DictionaryCacheKeys
#initialize_dictionary_cache, initialize_dictionary_cache, #initialized_dictionary_cache, initialized_dictionary_cache
Methods included from Modules::DictionaryKeyValidatable
#validate_dictionary_key, validate_dictionary_key
Methods included from Modules::DictionaryCacheValidatable
#validate_dictionary_cache, validate_dictionary_cache
Constructor Details
#initialize(dictionary_key:, dictionary_cache:) ⇒ DictionaryCacheService
This class produces the following (example) Hash that represents the dictionary cache structure:
{
'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' => {}
}
}
}
}
69 70 71 72 73 74 75 76 77 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 69 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 |
Instance Attribute Details
#dictionary_cache ⇒ Object
Returns the value of attribute dictionary_cache.
24 25 26 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 24 def dictionary_cache @dictionary_cache end |
Class Method Details
.count(dictionary_cache:) ⇒ Object
Returns the number of dictionaries currently in the cache.
97 98 99 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 97 def count(dictionary_cache:) dictionary_cache.dig(self::DICTIONARY_CACHE, self::DICTIONARIES)&.keys&.count || 0 end |
.init(dictionary_cache:) ⇒ Object
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.
84 85 86 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 84 def init(dictionary_cache:) Modules::DictionaryCacheKeys.initialize_dictionary_cache dictionary_cache: dictionary_cache end |
.init?(dictionary_cache:) ⇒ Boolean
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.
91 92 93 94 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 91 def init?(dictionary_cache:) initialized_dictionary_cache = init(dictionary_cache: {}) dictionary_cache.eql?(initialized_dictionary_cache) end |
Instance Method Details
#dictionary? ⇒ Boolean
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.
126 127 128 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 126 def dictionary? dictionary_cache[DICTIONARY_CACHE][DICTIONARIES].key? dictionary_id end |
#dictionary_exists? ⇒ Boolean
DEPRECATED: Use dictionary_exist?
instead.
154 155 156 157 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 154 def dictionary_exists? warn "[DEPRECATION] 'dictionary_exists?' is deprecated. Please use 'dictionary_exist?' instead." dictionary_object? end |
#dictionary_id ⇒ Object
Returns the dictionary id if there is a dictionary id in the dictionary cache associated with the given key; nil otherwise.
132 133 134 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 132 def dictionary_id dictionary_cache.dig(DICTIONARY_CACHE, DICTIONARY_REFERENCES, key, DICTIONARY_ID) end |
#dictionary_id! ⇒ Object
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.
139 140 141 142 143 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 139 def dictionary_id! return dictionary_id if dictionary_id? raise ArgumentError, "A dictionary id could not be found for key '#{key}'." end |
#dictionary_id? ⇒ Boolean (private)
206 207 208 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 206 def dictionary_id? dictionary_id.present? end |
#dictionary_object ⇒ Object
171 172 173 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 171 def dictionary_object dictionary_cache.dig(DICTIONARY_CACHE, DICTIONARIES, dictionary_id, DICTIONARY_OBJECT) end |
#dictionary_object! ⇒ Object
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.
162 163 164 165 166 167 168 169 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 162 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) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 175 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_exist? 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? ⇒ Boolean Also known as: dictionary_exist?
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.
148 149 150 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 148 def dictionary_object? dictionary_object.present? end |
#dictionary_reference ⇒ Object (private)
196 197 198 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 196 def dictionary_reference dictionary_cache.dig(DICTIONARY_CACHE, DICTIONARY_REFERENCES, key) end |
#dictionary_reference? ⇒ Boolean
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.
118 119 120 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 118 def dictionary_reference? dictionary_reference&.present? || false end |
#init ⇒ Object
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.
105 106 107 108 109 110 111 112 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 105 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 |
#set_dictionary_reference(dictionary_id:) ⇒ Object (private)
200 201 202 203 204 |
# File 'lib/LittleWeasel/services/dictionary_cache_service.rb', line 200 def set_dictionary_reference(dictionary_id:) dictionary_cache[DICTIONARY_CACHE][DICTIONARY_REFERENCES][key] = { DICTIONARY_ID => dictionary_id } end |