Class: TopSecret::FilteredText
- Inherits:
-
Object
- Object
- TopSecret::FilteredText
- Defined in:
- lib/top_secret/filtered_text.rb,
lib/top_secret/filtered_text/result.rb
Overview
Restores filtered text by substituting placeholders with original values.
This class is used to reverse the filtering process, typically when processing responses from external services like LLMs that may contain filtered placeholders.
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#output ⇒ String
readonly
The text being processed for restoration.
Class Method Summary collapse
-
.restore(filtered_text, mapping:) ⇒ Result
Convenience method to restore filtered text in one call.
Instance Method Summary collapse
-
#initialize(filtered_text, mapping:) ⇒ FilteredText
constructor
A new instance of FilteredText.
-
#restore ⇒ Result
Performs the restoration process.
Constructor Details
#initialize(filtered_text, mapping:) ⇒ FilteredText
Returns a new instance of FilteredText.
16 17 18 19 |
# File 'lib/top_secret/filtered_text.rb', line 16 def initialize(filtered_text, mapping:) @mapping = mapping @output = filtered_text.dup end |
Instance Attribute Details
#output ⇒ String (readonly)
Returns The text being processed for restoration.
12 13 14 |
# File 'lib/top_secret/filtered_text.rb', line 12 def output @output end |
Class Method Details
.restore(filtered_text, mapping:) ⇒ Result
Convenience method to restore filtered text in one call
33 34 35 |
# File 'lib/top_secret/filtered_text.rb', line 33 def self.restore(filtered_text, mapping:) new(filtered_text, mapping:).restore end |
Instance Method Details
#restore ⇒ Result
Performs the restoration process
Substitutes all found placeholders with their mapped values and tracks which placeholders were successfully restored vs those that remain unrestored.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/top_secret/filtered_text.rb', line 43 def restore restored = [] mapping.each do |filter, value| placeholder = build_placeholder(filter) if output.include? placeholder restored << placeholder output.gsub! placeholder, value end end unrestored = output.scan(/\[\w*_\d\]/) Result.new(output, unrestored, restored) end |