Class: Treaty::Entity::Attribute::OptionNormalizer
- Inherits:
-
Object
- Object
- Treaty::Entity::Attribute::OptionNormalizer
- Defined in:
- lib/treaty/entity/attribute/option_normalizer.rb
Overview
Normalizes options from simple mode to advanced mode.
Purpose
All options are stored and processed internally in advanced mode. This normalizer converts simple mode to advanced mode automatically.
Modes Explained
Simple Mode (Concise syntax)
{
required: true,
as: :value,
in: %w[twitter linkedin github],
default: 12
}
Advanced Mode (With messages)
{
required: { is: true, message: nil },
as: { is: :value, message: nil },
inclusion: { in: %w[twitter linkedin github], message: nil },
default: { is: 12, message: nil }
}
Key Mappings
Some simple mode keys are renamed in advanced mode:
in:→inclusion:(with value key:in)
Others keep the same name:
required:→required:(with value key:is)as:→as:(with value key:is)default:→default:(with value key:is)
Value Keys
Each option has a value key in advanced mode:
- Default:
:is(most options) - Special:
:in(inclusion validator)
Message Field
The message field in advanced mode allows custom error messages:
nil- Use default message (most common)- String - Custom error message for validation failures
Usage in DSL
Users can write in either mode:
Simple mode:
string :provider, in: %w[twitter linkedin]
Advanced mode:
string :provider, inclusion: { in: %w[twitter linkedin], message: "Invalid provider" }
Both are normalized to advanced mode internally.
Class Method Summary collapse
-
.normalize(options) ⇒ Hash
Normalizes all options from simple mode to advanced mode and sorts them by position for consistent execution order.
Class Method Details
.normalize(options) ⇒ Hash
Normalizes all options from simple mode to advanced mode and sorts them by position for consistent execution order.
95 96 97 98 99 100 101 102 |
# File 'lib/treaty/entity/attribute/option_normalizer.rb', line 95 def normalize() normalized = .each_with_object({}) do |(key, value), result| advanced_key, normalized_value = normalize_option(key, value) result[advanced_key] = normalized_value end sort_by_position(normalized) end |