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



66
67
68
69
70
71
72
# File 'lib/riffer/guardrails/result.rb', line 66

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).



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

def data
  @data
end

#metadataObject (readonly)

Optional metadata for block results.



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

def 
  @metadata
end

#typeObject (readonly)

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



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

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



52
53
54
# File 'lib/riffer/guardrails/result.rb', line 52

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



33
34
35
# File 'lib/riffer/guardrails/result.rb', line 33

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



42
43
44
# File 'lib/riffer/guardrails/result.rb', line 42

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

Instance Method Details

#block?Boolean

Returns true if this is a block result.

: () -> bool

Returns:

  • (Boolean)


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

def block?
  type == :block
end

#pass?Boolean

Returns true if this is a pass result.

: () -> bool

Returns:

  • (Boolean)


77
78
79
# File 'lib/riffer/guardrails/result.rb', line 77

def pass?
  type == :pass
end

#transform?Boolean

Returns true if this is a transform result.

: () -> bool

Returns:

  • (Boolean)


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

def transform?
  type == :transform
end