Class: Castkit::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/castkit/configuration.rb

Overview

Configuration container for global Castkit settings.

This includes validator registration and enforcement flags for various runtime checks.

Constant Summary collapse

DEFAULT_VALIDATORS =

Default mapping of primitive types to validators.

Returns:

  • (Hash<Symbol, Class>)
{
  string: Castkit::Validators::StringValidator,
  integer: Castkit::Validators::NumericValidator,
  float: Castkit::Validators::NumericValidator
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializevoid

Initializes the configuration with default validators and enforcement settings.



53
54
55
56
57
58
59
60
61
62
# File 'lib/castkit/configuration.rb', line 53

def initialize
  @validators = DEFAULT_VALIDATORS.dup
  @enforce_array_of_type = true
  @enforce_known_primitive_type = true
  @enforce_boolean_casting = true
  @enforce_union_match = true
  @enforce_attribute_access = true
  @enforce_unwrapped_prefix = true
  @enforce_array_options = true
end

Instance Attribute Details

#enforce_array_of_typeBoolean

Whether to raise an error if ‘of:` is missing for array types.

Returns:

  • (Boolean)


24
25
26
# File 'lib/castkit/configuration.rb', line 24

def enforce_array_of_type
  @enforce_array_of_type
end

#enforce_array_optionsBoolean

Whether to raise an error if an array attribute is missing the ‘of:` type.

Returns:

  • (Boolean)


48
49
50
# File 'lib/castkit/configuration.rb', line 48

def enforce_array_options
  @enforce_array_options
end

#enforce_attribute_accessBoolean

Whether to raise an error if access mode is not recognized.

Returns:

  • (Boolean)


40
41
42
# File 'lib/castkit/configuration.rb', line 40

def enforce_attribute_access
  @enforce_attribute_access
end

#enforce_boolean_castingBoolean

Whether to raise an error on invalid boolean coercion.

Returns:

  • (Boolean)


32
33
34
# File 'lib/castkit/configuration.rb', line 32

def enforce_boolean_casting
  @enforce_boolean_casting
end

#enforce_known_primitive_typeBoolean

Whether to raise an error for unrecognized primitive types.

Returns:

  • (Boolean)


28
29
30
# File 'lib/castkit/configuration.rb', line 28

def enforce_known_primitive_type
  @enforce_known_primitive_type
end

#enforce_union_matchBoolean

Whether to raise an error if a union type has no matching candidate.

Returns:

  • (Boolean)


36
37
38
# File 'lib/castkit/configuration.rb', line 36

def enforce_union_match
  @enforce_union_match
end

#enforce_unwrapped_prefixBoolean

Whether to raise an error if a prefix is defined without ‘unwrapped: true`.

Returns:

  • (Boolean)


44
45
46
# File 'lib/castkit/configuration.rb', line 44

def enforce_unwrapped_prefix
  @enforce_unwrapped_prefix
end

#validatorsHash<Symbol, #call> (readonly)

Returns registered validators by type.

Returns:

  • (Hash<Symbol, #call>)

    registered validators by type



20
21
22
# File 'lib/castkit/configuration.rb', line 20

def validators
  @validators
end

Instance Method Details

#register_validator(type, validator, override: false) ⇒ void

This method returns an undefined value.

Registers a custom validator for a given type.

Parameters:

  • type (Symbol)

    the type symbol (e.g., :string, :integer)

  • validator (#call)

    a callable object that implements ‘call(value, options:, context:)`

  • override (Boolean) (defaults to: false)

    whether to override an existing validator

Raises:



71
72
73
74
75
76
77
78
79
# File 'lib/castkit/configuration.rb', line 71

def register_validator(type, validator, override: false)
  return if @validators.key?(type.to_sym) && !override

  unless validator.respond_to?(:call)
    raise Castkit::Error, "Validator for `#{type}` must respond to `.call(value, options:, context:)`"
  end

  @validators[type.to_sym] = validator
end

#reset_validators!void

This method returns an undefined value.

Resets all validators to their default mappings.



92
93
94
# File 'lib/castkit/configuration.rb', line 92

def reset_validators!
  @validators = DEFAULT_VALIDATORS.dup
end

#validator_for(type) ⇒ #call?

Returns the registered validator for the given type.

Parameters:

  • type (Symbol)

Returns:

  • (#call, nil)


85
86
87
# File 'lib/castkit/configuration.rb', line 85

def validator_for(type)
  validators[type.to_sym]
end