Class: EncodedId::Blocklist

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/encoded_id/blocklist.rb

Overview

A blocklist of words that should not appear in encoded IDs.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(words = []) ⇒ Blocklist

Returns a new instance of Blocklist.



41
42
43
44
45
46
47
# File 'lib/encoded_id/blocklist.rb', line 41

def initialize(words = [])
  @words = if words.is_a?(Array) || words.is_a?(Set)
    Set.new(words.map(&:to_s).map(&:downcase))
  else
    Set.new
  end
end

Instance Attribute Details

#wordsObject (readonly)

: Set



38
39
40
# File 'lib/encoded_id/blocklist.rb', line 38

def words
  @words
end

Class Method Details

.emptyObject



21
22
23
# File 'lib/encoded_id/blocklist.rb', line 21

def empty
  @empty ||= new([])
end

.minimalObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/encoded_id/blocklist.rb', line 26

def minimal
  @minimal ||= new([
    "ass", "cum", "fag", "fap", "fck", "fuk", "jiz", "pis", "poo", "sex",
    "tit", "xxx", "anal", "anus", "ball", "blow", "butt", "clit", "cock",
    "coon", "cunt", "dick", "dyke", "fart", "fuck", "jerk", "jizz", "jugs",
    "kike", "kunt", "muff", "nigg", "nigr", "piss", "poon", "poop", "porn",
    "pube", "pusy", "quim", "rape", "scat", "scum", "shit", "slut", "suck",
    "turd", "twat", "vag", "wank", "whor"
  ])
end

.sqids_blocklistObject



16
17
18
# File 'lib/encoded_id/blocklist.rb', line 16

def sqids_blocklist
  new(::Sqids::DEFAULT_BLOCKLIST)
end

Instance Method Details

#blocks?(string) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
# File 'lib/encoded_id/blocklist.rb', line 60

def blocks?(string)
  return false if empty?

  downcased_string = string.to_s.downcase
  @words.each do |word|
    return word if downcased_string.include?(word)
  end
  false
end

#each(&block) ⇒ Object



50
51
52
# File 'lib/encoded_id/blocklist.rb', line 50

def each(&block)
  @words.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/encoded_id/blocklist.rb', line 76

def empty?
  @words.empty?
end

#filter_for_alphabet(alphabet) ⇒ Object

Filters the blocklist to only include words that can be formed from the given alphabet. Only keeps words where ALL characters exist in the alphabet (case-insensitive). Maintains minimum 3-character length requirement.



90
91
92
93
94
95
96
97
98
# File 'lib/encoded_id/blocklist.rb', line 90

def filter_for_alphabet(alphabet)
  alphabet_chars = Set.new(
    alphabet.is_a?(Alphabet) ? alphabet.unique_characters : alphabet.to_s.chars
  )

  self.class.new(
    @words.select { |word| word.length >= 3 && word.chars.to_set.subset?(alphabet_chars) }
  )
end

#include?(word) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/encoded_id/blocklist.rb', line 55

def include?(word)
  @words.include?(word.to_s.downcase)
end

#merge(other_blocklist) ⇒ Object



81
82
83
# File 'lib/encoded_id/blocklist.rb', line 81

def merge(other_blocklist)
  self.class.new(to_a + other_blocklist.to_a)
end

#sizeObject



71
72
73
# File 'lib/encoded_id/blocklist.rb', line 71

def size
  @words.size
end