Class: LittleWeasel::DictionaryManager

Inherits:
Object
  • Object
show all
Includes:
Modules::DictionaryKeyValidatable
Defined in:
lib/LittleWeasel/dictionary_manager.rb

Overview

This class provides dictionary management functionality.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Modules::DictionaryKeyValidatable

validate, #validate_dictionary_key

Constructor Details

#initializeDictionaryManager

Returns a new instance of DictionaryManager.



14
15
16
17
18
# File 'lib/LittleWeasel/dictionary_manager.rb', line 14

def initialize
  self.dictionary_cache = {}
  self. = {}
  init
end

Instance Attribute Details

#dictionary_cacheObject

Returns the value of attribute dictionary_cache.



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

def dictionary_cache
  @dictionary_cache
end

#dictionary_metadataObject

Returns the value of attribute dictionary_metadata.



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

def 
  @dictionary_metadata
end

Instance Method Details

#create_dictionary_from_file(dictionary_key:, file:, word_filters: nil, word_preprocessors: nil) ⇒ Object

Adds a dictionary file source, creates the dictionary and returns the Dictionary object.



32
33
34
35
36
37
# File 'lib/LittleWeasel/dictionary_manager.rb', line 32

def create_dictionary_from_file(dictionary_key:, file:, word_filters: nil, word_preprocessors: nil)
  validate_dictionary_key dictionary_key: dictionary_key

  dictionary_creator_service(dictionary_key: dictionary_key, word_filters: word_filters,
    word_preprocessors: word_preprocessors).from_file_source file: file
end

#create_dictionary_from_memory(dictionary_key:, dictionary_words:, word_filters: nil, word_preprocessors: nil) ⇒ Object

Adds a dictionary memory source, creates the dictionary and returns the Dictionary object.



41
42
43
44
45
46
# File 'lib/LittleWeasel/dictionary_manager.rb', line 41

def create_dictionary_from_memory(dictionary_key:, dictionary_words:, word_filters: nil, word_preprocessors: nil)
  validate_dictionary_key dictionary_key: dictionary_key

  dictionary_creator_service(dictionary_key: dictionary_key, word_filters: word_filters,
    word_preprocessors: word_preprocessors).from_memory_source dictionary_words: dictionary_words
end

#dictionary_cache_service(dictionary_key:) ⇒ Object (private)



70
71
72
# File 'lib/LittleWeasel/dictionary_manager.rb', line 70

def dictionary_cache_service(dictionary_key:)
  Services::DictionaryCacheService.new dictionary_key: dictionary_key, dictionary_cache: dictionary_cache
end

#dictionary_creator_service(dictionary_key:, word_filters:, word_preprocessors:) ⇒ Object (private)



74
75
76
77
78
# File 'lib/LittleWeasel/dictionary_manager.rb', line 74

def dictionary_creator_service(dictionary_key:, word_filters:, word_preprocessors:)
  Services::DictionaryCreatorService.new dictionary_key: dictionary_key, dictionary_cache: dictionary_cache,
    dictionary_metadata: , word_filters: word_filters,
    word_preprocessors: word_preprocessors
end

#dictionary_for(dictionary_key:) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/LittleWeasel/dictionary_manager.rb', line 20

def dictionary_for(dictionary_key:)
  validate_dictionary_key dictionary_key: dictionary_key

  unless dictionary_cache_service(dictionary_key: dictionary_key).dictionary_exists?
    # TODO: Raise an error or let the service handle it?
  end

  dictionary_cache_service(dictionary_key: dictionary_key).dictionary_object!
end

#dictionary_killer_service(dictionary_key:) ⇒ Object (private)



80
81
82
83
# File 'lib/LittleWeasel/dictionary_manager.rb', line 80

def dictionary_killer_service(dictionary_key:)
  Services::DictionaryKillerService.new dictionary_key: dictionary_key, dictionary_cache: dictionary_cache,
    dictionary_metadata: 
end

#initObject

Resets the cache and metadata by clearing it out completely.



60
61
62
63
64
# File 'lib/LittleWeasel/dictionary_manager.rb', line 60

def init
  Services::DictionaryCacheService.init dictionary_cache: dictionary_cache
  Services::DictionaryMetadataService.init dictionary_metadata: 
  self
end

#kill_dictionary(dictionary_key:) ⇒ Object

Removes any and all traces of the dictionary associated with the dictionary key from the dictionary cache - the Dictionary object, file reference and any metadata associated with the dictionary are completely removed from the dictionary cache.



52
53
54
55
56
57
# File 'lib/LittleWeasel/dictionary_manager.rb', line 52

def kill_dictionary(dictionary_key:)
  validate_dictionary_key dictionary_key: dictionary_key

  dictionary_killer_service(dictionary_key: dictionary_key).execute
  self
end