class LittleWeasel::Preprocessors::WordPreprocessor

This is a base class that provides methods and functionality for 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.

Attributes

preprocessor_on[R]

Public Class Methods

new(order:) click to toggle source

order:Integer, the order in which this preprocessor should be applied. preprocessor_on:Boolean, whether or not this preprocessor should be applied to any words.

# File lib/LittleWeasel/preprocessors/word_preprocessor.rb, line 24
def initialize(order:)
  validate_order order: order
  self.order = order
  preprocessor_on!
end
preprocess(_word) click to toggle source

This method should UNconditionally apply preprocessing to word ONLY if word meets the criteria for preprocessing (.preprocess?).

This method should return the following Array:

<preprocessed?>, <preprocessed word | nil>

Where:

<preprocessed?> == whether or not the word was preprocessed
  based on whether or not the word meets the preprocessing
  criteria (.preprocess?).

<preprocessed word | nil> == the preprocessed word (if word
  met the preprocessing criteria (.preprocessed?)) or nil if
  word was NOT preprocessed (word did NOT meet the preprocessing
  criteria).
# File lib/LittleWeasel/preprocessors/word_preprocessor.rb, line 58
def preprocess(_word)
  raise Errors::MustOverrideError
end
preprocess?(_word) click to toggle source

Should return true if word matches the preprocess criteria; false, otherwise. If this preprocessor has no preprocess criteria, simply return true. This class method is unlike the instance method in that it does not consider whether or not this preprocessor is “on” or “off”; it simply returns true or false based on whether or not the word matches the preprocess criteria.

# File lib/LittleWeasel/preprocessors/word_preprocessor.rb, line 37
def preprocess?(_word)
  true
end

Public Instance Methods

preprocess(word) click to toggle source

Applies preprocessing to word if this preprocessor is “on” AND if word meets the criteria for preprocessing; no preprocessing is applied to word otherwise.

This method should return a Preprocessors::PreprocessedWord object.

# File lib/LittleWeasel/preprocessors/word_preprocessor.rb, line 84
def preprocess(word)
  preprocessed, preprocessed_word = if preprocessor_on?
    self.class.preprocess word
  else
    [false, nil]
  end
  preprocessed_word(original_word: word, preprocessed_word: preprocessed_word, preprocessed: preprocessed)
end
preprocess?(word) click to toggle source

Returns true if word meets the criteria for preprocessing. false is returned if word does not meet the criteria for preprocessing, or, if the preprocessor is “off”.

# File lib/LittleWeasel/preprocessors/word_preprocessor.rb, line 73
def preprocess?(word)
  return false if preprocessor_off?

  self.class.preprocess? word
end
preprocessor_off!() click to toggle source
# File lib/LittleWeasel/preprocessors/word_preprocessor.rb, line 110
def preprocessor_off!
  @preprocessor_on = false
end
preprocessor_off?() click to toggle source

Returns true if this preprocessor is “off”. Preprocessing should not be applied to a word if this preprocessor is “off”.

# File lib/LittleWeasel/preprocessors/word_preprocessor.rb, line 106
def preprocessor_off?
  !preprocessor_on?
end
preprocessor_on!() click to toggle source
# File lib/LittleWeasel/preprocessors/word_preprocessor.rb, line 100
def preprocessor_on!
  @preprocessor_on = true
end
preprocessor_on=(value) click to toggle source
# File lib/LittleWeasel/preprocessors/word_preprocessor.rb, line 63
def preprocessor_on=(value)
  raise ArgumentError, "Argument value is not true or false: #{value}" \
    unless [true, false].include? value

  @preprocessor_on = value
end
preprocessor_on?() click to toggle source

Returns true if this preprocessor is “on”; false, otherwise. If this preprocessor is “on”, preprocessing should be applied to a word if word meets the criteria for preprocessing.

# File lib/LittleWeasel/preprocessors/word_preprocessor.rb, line 96
def preprocessor_on?
  preprocessor_on
end

Private Instance Methods

preprocessed_word(original_word:, preprocessed:, preprocessed_word:) click to toggle source
# File lib/LittleWeasel/preprocessors/word_preprocessor.rb, line 116
def preprocessed_word(original_word:, preprocessed:, preprocessed_word:)
  PreprocessedWord.new(original_word: original_word, preprocessed: preprocessed,
    preprocessed_word: preprocessed_word, preprocessor: to_sym, preprocessor_order: order)
end