Class: Profane::Filter
- Inherits:
-
Object
- Object
- Profane::Filter
- Defined in:
- lib/profane/filter.rb
Instance Attribute Summary collapse
-
#character ⇒ Object
Returns the value of attribute character.
-
#dictionary ⇒ Object
Returns the value of attribute dictionary.
-
#dictionary_regex ⇒ Object
Returns the value of attribute dictionary_regex.
Instance Method Summary collapse
- #cleanse!(word) ⇒ Object
- #filter(phrase) ⇒ Object
-
#initialize ⇒ Filter
constructor
A new instance of Filter.
- #profane?(phrase) ⇒ Boolean
Constructor Details
#initialize ⇒ Filter
Returns a new instance of Filter.
5 6 7 8 |
# File 'lib/profane/filter.rb', line 5 def initialize @dictionary = Profane.dictionary @character = Profane.character end |
Instance Attribute Details
#character ⇒ Object
Returns the value of attribute character.
3 4 5 |
# File 'lib/profane/filter.rb', line 3 def character @character end |
#dictionary ⇒ Object
Returns the value of attribute dictionary.
3 4 5 |
# File 'lib/profane/filter.rb', line 3 def dictionary @dictionary end |
#dictionary_regex ⇒ Object
Returns the value of attribute dictionary_regex.
3 4 5 |
# File 'lib/profane/filter.rb', line 3 def dictionary_regex @dictionary_regex end |
Instance Method Details
#cleanse!(word) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/profane/filter.rb', line 30 def cleanse!(word) value = dictionary[word.match(/\w+/).to_s] return word unless value if value == '' word.gsub!(/\w/, character) else value end end |
#filter(phrase) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/profane/filter.rb', line 10 def filter(phrase) new_phrase = phrase.split(/\s/).map do |word| cleanse!(word) end Array(new_phrase).join(' ') end |
#profane?(phrase) ⇒ Boolean
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/profane/filter.rb', line 18 def profane?(phrase) return false unless phrase phrase = phrase.downcase.split(/\s+/) dictionary.keys.each do |key| return true unless phrase.grep(/(\b|\W)#{key.downcase}(\b|\W)/).empty? end false end |