Class: LittleWeasel::Modules::DictionaryFileLoader::Loader
- Inherits:
-
Object
- Object
- LittleWeasel::Modules::DictionaryFileLoader::Loader
- Defined in:
- lib/LittleWeasel/modules/dictionary_file_loader.rb
Overview
Helps with dictionary loading.
Instance Attribute Summary collapse
-
#config ⇒ Object
private
Returns the value of attribute config.
-
#dictionary_file_path ⇒ Object
private
Returns the value of attribute dictionary_file_path.
-
#dictionary_words ⇒ Object
private
Returns the value of attribute dictionary_words.
Instance Method Summary collapse
- #file_empty? ⇒ Boolean private
- #file_exist? ⇒ Boolean private
- #file_size ⇒ Object private
- #file_too_large? ⇒ Boolean private
-
#initialize(dictionary_file_path, config) ⇒ Loader
constructor
A new instance of Loader.
-
#load ⇒ Object
Loads but DOES NOT update the dictionaries_hash.
- #load_dictionary ⇒ Object private
- #prepare_dictionary(words) ⇒ Object private
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
#config ⇒ Object (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_path ⇒ Object (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_words ⇒ Object (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)
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)
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_size ⇒ Object (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)
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 |
#load ⇒ Object
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_dictionary ⇒ Object (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 |