Class: Fractor::Workflow::PreExecutionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/fractor/workflow/pre_execution_context.rb

Overview

Manages pre-execution validation for workflows. Provides hooks for custom validation logic and detailed error messages.

This class ensures workflows are validated with full context before execution begins, catching errors early with clear messages.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workflow, input) ⇒ PreExecutionContext

Returns a new instance of PreExecutionContext.



13
14
15
16
17
18
19
# File 'lib/fractor/workflow/pre_execution_context.rb', line 13

def initialize(workflow, input)
  @workflow = workflow
  @input = input
  @errors = []
  @warnings = []
  @validation_hooks = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



11
12
13
# File 'lib/fractor/workflow/pre_execution_context.rb', line 11

def errors
  @errors
end

#inputObject (readonly)

Returns the value of attribute input.



11
12
13
# File 'lib/fractor/workflow/pre_execution_context.rb', line 11

def input
  @input
end

#warningsObject (readonly)

Returns the value of attribute warnings.



11
12
13
# File 'lib/fractor/workflow/pre_execution_context.rb', line 11

def warnings
  @warnings
end

#workflowObject (readonly)

Returns the value of attribute workflow.



11
12
13
# File 'lib/fractor/workflow/pre_execution_context.rb', line 11

def workflow
  @workflow
end

Instance Method Details

#add_error(message) ⇒ Object

Add an error message to the validation context.

Parameters:

  • message (String)

    The error message



70
71
72
# File 'lib/fractor/workflow/pre_execution_context.rb', line 70

def add_error(message)
  @errors << message
end

#add_validation_hook(name = nil) {|context| ... } ⇒ Object

Register a custom validation hook. Hooks should return true if validation passes, or add error messages.

Examples:

Add custom validation

context.add_validation_hook(:check_api_key) do |ctx|
  unless ctx.input.api_key
    ctx.add_error("API key is required")
  end
end

Parameters:

  • name (String, Symbol) (defaults to: nil)

    Optional name for the validation

Yields:

  • (context)

    Block that receives the pre-execution context



33
34
35
36
37
38
39
# File 'lib/fractor/workflow/pre_execution_context.rb', line 33

def add_validation_hook(name = nil, &block)
  unless block
    raise ArgumentError, "Must provide a block for validation hook"
  end

  @validation_hooks << { name: name, block: block }
end

#add_warning(message) ⇒ Object

Add a warning message to the validation context.

Parameters:

  • message (String)

    The warning message



77
78
79
# File 'lib/fractor/workflow/pre_execution_context.rb', line 77

def add_warning(message)
  @warnings << message
end

#has_warnings?Boolean

Check if there are any warnings (regardless of errors).

Returns:

  • (Boolean)

    true if warnings present



91
92
93
# File 'lib/fractor/workflow/pre_execution_context.rb', line 91

def has_warnings?
  !@warnings.empty?
end

#valid?Boolean

Check if validation passed (errors only).

Returns:

  • (Boolean)

    true if no errors



84
85
86
# File 'lib/fractor/workflow/pre_execution_context.rb', line 84

def valid?
  @errors.empty?
end

#validate!Boolean

Run all validations and return whether validation passed.

Returns:

  • (Boolean)

    true if all validations pass

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fractor/workflow/pre_execution_context.rb', line 45

def validate!
  reset_results

  # Run built-in validations
  validate_workflow_definition!
  validate_input_type!
  validate_input_presence!

  # Run custom validation hooks
  run_validation_hooks

  # Raise error if any validation failed
  unless @errors.empty?
    raise WorkflowError, validation_error_message
  end

  # Log warnings if any
  log_warnings unless @warnings.empty?

  true
end