Class: Riffer::Guardrails::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/riffer/guardrails/result.rb

Overview

Represents the result of a guardrail execution.

Results can be one of three types:

  • pass: Continue with the original data unchanged

  • transform: Continue with transformed data

  • block: Halt execution with a reason

Use the factory methods to create results:

Result.pass(data)
Result.transform(data)
Result.block(reason, metadata: nil)

Constant Summary collapse

TYPES =

: Array

%i[pass transform block].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, data, metadata: nil) ⇒ Result

Creates a new result.

type

the result type (:pass, :transform, or :block).

data

the data or reason.

metadata

optional metadata for block results.

Raises Riffer::ArgumentError if the result type is invalid.

– : (Symbol, untyped, ?metadata: Hash[Symbol, untyped]?) -> void



71
72
73
74
75
76
77
# File 'lib/riffer/guardrails/result.rb', line 71

def initialize(type, data, metadata: nil)
  raise Riffer::ArgumentError, "Invalid result type: #{type}" unless TYPES.include?(type)

  @type = type
  @data = data
  @metadata = 
end

Instance Attribute Details

#dataObject (readonly)

The data (for pass/transform) or reason (for block).



23
24
25
# File 'lib/riffer/guardrails/result.rb', line 23

def data
  @data
end

#metadataObject (readonly)

Optional metadata for block results.



26
27
28
# File 'lib/riffer/guardrails/result.rb', line 26

def 
  @metadata
end

#typeObject (readonly)

The result type (:pass, :transform, or :block).



20
21
22
# File 'lib/riffer/guardrails/result.rb', line 20

def type
  @type
end

Class Method Details

.block(reason, metadata: nil) ⇒ Object

Creates a block result that halts execution.

reason

the reason for blocking.

metadata

optional additional information.

– : (String, ?metadata: Hash[Symbol, untyped]?) -> Riffer::Guardrails::Result



56
57
58
# File 'lib/riffer/guardrails/result.rb', line 56

def block(reason, metadata: nil)
  new(:block, reason, metadata: )
end

.pass(data) ⇒ Object

Creates a pass result that continues with unchanged data.

data

the original data to pass through.

– : (untyped) -> Riffer::Guardrails::Result



35
36
37
# File 'lib/riffer/guardrails/result.rb', line 35

def pass(data)
  new(:pass, data)
end

.transform(data) ⇒ Object

Creates a transform result that continues with transformed data.

data

the transformed data.

– : (untyped) -> Riffer::Guardrails::Result



45
46
47
# File 'lib/riffer/guardrails/result.rb', line 45

def transform(data)
  new(:transform, data)
end

Instance Method Details

#block?Boolean

Returns true if this is a block result.

– : () -> bool

Returns:



99
100
101
# File 'lib/riffer/guardrails/result.rb', line 99

def block?
  type == :block
end

#pass?Boolean

Returns true if this is a pass result.

– : () -> bool

Returns:



83
84
85
# File 'lib/riffer/guardrails/result.rb', line 83

def pass?
  type == :pass
end

#transform?Boolean

Returns true if this is a transform result.

– : () -> bool

Returns:



91
92
93
# File 'lib/riffer/guardrails/result.rb', line 91

def transform?
  type == :transform
end