Class: Text::Gen::Filter::Censor

Inherits:
Base
  • Object
show all
Defined in:
lib/text/gen/filter/censor.rb

Overview

Censor looks up a different builder and rejects the result if the result text is included in the

Instance Method Summary collapse

Methods inherited from Base

#component_key, filter_key, filter_name, #initialize, #key, #to_s, #type, #value

Constructor Details

This class inherits a constructor from Text::Gen::Filter::Base

Instance Method Details

#result(context, result) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/text/gen/filter/censor.rb', line 9

def result(context, result)
  return result if @depth && context.depth != @depth

  return result if key.nil?

  builder = context.store.fetch(key)
  return result unless builder

  # Build a list of censor texts
  runner = Text::Gen::Runner.new(key:, lookup: nil)
  censor = Context.new(context.store)
  list = builder["items"].map do |item|
    runner.run_item(censor, item)&.text
  end.compact.uniq

  # Get the text to compare
  compare_text = apply_function(result.text)

  # Return nil if result text matches any censor text
  return if list.any? { |s| apply_function(s) == compare_text }

  result
end