Class: Phronomy::Guardrail::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/phronomy/guardrail/base.rb

Overview

Abstract base class for all guardrails.

Subclasses override #check to validate input or output. Call #fail! inside #check to reject with a reason.

Examples:

class NoPIIGuardrail < Phronomy::Guardrail::InputGuardrail
  def check(input)
    fail!("PII detected") if input.to_s.match?(/\d{3}-\d{2}-\d{4}/)
  end
end

Direct Known Subclasses

InputGuardrail, OutputGuardrail

Instance Method Summary collapse

Instance Method Details

#check(value) ⇒ Object

Validate the value. Subclasses must implement this method.

Parameters:

  • value (Object)

    the input or output being checked

Raises:



20
21
22
# File 'lib/phronomy/guardrail/base.rb', line 20

def check(value)
  raise NotImplementedError, "#{self.class}#check is not implemented"
end

#run!(value) ⇒ Object

Run the check, raising GuardrailError on failure.

Parameters:

  • value (Object)

Returns:

  • (Object)

    the original value (unchanged) when the check passes



27
28
29
30
# File 'lib/phronomy/guardrail/base.rb', line 27

def run!(value)
  check(value)
  value
end