Class: Castkit::Types::Base
- Inherits:
-
Object
- Object
- Castkit::Types::Base
- Defined in:
- lib/castkit/types/base.rb
Overview
Abstract base class for type definitions in Castkit.
Provides default behavior for (de)serialization, validation, and coercion. All primitive types should subclass this and override methods as needed.
The ‘cast!` method is the primary entry point used by attribute processing to validate and coerce values in a predictable order.
Class Method Summary collapse
-
.cast!(value, validator: nil, options: {}, context: {}, **extra_options) ⇒ Object
Coerces and validates a value for use in a Castkit DataObject.
-
.deserialize(value) ⇒ Object
Deserializes the value using the default type behavior.
-
.serialize(value) ⇒ Object
Serializes the value using the default type behavior.
-
.validate!(value, options: {}, context: {}) ⇒ void
Validates the value using the default validator.
Instance Method Summary collapse
-
#deserialize(value) ⇒ Object
Deserializes the value.
-
#serialize(value) ⇒ Object
Serializes the value.
-
#validate!(value, options: {}, context: {}) ⇒ void
Validates the value.
Class Method Details
.cast!(value, validator: nil, options: {}, context: {}, **extra_options) ⇒ Object
Coerces and validates a value for use in a Castkit DataObject.
When ‘force_type` is true, the value is deserialized (coerced) first, then validated. This is useful when a value may need to be converted before it can pass validation (e.g. `“123”` → `123`).
Otherwise, the raw value is validated before coercion.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/castkit/types/base.rb', line 27 def cast!(value, validator: nil, options: {}, context: {}, **) = .merge() instance = new validator ||= .delete(:validator) validator ||= default_validator(instance) if [:force_type] deserialized_value = instance.deserialize(value) invoke_validator(validator, deserialized_value, options: , context: context) return deserialized_value end invoke_validator(validator, value, options: , context: context) instance.deserialize(value) end |
.deserialize(value) ⇒ Object
Deserializes the value using the default type behavior.
47 48 49 |
# File 'lib/castkit/types/base.rb', line 47 def deserialize(value) new.deserialize(value) end |
.serialize(value) ⇒ Object
Serializes the value using the default type behavior.
55 56 57 |
# File 'lib/castkit/types/base.rb', line 55 def serialize(value) new.serialize(value) end |
.validate!(value, options: {}, context: {}) ⇒ void
This method returns an undefined value.
Validates the value using the default validator.
65 66 67 |
# File 'lib/castkit/types/base.rb', line 65 def validate!(value, options: {}, context: {}) new.validate!(value, options: , context: context) end |
Instance Method Details
#deserialize(value) ⇒ Object
Deserializes the value. Override in subclasses to coerce input (e.g., ‘“123”` → `123`).
106 107 108 |
# File 'lib/castkit/types/base.rb', line 106 def deserialize(value) value end |
#serialize(value) ⇒ Object
Serializes the value. Override in subclasses if the output should be transformed.
114 115 116 |
# File 'lib/castkit/types/base.rb', line 114 def serialize(value) value end |
#validate!(value, options: {}, context: {}) ⇒ void
This method returns an undefined value.
Validates the value. No-op by default.
124 125 126 |
# File 'lib/castkit/types/base.rb', line 124 def validate!(value, options: {}, context: {}) # override in subclasses end |