Class: Castkit::Types::Generic

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

Boolean, Collection, Date, DateTime, Float, Integer, String

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • value (Object)

    the input value

  • validator (#call, nil) (defaults to: nil)

    optional custom validator (default uses validate!)

  • options (Hash) (defaults to: {})

    options passed to validate!, e.g., min, max, force_type

  • context (Symbol, String, nil) (defaults to: {})

    context label for error messages

Returns:

  • (Object)

    the deserialized and validated value



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 ||= options.delete(:validator)
  validator ||= default_validator(instance)

  if options[:force_type]
    deserialized_value = instance.deserialize(value)
    validator.call(deserialized_value, options: options, context: context)
    return deserialized_value
  end

  validator.call(value, options: options, context: context)
  instance.deserialize(value)
end

.deserialize(value) ⇒ Object

Deserializes the value using the default type behavior.

Parameters:

  • value (Object)

Returns:

  • (Object)

    the coerced value



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.

Parameters:

  • value (Object)

Returns:

  • (Object)


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.

Parameters:

  • value (Object)

    the value to check

  • options (Hash) (defaults to: {})

    validation rules (e.g., min, max, format)

  • context (Symbol, String) (defaults to: {})

    label for error reporting



64
65
66
# File 'lib/castkit/types/generic.rb', line 64

def validate!(value, options: {}, context: {})
  new.validate!(value, options: options, context: context)
end

Instance Method Details

#deserialize(value) ⇒ Object

Deserializes the value. Override in subclasses to coerce input (e.g., ‘“123”` → 123).

Parameters:

  • value (Object)

Returns:

  • (Object)


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.

Parameters:

  • value (Object)

Returns:

  • (Object)


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.

Parameters:

  • value (Object)
  • options (Hash) (defaults to: {})
  • context (Symbol, String) (defaults to: {})


103
104
105
# File 'lib/castkit/types/generic.rb', line 103

def validate!(value, options: {}, context: {})
  # override in subclasses
end