Class: TypedFormModel::CoercerRegistry
- Inherits:
-
Object
- Object
- TypedFormModel::CoercerRegistry
- Defined in:
- lib/typed_form_model/coercer_registry.rb
Overview
A registry of coercion procs keyed by declared prop type.
Form classes each own a registry that inherits entries from their parent
(so subclasses automatically see built-in and parent-custom coercers).
Custom coercers are registered per-form via register_coercer(type, &block)
on the class body.
Lookup order (used by Base.default_coercer_for):
1. Exact registered entries (by class identity, via `lookup`)
2. Built-in fallbacks for the primitive type families
(Integer, Float, Numeric, BigDecimal, String, Symbol,
Date/Time/DateTime, _Boolean, nested forms, arrays of forms)
3. No coercer (the value is passed through to Literal unchanged)
Instance Method Summary collapse
-
#child ⇒ Object
Returns a new registry that inherits from this one.
-
#initialize(parent: nil) ⇒ CoercerRegistry
constructor
A new instance of CoercerRegistry.
-
#lookup(type) ⇒ Object
Find a coercer for the given type.
-
#register(type, &block) ⇒ Object
Register a coercion proc for a specific type.
Constructor Details
#initialize(parent: nil) ⇒ CoercerRegistry
Returns a new instance of CoercerRegistry.
18 19 20 21 |
# File 'lib/typed_form_model/coercer_registry.rb', line 18 def initialize(parent: nil) @entries = {} @parent = parent end |
Instance Method Details
#child ⇒ Object
Returns a new registry that inherits from this one. Used when a subclass declares its own custom coercers.
39 40 41 |
# File 'lib/typed_form_model/coercer_registry.rb', line 39 def child self.class.new(parent: self) end |
#lookup(type) ⇒ Object
Find a coercer for the given type. Returns nil if none registered and no built-in fallback applies.
33 34 35 |
# File 'lib/typed_form_model/coercer_registry.rb', line 33 def lookup(type) @entries[type] || @parent&.lookup(type) end |
#register(type, &block) ⇒ Object
Register a coercion proc for a specific type. The proc receives the raw input and must return the coerced value (or nil).
25 26 27 28 29 |
# File 'lib/typed_form_model/coercer_registry.rb', line 25 def register(type, &block) raise ArgumentError, "register_coercer requires a block" unless block @entries[type] = block self end |