Class: Treaty::Attribute::OptionOrchestrator
- Inherits:
-
Object
- Object
- Treaty::Attribute::OptionOrchestrator
- 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
-
**Processor Building** - Creates instances of all relevant option processors
-
**Schema Validation** - Validates DSL definition correctness (phase 1)
-
**Value Validation** - Validates runtime data values (phase 2)
-
**Value Transformation** - Transforms values through modifiers (phase 3)
-
**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
-
#initialize(attribute) ⇒ OptionOrchestrator
constructor
Creates a new orchestrator instance.
-
#processor_for(option_name) ⇒ Option::Base?
Gets specific processor by option name.
-
#target_name ⇒ Symbol
Gets the target name from the processor that transforms names Returns original name if no transformation.
-
#transform_value(value) ⇒ Object
Phase 3: Transforms value through all option modifiers Applies transformations like defaults, type coercion, etc.
-
#transforms_name? ⇒ Boolean
Checks if any processor transforms the attribute name.
-
#validate_schema! ⇒ void
Phase 1: Validates all option schemas Ensures DSL definition is correct and all options are registered.
-
#validate_value!(value) ⇒ void
Phase 2: Validates value against all option validators Validates runtime data against all defined constraints.
Constructor Details
#initialize(attribute) ⇒ OptionOrchestrator
Creates a new orchestrator instance
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
133 134 135 |
# File 'lib/treaty/attribute/option_orchestrator.rb', line 133 def processor_for(option_name) @processors.fetch(option_name) end |
#target_name ⇒ Symbol
Gets the target name from the processor that transforms names Returns original name if no transformation
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.
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
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
84 85 86 87 88 |
# File 'lib/treaty/attribute/option_orchestrator.rb', line 84 def validate_schema! @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
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 |