Class: Treaty::Entity::Attribute::Option::Base
- Inherits:
-
Object
- Object
- Treaty::Entity::Attribute::Option::Base
- Defined in:
- lib/treaty/entity/attribute/option/base.rb
Overview
Base class for all option processors (validators and modifiers).
Option Modes
Treaty supports two modes for defining options:
-
Simple Mode - Concise syntax for common cases:
required: trueas: :valuedefault: 12in: %w[twitter linkedin]
-
Advanced Mode - Extended syntax with custom messages:
required: { is: true, message: "Custom error" }as: { is: :value, message: nil }inclusion: { in: %w[...], message: "Must be one of..." }
Helpers
Helpers are shortcuts in DSL that map to simple mode options:
:required→required: true:optional→required: false
Advanced Mode Keys
Each option in advanced mode has a value key:
- Default key:
:is(used by most options) - Special key:
:in(used by inclusion validator)
The value key is defined by overriding value_key method in subclasses.
Processing Phases
Each option processor can participate in three phases:
- Phase 1: Schema validation (validate DSL definition correctness)
- Phase 2: Value validation (validate runtime data values)
- Phase 3: Value transformation (transform values: defaults, renaming, etc.)
Direct Known Subclasses
Conditionals::Base, Modifiers::AsModifier, Modifiers::CastModifier, Modifiers::ComputedModifier, Modifiers::DefaultModifier, Modifiers::TransformModifier, Validators::FormatValidator, Validators::InclusionValidator, Validators::RequiredValidator, Validators::TypeValidator
Instance Method Summary collapse
-
#initialize(attribute_name:, attribute_type:, option_schema:) ⇒ Base
constructor
Creates a new option processor instance.
-
#target_name ⇒ Symbol
Returns the target name for the attribute if this processor transforms names Override in subclasses if needed (e.g., AsModifier).
-
#transform_value(value, _root_data = {}) ⇒ Object
Phase 3: Transforms value Returns transformed value or original if no transformation needed Override in subclasses if transformation is needed.
-
#transforms_name? ⇒ Boolean
Indicates if this option processor transforms attribute names Override in subclasses if needed (e.g., AsModifier).
-
#validate_schema! ⇒ void
Phase 1: Validates schema (DSL definition) Override in subclasses if validation is needed.
-
#validate_value!(value) ⇒ void
Phase 2: Validates value (runtime data) Override in subclasses if validation is needed.
Constructor Details
#initialize(attribute_name:, attribute_type:, option_schema:) ⇒ Base
Creates a new option processor instance
50 51 52 53 54 |
# File 'lib/treaty/entity/attribute/option/base.rb', line 50 def initialize(attribute_name:, attribute_type:, option_schema:) @attribute_name = attribute_name @attribute_type = attribute_type @option_schema = option_schema end |
Instance Method Details
#target_name ⇒ Symbol
Returns the target name for the attribute if this processor transforms names Override in subclasses if needed (e.g., AsModifier)
98 99 100 |
# File 'lib/treaty/entity/attribute/option/base.rb', line 98 def target_name @attribute_name end |
#transform_value(value, _root_data = {}) ⇒ Object
Phase 3: Transforms value Returns transformed value or original if no transformation needed Override in subclasses if transformation is needed
82 83 84 |
# File 'lib/treaty/entity/attribute/option/base.rb', line 82 def transform_value(value, _root_data = {}) value end |
#transforms_name? ⇒ Boolean
Indicates if this option processor transforms attribute names Override in subclasses if needed (e.g., AsModifier)
90 91 92 |
# File 'lib/treaty/entity/attribute/option/base.rb', line 90 def transforms_name? false end |
#validate_schema! ⇒ void
This method returns an undefined value.
Phase 1: Validates schema (DSL definition) Override in subclasses if validation is needed
61 62 63 |
# File 'lib/treaty/entity/attribute/option/base.rb', line 61 def validate_schema! # No-op by default end |
#validate_value!(value) ⇒ void
This method returns an undefined value.
Phase 2: Validates value (runtime data) Override in subclasses if validation is needed
71 72 73 |
# File 'lib/treaty/entity/attribute/option/base.rb', line 71 def validate_value!(value) # No-op by default end |