Class: LittleWeasel::Preprocessors::WordPreprocessor

Inherits:
Object
  • Object
show all
Includes:
Modules::ClassNameToSymbol, Modules::Orderable
Defined in:
lib/LittleWeasel/preprocessors/word_preprocessor.rb

Overview

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. :reek:MissingSafeMethod, ignored - safe methods for preprocessor_on!/off! make no sense in this case

Direct Known Subclasses

EnUs::CapitalizePreprocessor

Instance Attribute Summary collapse

Attributes included from Modules::Orderable

#order

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Modules::OrderValidatable

#validate_order, validate_order

Methods included from Modules::ClassNameToSymbol

included, #to_sym

Constructor Details

#initialize(order:) ⇒ WordPreprocessor

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.



25
26
27
28
29
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 25

def initialize(order:)
  validate_order order: order
  self.order = order
  preprocessor_on!
end

Instance Attribute Details

#preprocessor_onObject

Returns the value of attribute preprocessor_on.



19
20
21
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 19

def preprocessor_on
  @preprocessor_on
end

Class Method Details

.preprocess(_word) ⇒ Object

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).


59
60
61
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 59

def preprocess(_word)
  raise Errors::MustOverrideError
end

.preprocess?(_word) ⇒ Boolean

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.

Returns:

  • (Boolean)


38
39
40
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 38

def preprocess?(_word)
  true
end

Instance Method Details

#preprocess(word) ⇒ Object

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.



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

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) ⇒ Boolean

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”.

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 74

def preprocess?(word)
  return false if preprocessor_off?

  self.class.preprocess? word
end

#preprocessed_word(original_word:, preprocessed:, preprocessed_word:) ⇒ Object (private)



117
118
119
120
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 117

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

#preprocessor_off!Object



111
112
113
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 111

def preprocessor_off!
  @preprocessor_on = false
end

#preprocessor_off?Boolean

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

Returns:

  • (Boolean)


107
108
109
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 107

def preprocessor_off?
  !preprocessor_on?
end

#preprocessor_on!Object



101
102
103
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 101

def preprocessor_on!
  @preprocessor_on = true
end

#preprocessor_on?Boolean

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.

Returns:

  • (Boolean)


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

def preprocessor_on?
  preprocessor_on
end