Class: NgWord::RuleList
- Inherits:
-
Object
- Object
- NgWord::RuleList
- Defined in:
- lib/ng_word/rule_list.rb
Instance Method Summary collapse
-
#initialize(rules) ⇒ RuleList
constructor
A new instance of RuleList.
- #masked_text(text, replace_text: '***', for_monitoring: false) ⇒ Object
- #match?(text) ⇒ Boolean
- #verify(text) ⇒ Object
Constructor Details
#initialize(rules) ⇒ RuleList
Returns a new instance of RuleList.
3 4 5 6 7 8 9 10 11 12 |
# 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 @regexp = Regexp.union(@rules.keys.map { |s| /#{s}/i }) freeze end |
Instance Method Details
#masked_text(text, replace_text: '***', for_monitoring: false) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ng_word/rule_list.rb', line 31 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
47 48 49 50 |
# File 'lib/ng_word/rule_list.rb', line 47 def match?(text) result = verify(text) !result.valid? end |
#verify(text) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ng_word/rule_list.rb', line 14 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 |