class LittleWeasel::Modules::DictionaryFileLoader::Loader

Helps with dictionary loading.

Attributes

config[RW]
dictionary_file_path[RW]
dictionary_words[RW]

Public Class Methods

new(dictionary_file_path, config) click to toggle source
# File lib/LittleWeasel/modules/dictionary_file_loader.rb, line 19
def initialize(dictionary_file_path, config)
  self.dictionary_file_path = dictionary_file_path
  self.config = config
end

Public Instance Methods

load() click to toggle source

Loads but DOES NOT update the dictionaries_hash. Use this if the dictionary DOES NOT need to hang around for any length of time.

# File lib/LittleWeasel/modules/dictionary_file_loader.rb, line 26
def load
  raise Errors::DictionaryFileNotFoundError unless file_exist?
  raise Errors::DictionaryFileEmptyError if file_empty?
  raise Errors::DictionaryFileTooLargeError if file_too_large?

  load_dictionary
end

Private Instance Methods

file_empty?() click to toggle source
# File lib/LittleWeasel/modules/dictionary_file_loader.rb, line 57
def file_empty?
  @file_empty ||= file_exist? && file_size.zero?
end
file_exist?() click to toggle source
# File lib/LittleWeasel/modules/dictionary_file_loader.rb, line 53
def file_exist?
  @file_exist ||= File.exist? dictionary_file_path
end
file_size() click to toggle source
# File lib/LittleWeasel/modules/dictionary_file_loader.rb, line 47
def file_size
  # File.size? returns nil if file_name doesn't exist or has zero size,
  # the size of the file otherwise.
  @file_size ||= File.size?(dictionary_file_path) || 0
end
file_too_large?() click to toggle source
# File lib/LittleWeasel/modules/dictionary_file_loader.rb, line 61
def file_too_large?
  @file_too_large ||= file_exist? && file_size > config.max_dictionary_file_bytes
end
load_dictionary() click to toggle source
# File lib/LittleWeasel/modules/dictionary_file_loader.rb, line 38
def load_dictionary
  prepare_dictionary(File.read(dictionary_file_path, mode: 'r')&.split)
end
prepare_dictionary(words) click to toggle source
# File lib/LittleWeasel/modules/dictionary_file_loader.rb, line 42
def prepare_dictionary(words)
  words&.uniq!&.compact!
  words if words.present?
end