Class: Treaty::Attribute::OptionOrchestrator

Inherits:
Object
  • Object
show all
Defined in:
lib/treaty/attribute/option_orchestrator.rb

Overview

Orchestrates all option processors for a single attribute.

## Purpose

Coordinates the execution of all option processors (validators and modifiers) for an attribute through three distinct processing phases.

## Responsibilities

  1. **Processor Building** - Creates instances of all relevant option processors

  2. **Schema Validation** - Validates DSL definition correctness (phase 1)

  3. **Value Validation** - Validates runtime data values (phase 2)

  4. **Value Transformation** - Transforms values through modifiers (phase 3)

  5. **Name Transformation** - Provides target name if ‘as:` option is used

## Processing Phases

### Phase 1: Schema Validation Validates that the attribute definition in the DSL is correct. Called once during treaty definition loading.

“‘ruby orchestrator.validate_schema! “`

### Phase 2: Value Validation Validates that runtime data matches the constraints. Called for each request/response.

“‘ruby orchestrator.validate_value!(value) “`

### Phase 3: Value Transformation Transforms the value (applies defaults, renaming, etc.). Called for each request/response after validation.

“‘ruby transformed = orchestrator.transform_value(value) “`

## Usage

Used by AttributeValidator to coordinate all option processing:

orchestrator = OptionOrchestrator.new(attribute)
orchestrator.validate_schema!
orchestrator.validate_value!(value)
transformed = orchestrator.transform_value(value)
target_name = orchestrator.target_name

## Processor Building

Automatically:

  • Builds processor instances for all defined options

  • Always includes TypeValidator (even if not explicitly defined)

  • Validates that all options are registered in Registry

  • Raises error for unknown options

## Architecture

Works with:

  • Option::Registry - Looks up processor classes

  • Option::Base - Base class for all processors

  • AttributeValidator - Uses orchestrator to coordinate processing

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ OptionOrchestrator

Creates a new orchestrator instance

Parameters:



74
75
76
77
# File 'lib/treaty/attribute/option_orchestrator.rb', line 74

def initialize(attribute)
  @attribute = attribute
  @processors = build_processors
end

Instance Method Details

#processor_for(option_name) ⇒ Option::Base?

Gets specific processor by option name

Parameters:

  • option_name (Symbol)

    The option name (:required, :type, etc.)

Returns:

  • (Option::Base, nil)

    The processor instance or nil if not found



133
134
135
# File 'lib/treaty/attribute/option_orchestrator.rb', line 133

def processor_for(option_name)
  @processors.fetch(option_name)
end

#target_nameSymbol

Gets the target name from the processor that transforms names Returns original name if no transformation

Returns:

  • (Symbol)

    The target attribute name



124
125
126
127
# File 'lib/treaty/attribute/option_orchestrator.rb', line 124

def target_name
  name_transformer = @processors.values.find(&:transforms_name?)
  name_transformer ? name_transformer.target_name : @attribute.name
end

#transform_value(value) ⇒ Object

Phase 3: Transforms value through all option modifiers Applies transformations like defaults, type coercion, etc.

Parameters:

  • value (Object)

    The value to transform

Returns:

  • (Object)

    Transformed value



107
108
109
110
111
# File 'lib/treaty/attribute/option_orchestrator.rb', line 107

def transform_value(value)
  @processors.values.reduce(value) do |current_value, processor|
    processor.transform_value(current_value)
  end
end

#transforms_name?Boolean

Checks if any processor transforms the attribute name

Returns:

  • (Boolean)

    True if any processor (like AsModifier) transforms names



116
117
118
# File 'lib/treaty/attribute/option_orchestrator.rb', line 116

def transforms_name?
  @processors.values.any?(&:transforms_name?)
end

#validate_schema!void

This method returns an undefined value.

Phase 1: Validates all option schemas Ensures DSL definition is correct and all options are registered

Raises:



84
85
86
87
88
# File 'lib/treaty/attribute/option_orchestrator.rb', line 84

def validate_schema!
  validate_known_options!

  @processors.each_value(&:validate_schema!)
end

#validate_value!(value) ⇒ void

This method returns an undefined value.

Phase 2: Validates value against all option validators Validates runtime data against all defined constraints

Parameters:

  • value (Object)

    The value to validate

Raises:



96
97
98
99
100
# File 'lib/treaty/attribute/option_orchestrator.rb', line 96

def validate_value!(value)
  @processors.each_value do |processor|
    processor.validate_value!(value)
  end
end