Class: BlackList

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/black_list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBlackList

Loads blacklist words from black_list.yml and removes any nested greedy words. This is always called implicitly due to the fact that BlackList is a Singleton. As such, there is only ever one instance.



50
51
52
53
# File 'lib/black_list.rb', line 50

def initialize #:nodoc:
  load_words!
  trim_greedy_words!
end

Instance Attribute Details

#exactObject

Returns the value of attribute exact.



36
37
38
# File 'lib/black_list.rb', line 36

def exact
  @exact
end

#greedyObject

Returns the value of attribute greedy.



36
37
38
# File 'lib/black_list.rb', line 36

def greedy
  @greedy
end

Class Method Details

.method_missing(method, *args) ⇒ Object

Redirects all method calls made directly on BlackList to BlackList.instance. For example:

BlackList.greedy?("foo") => BlackList.instance.greedy?("foo")


42
43
44
# File 'lib/black_list.rb', line 42

def self.method_missing(method, *args) #:nodoc:
  self.instance.send(method, *args) unless method == :instance
end

Instance Method Details

#block?(text) ⇒ Boolean

Check the supplied text to see whether it contains a blacklisted word and should be blocked.

Returns:

  • (Boolean)


57
58
59
# File 'lib/black_list.rb', line 57

def block?(text)
  greedy?(text) or exact?(text)
end

#exact?(text) ⇒ Boolean

Check the supplied text to see whether it contains an exact blacklisted word.

Returns:

  • (Boolean)


63
64
65
# File 'lib/black_list.rb', line 63

def exact?(text)
  check(text, :exact, @exact)
end

#greedy?(text) ⇒ Boolean

Check the supplied text to see whether it contains a greedy blacklisted word.

Returns:

  • (Boolean)


69
70
71
# File 'lib/black_list.rb', line 69

def greedy?(text)
  check(text, :greedy, @greedy)
end

#highlight(text) ⇒ Object

Get the supplied text in HTML format with any blacklisted words bolded. Text is passed through a Textile markup processor (RedCloth).



76
77
78
79
80
81
82
83
# File 'lib/black_list.rb', line 76

def highlight(text)
  return text if text !~ /\S/
  
  text = highlight_words!(text, :exact, @exact)
  text = highlight_words!(text, :greedy, @greedy)
  
  RedCloth.new(text).to_html
end