Module: LittleWeasel::Filters::WordFilterManagable

Includes:
WordFilterable, WordFiltersValidatable
Included in:
Dictionary
Defined in:
lib/LittleWeasel/filters/word_filter_managable.rb

Overview

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.

Instance Method Summary collapse

Methods included from WordFiltersValidatable

validate, #validate_word_filters

Methods included from WordFilterValidatable

valid_word_filter?, validate, #validate_word_filter

Instance Method Details

#add_filters(word_filters: nil) {|word_filters| ... } ⇒ Object Also known as: append_filters

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.

Yields:



39
40
41
42
43
44
45
46
47
48
49
50
# 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

#clear_filtersObject



26
27
28
# File 'lib/LittleWeasel/filters/word_filter_managable.rb', line 26

def clear_filters
  self.word_filters = []
end

#filter_match?(word) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/LittleWeasel/filters/word_filter_managable.rb', line 75

def filter_match?(word)
  filters_matched(word).present?
end

#filters_matched(word) ⇒ Object

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
62
# 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:) ⇒ Object



53
54
55
56
# 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_filtersObject

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



22
23
24
# File 'lib/LittleWeasel/filters/word_filter_managable.rb', line 22

def word_filters
  @word_filters ||= []
end