Class: LittleWeasel::Services::DictionaryCreatorService

Inherits:
Object
  • Object
show all
Includes:
Filters::WordFilterable, Filters::WordFiltersValidatable, Modules::DictionaryCacheServicable, Modules::DictionaryKeyable, Modules::DictionaryMetadataServicable, Modules::DictionarySourceable, Preprocessors::WordPreprocessorManagable
Defined in:
lib/LittleWeasel/services/dictionary_creator_service.rb

Overview

This class provides a service to load dictionaries from disk, create and return a Dictionary object.

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

Attributes included from Preprocessors::WordPreprocessable

#word_preprocessors

Attributes included from Modules::DictionaryMetadataServicable

#dictionary_cache, #dictionary_key, #dictionary_metadata

Attributes included from Modules::DictionaryKeyable

#dictionary_key

Attributes included from Modules::DictionaryCacheServicable

#dictionary_cache, #dictionary_key

Attributes included from Filters::WordFilterable

#word_filters

Instance Method Summary collapse

Methods included from Preprocessors::WordPreprocessorManagable

#add_preprocessors, #clear_preprocessors, #concat_and_sort_word_preprocessors!, #preprocess, #preprocessed_word, #preprocessed_words, #preprocessors_on=, #replace_preprocessors, #word_preprocessors

Methods included from Preprocessors::WordPreprocessorsValidatable

#validate_word_preprocessors, validate_word_preprocessors

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::DictionaryMetadataServicable

#dictionary_metadata_service

Methods included from Modules::DictionaryMetadataValidatable

#validate_dictionary_metadata, validate_dictionary_metadata

Methods included from Modules::DictionaryCacheValidatable

#validate_dictionary_cache, validate_dictionary_cache

Methods included from Modules::DictionaryKeyValidatable

#validate_dictionary_key, validate_dictionary_key

Methods included from Modules::DictionaryCacheServicable

#dictionary_cache_service

Methods included from Filters::WordFiltersValidatable

validate, #validate_word_filters

Methods included from Filters::WordFilterValidatable

valid_word_filter?, validate, #validate_word_filter

Constructor Details

#initialize(dictionary_key:, dictionary_cache:, dictionary_metadata:, word_filters: nil, word_preprocessors: nil) ⇒ DictionaryCreatorService

Returns a new instance of DictionaryCreatorService.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/LittleWeasel/services/dictionary_creator_service.rb', line 28

def initialize(dictionary_key:, dictionary_cache:, dictionary_metadata:,
  word_filters: nil, word_preprocessors: nil)
  validate_dictionary_key dictionary_key: dictionary_key
  self.dictionary_key = dictionary_key

  validate_dictionary_cache dictionary_cache: dictionary_cache
  self.dictionary_cache = dictionary_cache

   dictionary_metadata: 
  self. = 

  validate_word_filters word_filters: word_filters unless word_filters.blank?
  self.word_filters = word_filters

  validate_word_preprocessors word_preprocessors: word_preprocessors unless word_preprocessors.blank?
  self.word_preprocessors = word_preprocessors
end

Instance Method Details

#add_dictionary_file_source(file:) ⇒ Object (private)

Adds a dictionary file source. A “file source” is a file path that indicates that the dictionary words associated with this dictionary are located on disk. This file path is used to locate and load the dictionary words into the dictionary cache for use.

Parameters:

  • file (String)

    a file path pointing to the dictionary file to load and use.

Returns:

  • returns a reference to self.



79
80
81
# File 'lib/LittleWeasel/services/dictionary_creator_service.rb', line 79

def add_dictionary_file_source(file:)
  dictionary_cache_service.add_dictionary_source(dictionary_source: file)
end

#add_dictionary_memory_sourceObject (private)

Adds a dictionary memory source. A “memory source” indicates that the dictionary words associated with this dictionary were created dynamically and will be located in memory, as opposed to loaded from a file on disk.

Returns:

  • returns a reference to self.



89
90
91
# File 'lib/LittleWeasel/services/dictionary_creator_service.rb', line 89

def add_dictionary_memory_source
  dictionary_cache_service.add_dictionary_source(dictionary_source: memory_source)
end

#create_dictionary(dictionary_words:) ⇒ Object (private)



63
64
65
66
67
68
69
# File 'lib/LittleWeasel/services/dictionary_creator_service.rb', line 63

def create_dictionary(dictionary_words:)
  Dictionary.new(dictionary_key: dictionary_key, dictionary_cache: dictionary_cache,
    dictionary_metadata: , dictionary_words: dictionary_words, word_filters: word_filters,
    word_preprocessors: word_preprocessors).tap do |dictionary|
    dictionary_cache_service.dictionary_object = dictionary
  end
end

#dictionary_file_loader_serviceObject (private)



59
60
61
# File 'lib/LittleWeasel/services/dictionary_creator_service.rb', line 59

def dictionary_file_loader_service
  Services::DictionaryFileLoaderService.new dictionary_key: dictionary_key, dictionary_cache: dictionary_cache
end

#from_file_source(file:) ⇒ Object



46
47
48
49
50
# File 'lib/LittleWeasel/services/dictionary_creator_service.rb', line 46

def from_file_source(file:)
  add_dictionary_file_source file: file
  dictionary_words = dictionary_file_loader_service.execute
  create_dictionary dictionary_words: dictionary_words
end

#from_memory_source(dictionary_words:) ⇒ Object



52
53
54
55
# File 'lib/LittleWeasel/services/dictionary_creator_service.rb', line 52

def from_memory_source(dictionary_words:)
  add_dictionary_memory_source
  create_dictionary dictionary_words: dictionary_words
end