Class: NgWord::Rule
- Inherits:
-
Object
- Object
- NgWord::Rule
- Defined in:
- lib/ng_word/rule.rb
Instance Attribute Summary collapse
-
#exclude_word_rules ⇒ Object
readonly
Returns the value of attribute exclude_word_rules.
-
#ng_word ⇒ Object
readonly
Returns the value of attribute ng_word.
Instance Method Summary collapse
-
#initialize(ng_word, exclude_words: []) ⇒ Rule
constructor
A new instance of Rule.
- #masked_text(text, replace_text: '***', for_monitoring: false) ⇒ Object
- #match?(downcased_text) ⇒ Boolean
- #verify(downcased_text, offset: 0) ⇒ Object
Constructor Details
permalink #initialize(ng_word, exclude_words: []) ⇒ Rule
Returns a new instance of Rule.
5 6 7 8 9 10 11 12 13 |
# File 'lib/ng_word/rule.rb', line 5 def initialize(ng_word, exclude_words: []) @ng_word = ng_word @exclude_words = exclude_words @downcased_ng_word = @ng_word.downcase @exclude_word_rules = Array(@exclude_words).map { |w| ExcludeWordRule.new(@downcased_ng_word, w) } freeze end |
Instance Attribute Details
permalink #exclude_word_rules ⇒ Object (readonly)
Returns the value of attribute exclude_word_rules.
3 4 5 |
# File 'lib/ng_word/rule.rb', line 3 def exclude_word_rules @exclude_word_rules end |
permalink #ng_word ⇒ Object (readonly)
Returns the value of attribute ng_word.
3 4 5 |
# File 'lib/ng_word/rule.rb', line 3 def ng_word @ng_word end |
Instance Method Details
permalink #masked_text(text, replace_text: '***', for_monitoring: false) ⇒ Object
[View source]
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ng_word/rule.rb', line 26 def masked_text(text, replace_text: '***', for_monitoring: false) downcased_text = text.downcase index = downcased_text.index(downcased_ng_word) return text if index.nil? index_list = split(downcased_text) words = [] current_index = 0 index_list.each do |index| words << text.slice(current_index, index - current_index) current_index = index + downcased_ng_word.length end words << text.slice(current_index, text.length - current_index) replaced_text = for_monitoring ? "#{replace_text}(#{ng_word})" : replace_text words.join(replaced_text) end |
permalink #match?(downcased_text) ⇒ Boolean
45 46 47 48 |
# File 'lib/ng_word/rule.rb', line 45 def match?(downcased_text) result = verify(downcased_text) !result.valid? end |
permalink #verify(downcased_text, offset: 0) ⇒ Object
[View source]
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ng_word/rule.rb', line 15 def verify(downcased_text, offset: 0) index = downcased_text.index(downcased_ng_word, offset) return VerificationResult.new(true) if index.nil? exclude_word_rules.each do |rule| return verify(downcased_text, offset: index + 1) if rule.match?(downcased_text, index) end VerificationResult.new(false, ng_word: ng_word) end |