Class: Treaty::Attribute::Option::Base
- Inherits:
-
Object
- Object
- Treaty::Attribute::Option::Base
- Defined in:
- lib/treaty/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: true`
-
‘as: :value`
-
‘default: 12`
-
‘in: %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::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) ⇒ 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
49 50 51 52 53 |
# File 'lib/treaty/attribute/option/base.rb', line 49 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)
96 97 98 |
# File 'lib/treaty/attribute/option/base.rb', line 96 def target_name @attribute_name end |
#transform_value(value) ⇒ Object
Phase 3: Transforms value Returns transformed value or original if no transformation needed Override in subclasses if transformation is needed
80 81 82 |
# File 'lib/treaty/attribute/option/base.rb', line 80 def transform_value(value) value end |
#transforms_name? ⇒ Boolean
Indicates if this option processor transforms attribute names Override in subclasses if needed (e.g., AsModifier)
88 89 90 |
# File 'lib/treaty/attribute/option/base.rb', line 88 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
60 61 62 |
# File 'lib/treaty/attribute/option/base.rb', line 60 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
70 71 72 |
# File 'lib/treaty/attribute/option/base.rb', line 70 def validate_value!(value) # No-op by default end |