Class: LittleWeasel::Modules::DictionaryFileLoader::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/LittleWeasel/modules/dictionary_file_loader.rb

Overview

Helps with dictionary loading.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dictionary_file_path, config) ⇒ Loader

Returns a new instance of Loader.



19
20
21
22
# 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

Instance Attribute Details

#configObject (private)

Returns the value of attribute config.



36
37
38
# File 'lib/LittleWeasel/modules/dictionary_file_loader.rb', line 36

def config
  @config
end

#dictionary_file_pathObject (private)

Returns the value of attribute dictionary_file_path.



36
37
38
# File 'lib/LittleWeasel/modules/dictionary_file_loader.rb', line 36

def dictionary_file_path
  @dictionary_file_path
end

#dictionary_wordsObject (private)

Returns the value of attribute dictionary_words.



36
37
38
# File 'lib/LittleWeasel/modules/dictionary_file_loader.rb', line 36

def dictionary_words
  @dictionary_words
end

Instance Method Details

#file_empty?Boolean (private)

Returns:

  • (Boolean)


57
58
59
# File 'lib/LittleWeasel/modules/dictionary_file_loader.rb', line 57

def file_empty?
  @file_empty ||= file_exist? && file_size.zero?
end

#file_exist?Boolean (private)

Returns:

  • (Boolean)


53
54
55
# File 'lib/LittleWeasel/modules/dictionary_file_loader.rb', line 53

def file_exist?
  @file_exist ||= File.exist? dictionary_file_path
end

#file_sizeObject (private)



47
48
49
50
51
# 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?Boolean (private)

Returns:

  • (Boolean)


61
62
63
# 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

#loadObject

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



26
27
28
29
30
31
32
# 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

#load_dictionaryObject (private)



38
39
40
# 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) ⇒ Object (private)



42
43
44
45
# File 'lib/LittleWeasel/modules/dictionary_file_loader.rb', line 42

def prepare_dictionary(words)
  words&.uniq!&.compact!
  words if words.present?
end