Class: Castkit::Configuration
- Inherits:
-
Object
- Object
- Castkit::Configuration
- 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.
{ string: Castkit::Validators::StringValidator, integer: Castkit::Validators::NumericValidator, float: Castkit::Validators::NumericValidator }.freeze
Instance Attribute Summary collapse
-
#enforce_array_of_type ⇒ Boolean
Whether to raise an error if ‘of:` is missing for array types.
-
#enforce_array_options ⇒ Boolean
Whether to raise an error if an array attribute is missing the ‘of:` type.
-
#enforce_attribute_access ⇒ Boolean
Whether to raise an error if access mode is not recognized.
-
#enforce_boolean_casting ⇒ Boolean
Whether to raise an error on invalid boolean coercion.
-
#enforce_known_primitive_type ⇒ Boolean
Whether to raise an error for unrecognized primitive types.
-
#enforce_union_match ⇒ Boolean
Whether to raise an error if a union type has no matching candidate.
-
#enforce_unwrapped_prefix ⇒ Boolean
Whether to raise an error if a prefix is defined without ‘unwrapped: true`.
-
#validators ⇒ Hash<Symbol, #call>
readonly
Registered validators by type.
Instance Method Summary collapse
-
#initialize ⇒ void
constructor
Initializes the configuration with default validators and enforcement settings.
-
#register_validator(type, validator, override: false) ⇒ void
Registers a custom validator for a given type.
-
#reset_validators! ⇒ void
Resets all validators to their default mappings.
-
#validator_for(type) ⇒ #call?
Returns the registered validator for the given type.
Constructor Details
#initialize ⇒ void
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 = true end |
Instance Attribute Details
#enforce_array_of_type ⇒ Boolean
Whether to raise an error if ‘of:` is missing for array types.
24 25 26 |
# File 'lib/castkit/configuration.rb', line 24 def enforce_array_of_type @enforce_array_of_type end |
#enforce_array_options ⇒ Boolean
Whether to raise an error if an array attribute is missing the ‘of:` type.
48 49 50 |
# File 'lib/castkit/configuration.rb', line 48 def end |
#enforce_attribute_access ⇒ Boolean
Whether to raise an error if access mode is not recognized.
40 41 42 |
# File 'lib/castkit/configuration.rb', line 40 def enforce_attribute_access @enforce_attribute_access end |
#enforce_boolean_casting ⇒ Boolean
Whether to raise an error on invalid boolean coercion.
32 33 34 |
# File 'lib/castkit/configuration.rb', line 32 def enforce_boolean_casting @enforce_boolean_casting end |
#enforce_known_primitive_type ⇒ Boolean
Whether to raise an error for unrecognized primitive types.
28 29 30 |
# File 'lib/castkit/configuration.rb', line 28 def enforce_known_primitive_type @enforce_known_primitive_type end |
#enforce_union_match ⇒ Boolean
Whether to raise an error if a union type has no matching candidate.
36 37 38 |
# File 'lib/castkit/configuration.rb', line 36 def enforce_union_match @enforce_union_match end |
#enforce_unwrapped_prefix ⇒ Boolean
Whether to raise an error if a prefix is defined without ‘unwrapped: true`.
44 45 46 |
# File 'lib/castkit/configuration.rb', line 44 def enforce_unwrapped_prefix @enforce_unwrapped_prefix end |
#validators ⇒ Hash<Symbol, #call> (readonly)
Returns 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.
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.
85 86 87 |
# File 'lib/castkit/configuration.rb', line 85 def validator_for(type) validators[type.to_sym] end |