Class: Profane::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/profane/filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilter

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

#characterObject

Returns the value of attribute character.



3
4
5
# File 'lib/profane/filter.rb', line 3

def character
  @character
end

#dictionaryObject

Returns the value of attribute dictionary.



3
4
5
# File 'lib/profane/filter.rb', line 3

def dictionary
  @dictionary
end

#dictionary_regexObject

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

Returns:

  • (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