Module: LittleWeasel::Preprocessors::WordPreprocessorManagable

Includes:
WordPreprocessable, WordPreprocessorsValidatable
Included in:
Dictionary, Services::DictionaryCreatorService
Defined in:
lib/LittleWeasel/preprocessors/word_preprocessor_managable.rb

Overview

This module provides methods and functionality to manage word preprocessors. A “word preprocessor” is an object that manipulates a word before it is passed to any word filters and before it is compared against the dictionary for validity.

When creating your own word preprocessors, here are some things you need to consider:

Multiple word preprocessors can be applied to a given word. Word processors will be applied to a word in Preprocessors::WordPreprocessor#order order (ascending). Even though this is the case, it doesn’t mean you should seek to apply more than one word preprocessor at a time. However, if you do, write and order your word preprocessors in such a way that each preprocessor manipulates the word in a complimentary rather than contridictionary way. For example, applying one word preprocessor that convert a word to uppercase and a second that converts the word to lowercase, contradict each other.

Another thing you need to consider, is whether or not metadata observers should be notified of the preprocessed word (now that it has been potentially manipulated) or if they should be notified of the original word; this is because, the original word may not be found as a valid word in the dictionary, while the preprocessed word might and vise versa.

Instance Method Summary collapse

Methods included from WordPreprocessorsValidatable

#validate_word_preprocessors, validate_word_preprocessors

Instance Method Details

#add_preprocessors(word_preprocessors: nil) {|word_preprocessors| ... } ⇒ Object Also known as: append_preprocessors

Appends word preprocessors to the #word_preprocessors Array.

If Argument word_preprocessor is nil, a block must be passed to populate the word_preprocessors with an Array of valid word preprocessor objects.

This method is used for adding/appending word preprocessors to the word_preprocessors Array. To replace word preprocessors, use #replace_preprocessors; to perform any other manipulation of the word_preprocessors Array, use #word_preprocessors directly.

Yields:



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/LittleWeasel/preprocessors/word_preprocessor_managable.rb', line 54

def add_preprocessors(word_preprocessors: nil)
  return if word_preprocessors.is_a?(Array) && word_preprocessors.blank?

  unless word_preprocessors.present? || block_given?
    raise 'A block is required if argument word_preprocessors is nil'
  end

  word_preprocessors ||= []
  yield word_preprocessors if block_given?

  concat_and_sort_word_preprocessors! word_preprocessors
end

#clear_preprocessorsObject



41
42
43
# File 'lib/LittleWeasel/preprocessors/word_preprocessor_managable.rb', line 41

def clear_preprocessors
  self.word_preprocessors = []
end

#concat_and_sort_word_preprocessors!(preprocessors) ⇒ Object (private)

This method concatinates preprocessors to #word_preprocessors, sorts #word_preprocessors by WordPreprocessor#order and returns the results.



106
107
108
109
110
111
# File 'lib/LittleWeasel/preprocessors/word_preprocessor_managable.rb', line 106

def concat_and_sort_word_preprocessors!(preprocessors)
  validate_word_preprocessors word_preprocessors: preprocessors

  word_preprocessors.concat preprocessors
  word_preprocessors.sort_by!(&:order)
end

#preprocess(word:) ⇒ Object

Returns a Preprocessors::PreprocessedWords object.



80
81
82
83
# File 'lib/LittleWeasel/preprocessors/word_preprocessor_managable.rb', line 80

def preprocess(word:)
  preprocessed_words = preprocessed_words word: word
  PreprocessedWords.new(original_word: word, preprocessed_words: preprocessed_words)
end

#preprocessed_word(word:) ⇒ Object

Returns the final (or last) preprocessed word in the Array of preprocessed words. The final preprocessed word is the word that has passed through all the word preprocessors.



96
97
98
99
# File 'lib/LittleWeasel/preprocessors/word_preprocessor_managable.rb', line 96

def preprocessed_word(word:)
  preprocessed_words = self.preprocessed_words word: word
  preprocessed_words.max_by(&:preprocessor_order).preprocessed_word unless preprocessed_words.blank?
end

#preprocessed_words(word:) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/LittleWeasel/preprocessors/word_preprocessor_managable.rb', line 85

def preprocessed_words(word:)
  word_preprocessors.map do |word_preprocessor|
    word_preprocessor.preprocess(word).tap do |processed_word|
      word = processed_word.preprocessed_word
    end
  end
end

#preprocessors_on=(on) ⇒ Object

Raises:

  • (ArgumentError)


73
74
75
76
77
# File 'lib/LittleWeasel/preprocessors/word_preprocessor_managable.rb', line 73

def preprocessors_on=(on)
  raise ArgumentError, "Argument on is not true or false: #{on.class}" unless [true, false].include?(on)

  word_preprocessors.each { |word_preprocessor| word_preprocessor.preprocessor_on = on }
end

#replace_preprocessors(word_preprocessors:) ⇒ Object



68
69
70
71
# File 'lib/LittleWeasel/preprocessors/word_preprocessor_managable.rb', line 68

def replace_preprocessors(word_preprocessors:)
  clear_preprocessors
  add_preprocessors word_preprocessors: word_preprocessors
end

#word_preprocessorsObject

Override attr_reader word_preprocessor found in WordPreprocessable so that we don’t raise nil errors when using word_preprocessors.



37
38
39
# File 'lib/LittleWeasel/preprocessors/word_preprocessor_managable.rb', line 37

def word_preprocessors
  @word_preprocessors ||= []
end