module LittleWeasel::Filters::WordFilterManagable

This module provides methods/functionality to manage word filters. Word fliters are processes through which words are passed; if the process returns true, the word should be considered valid and all subsequent filters ignored; if the process returns false, the word should be passed to the next filter, and so on. When using word filters, you need to consider whether or not metadata observers should be notified of the word now that it is considered “valid” although may not literally be a valid word in the dictionary.

Public Instance Methods

add_filters(word_filters: nil) { |word_filters| ... } click to toggle source

Appends word filters to the word_filters Array.

If Argument word_filter is nil, a block must be passed to populate the word_filters with an Array of valid word filter objects.

This method is used for adding/appending word filters to the word_filters Array. To replace word filters, use replace_filters; to perform any other manipulation of the word_filters Array, use word_filters directly.

# File lib/LittleWeasel/filters/word_filter_managable.rb, line 39
def add_filters(word_filters: nil)
  return if word_filters.is_a?(Array) && word_filters.blank?

  raise 'A block is required if argument word_filters is nil' if word_filters.nil? && !block_given?

  word_filters ||= []
  yield word_filters if block_given?

  validate_word_filters word_filters: word_filters

  self.word_filters.concat word_filters
end
Also aliased as: append_filters
append_filters(word_filters: nil)
Alias for: add_filters
clear_filters() click to toggle source
# File lib/LittleWeasel/filters/word_filter_managable.rb, line 26
def clear_filters
  self.word_filters = []
end
filter_match?(word) click to toggle source
# File lib/LittleWeasel/filters/word_filter_managable.rb, line 75
def filter_match?(word)
  filters_matched(word).present?
end
filters_matched(word) click to toggle source
# File lib/LittleWeasel/filters/word_filter_managable.rb, line 64
def filters_matched(word)
  raise ArgumentError, "Argument word is not a String: #{word.class}" unless word.is_a? String

  return [] if word_filters.blank?
  return [] if word.empty?

  word_filters.filter_map do |word_filter|
    word_filter.to_sym if word_filter.filter_match?(word)
  end
end
filters_on=(on) click to toggle source
# File lib/LittleWeasel/filters/word_filter_managable.rb, line 58
def filters_on=(on)
  raise ArgumentError, "Argument on is not true or false: #{on.class}" unless [true, false].include?(on)

  word_filters.each { |word_filter| word_filter.filter_on = on }
end
replace_filters(word_filters:) click to toggle source
# File lib/LittleWeasel/filters/word_filter_managable.rb, line 53
def replace_filters(word_filters:)
  clear_filters
  add_filters word_filters: word_filters
end
word_filters() click to toggle source

Override attr_reader word_filter found in WordFilterable so that we don't raise nil errors when using word_filters.

# File lib/LittleWeasel/filters/word_filter_managable.rb, line 22
def word_filters
  @word_filters ||= []
end