Class: Vangrail::Rails::Pattern

Inherits:
Vangrail::Rail show all
Defined in:
lib/vangrail/rails/pattern.rb

Overview

Blocks text matching any of a list of patterns.

A guardrail stack that is only a language model has no floor: when the endpoint is slow, cold, or down, every check degrades at once. A pattern rail decides in microseconds, offline, and identically every time, so the cases you can state exactly stay covered no matter what the network does.

Pattern.new(patterns: { 'instruction_override' => /ignore (all )?previous instructions/i })

Constant Summary

Constants inherited from Vangrail::Rail

Vangrail::Rail::DEFAULT_SIDES, Vangrail::Rail::SIDES

Instance Attribute Summary collapse

Attributes inherited from Vangrail::Rail

#name, #sides

Instance Method Summary collapse

Methods inherited from Vangrail::Rail

#applies_to?, #placeholder?, #to_s

Constructor Details

#initialize(patterns:, name: 'pattern', sides: Rail::SIDES, reason: nil) ⇒ Pattern

Returns a new instance of Pattern.



18
19
20
21
22
# File 'lib/vangrail/rails/pattern.rb', line 18

def initialize(patterns:, name: 'pattern', sides: Rail::SIDES, reason: nil)
  super(name: name, sides: sides)
  @patterns = normalize(patterns)
  @reason = reason
end

Instance Attribute Details

#patternsObject (readonly)

Returns the value of attribute patterns.



16
17
18
# File 'lib/vangrail/rails/pattern.rb', line 16

def patterns
  @patterns
end

Instance Method Details

#cache_key(text, _context) ⇒ Object



28
29
30
# File 'lib/vangrail/rails/pattern.rb', line 28

def cache_key(text, _context)
  text
end

#call(text, _context) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/vangrail/rails/pattern.rb', line 32

def call(text, _context)
  hit = patterns.find { |_label, pattern| pattern.match?(text.to_s) }
  return pass unless hit

  label = hit.first
  block(categories: [label], reason: @reason || "matched #{label}")
end

#offline?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/vangrail/rails/pattern.rb', line 24

def offline?
  true
end