Class: NgWord::RuleList

Inherits:
Object
  • Object
show all
Defined in:
lib/ng_word/rule_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(rules) ⇒ RuleList

Returns a new instance of RuleList.



3
4
5
6
7
8
9
10
11
# File 'lib/ng_word/rule_list.rb', line 3

def initialize(rules)
  @rules = {}
  Array(rules).each do |rule|
    @rules[rule.ng_word.downcase] = rule
  end

  @ng_words = @rules.keys
  @regexp = Regexp.union(@rules.keys.map { |s| /#{s}/i })
end

Instance Method Details

#masked_text(text, replace_text: '***', for_monitoring: false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ng_word/rule_list.rb', line 30

def masked_text(text, replace_text: '***', for_monitoring: false)
  return text if text.blank?

  candidate_ng_words = text.scan(@regexp)
  return text if candidate_ng_words.blank?

  candidate_ng_words.each do |ng_word|
    rule = @rules[ng_word.downcase]
    next if rule.blank?

    text = rule.masked_text(text, replace_text: replace_text, for_monitoring: for_monitoring)
  end

  text
end

#match?(text) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/ng_word/rule_list.rb', line 46

def match?(text)
  result = verify(text)
  !result.valid?
end

#verify(text) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ng_word/rule_list.rb', line 13

def verify(text)
  return VerificationResult.new(true) if text.blank?

  candidate_ng_words = text.scan(@regexp)
  return VerificationResult.new(true) if candidate_ng_words.blank?

  downcased_text = text.downcase
  candidate_ng_words.each do |ng_word|
    rule = @rules[ng_word.downcase]
    next if rule.blank?

    return VerificationResult.new(false, ng_word: rule.ng_word) if rule.match?(downcased_text)
  end

  VerificationResult.new(true)
end