class LittleWeasel::Services::DictionaryCreatorService

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

Public Class Methods

new(dictionary_key:, dictionary_cache:, dictionary_metadata:, word_filters: nil, word_preprocessors: nil) click to toggle source
# 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

  validate_dictionary_metadata dictionary_metadata: dictionary_metadata
  self.dictionary_metadata = dictionary_metadata

  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

Public Instance Methods

from_file_source(file:) click to toggle source
# 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:) click to toggle source
# 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

Private Instance Methods

add_dictionary_file_source(file:) click to toggle source

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.

@param file [String] a file path pointing to the dictionary file to load and use.

@return returns a reference to self.

# File lib/LittleWeasel/services/dictionary_creator_service.rb, line 79
def add_dictionary_file_source(file:)
  dictionary_cache_service.add_dictionary_source(source: file)
end
add_dictionary_memory_source() click to toggle source

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.

@return returns a reference to self.

# File lib/LittleWeasel/services/dictionary_creator_service.rb, line 89
def add_dictionary_memory_source
  dictionary_cache_service.add_dictionary_source(source: memory_source)
end
create_dictionary(dictionary_words:) click to toggle source
# 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_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_service() click to toggle source
# 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