Class: Castkit::Types::Generic
- Inherits:
-
Object
- Object
- Castkit::Types::Generic
- Defined in:
- lib/castkit/types/generic.rb
Overview
Generic 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: {}) ⇒ 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: {}) ⇒ 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 |
# File 'lib/castkit/types/generic.rb', line 27 def cast!(value, validator: nil, options: {}, context: {}) instance = new validator ||= .delete(:validator) validator ||= default_validator(instance) if [:force_type] deserialized_value = instance.deserialize(value) validator.call(deserialized_value, options: , context: context) return deserialized_value end validator.call(value, options: , context: context) instance.deserialize(value) end |
.deserialize(value) ⇒ Object
Deserializes the value using the default type behavior.
46 47 48 |
# File 'lib/castkit/types/generic.rb', line 46 def deserialize(value) new.deserialize(value) end |
.serialize(value) ⇒ Object
Serializes the value using the default type behavior.
54 55 56 |
# File 'lib/castkit/types/generic.rb', line 54 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.
64 65 66 |
# File 'lib/castkit/types/generic.rb', line 64 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).
85 86 87 |
# File 'lib/castkit/types/generic.rb', line 85 def deserialize(value) value end |
#serialize(value) ⇒ Object
Serializes the value. Override in subclasses if the output should be transformed.
93 94 95 |
# File 'lib/castkit/types/generic.rb', line 93 def serialize(value) value end |
#validate!(value, options: {}, context: {}) ⇒ void
This method returns an undefined value.
Validates the value. No-op by default.
103 104 105 |
# File 'lib/castkit/types/generic.rb', line 103 def validate!(value, options: {}, context: {}) # override in subclasses end |