Class: Treaty::Entity::Attribute::OptionOrchestrator
- Inherits:
-
Object
- Object
- Treaty::Entity::Attribute::OptionOrchestrator
- 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
- 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.
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
-
#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, root_data = {}) ⇒ 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
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
135 136 137 |
# File 'lib/treaty/entity/attribute/option_orchestrator.rb', line 135 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
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.
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
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
85 86 87 88 89 |
# File 'lib/treaty/entity/attribute/option_orchestrator.rb', line 85 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
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 |