Class: Riffer::Guardrails::Result

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

Overview

Represents the result of a guardrail execution: pass (continue unchanged), transform (continue with changed data), or block (halt with a reason).

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

Raises Riffer::ArgumentError if type is not :pass, :transform, or :block. – : (Symbol, untyped, ?metadata: Hash[Symbol, untyped]?) -> void



44
45
46
47
48
49
50
# File 'lib/riffer/guardrails/result.rb', line 44

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



13
14
15
# File 'lib/riffer/guardrails/result.rb', line 13

def data
  @data
end

#metadataObject (readonly)

Optional metadata for block results.



16
17
18
# File 'lib/riffer/guardrails/result.rb', line 16

def 
  @metadata
end

#typeObject (readonly)

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



10
11
12
# File 'lib/riffer/guardrails/result.rb', line 10

def type
  @type
end

Class Method Details

.block(reason, metadata: nil) ⇒ Object

Creates a block result that halts execution. – : (String, ?metadata: Hash[Symbol, untyped]?) -> Riffer::Guardrails::Result



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

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

.pass(data) ⇒ Object

Creates a pass result that continues with unchanged data. – : (untyped) -> Riffer::Guardrails::Result



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

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

.transform(data) ⇒ Object

Creates a transform result that continues with transformed data. – : (untyped) -> Riffer::Guardrails::Result



29
30
31
# File 'lib/riffer/guardrails/result.rb', line 29

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

Instance Method Details

#block?Boolean

Returns true if this is a block result.

– : () -> bool

Returns:

  • (Boolean)


72
73
74
# File 'lib/riffer/guardrails/result.rb', line 72

def block?
  type == :block
end

#pass?Boolean

Returns true if this is a pass result.

– : () -> bool

Returns:

  • (Boolean)


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

def pass?
  type == :pass
end

#transform?Boolean

Returns true if this is a transform result.

– : () -> bool

Returns:

  • (Boolean)


64
65
66
# File 'lib/riffer/guardrails/result.rb', line 64

def transform?
  type == :transform
end