Class: Treaty::Entity::Attribute::OptionOrchestrator

Inherits:
Object
  • Object
show all
Defined in:
lib/treaty/entity/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.

orchestrator.validate_schema!

Phase 2: Value Validation

Validates that runtime data matches the constraints. Called for each request/response.

orchestrator.validate_value!(value)

Phase 3: Value Transformation

Transforms the value (applies defaults, renaming, etc.). Called for each request/response after validation.

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:



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

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



135
136
137
# File 'lib/treaty/entity/attribute/option_orchestrator.rb', line 135

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



126
127
128
129
# File 'lib/treaty/entity/attribute/option_orchestrator.rb', line 126

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

#transform_value(value, root_data = {}) ⇒ Object

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

Parameters:

  • value (Object)

    The value to transform

  • root_data (Hash) (defaults to: {})

    Full raw data from root level (used by computed modifier)

Returns:

  • (Object)

    Transformed value



109
110
111
112
113
# File 'lib/treaty/entity/attribute/option_orchestrator.rb', line 109

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

#transforms_name?Boolean

Checks if any processor transforms the attribute name

Returns:

  • (Boolean)

    True if any processor (like AsModifier) transforms names



118
119
120
# File 'lib/treaty/entity/attribute/option_orchestrator.rb', line 118

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:



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

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:



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

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