Class: Castkit::Configuration
- Inherits:
-
Object
- Object
- Castkit::Configuration
- Defined in:
- lib/castkit/configuration.rb
Overview
Configuration container for global Castkit settings.
This includes type registration, validation, and enforcement flags used throughout Castkit’s attribute system.
Constant Summary collapse
- DEFAULT_TYPES =
Default mapping of primitive type definitions.
{ array: Castkit::Types::Collection.new, boolean: Castkit::Types::Boolean.new, date: Castkit::Types::Date.new, datetime: Castkit::Types::DateTime.new, float: Castkit::Types::Float.new, hash: Castkit::Types::Generic.new, integer: Castkit::Types::Integer.new, string: Castkit::Types::String.new }.freeze
- TYPE_ALIASES =
Type aliases for primitive type definitions.
{ collection: :array, bool: :boolean, int: :integer, map: :hash, number: :float, str: :string, timestamp: :datetime, uuid: :string }.freeze
Instance Attribute Summary collapse
-
#enable_warnings ⇒ Boolean
Whether to emit warnings when Castkit detects misconfigurations.
-
#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_typing ⇒ Boolean
Whether to raise an error if values should be validated before deserializing, e.g.
-
#enforce_unwrapped_prefix ⇒ Boolean
Whether to raise an error if a prefix is defined without ‘unwrapped: true`.
-
#raise_type_errors ⇒ Boolean
Whether to raise an error for unknown and invalid type definitions.
-
#strict_by_default ⇒ Boolean
Whether the strict flag is enabled by default for all DataObjects and Contracts.
-
#types ⇒ Hash{Symbol => Castkit::Types::Generic}
readonly
Registered types.
Instance Method Summary collapse
-
#fetch_type(type) ⇒ Castkit::Types::Generic
Returns the type handler for a given type symbol.
-
#initialize ⇒ void
constructor
Initializes the configuration with default types and enforcement flags.
-
#register_type(type, klass, aliases: [], override: false) ⇒ void
Registers a new type definition.
-
#reset_types! ⇒ void
Restores the type registry to its default state.
-
#type_registered?(type) ⇒ Boolean
Returns whether a type is currently registered.
Constructor Details
#initialize ⇒ void
Initializes the configuration with default types and enforcement flags.
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/castkit/configuration.rb', line 73 def initialize @types = DEFAULT_TYPES.dup @enforce_typing = true @enforce_attribute_access = true @enforce_unwrapped_prefix = true @enforce_array_options = true @raise_type_errors = true @enable_warnings = true @strict_by_default = true apply_type_aliases! end |
Instance Attribute Details
#enable_warnings ⇒ Boolean
Whether to emit warnings when Castkit detects misconfigurations.
64 65 66 |
# File 'lib/castkit/configuration.rb', line 64 def enable_warnings @enable_warnings end |
#enforce_array_options ⇒ Boolean
Whether to raise an error if an array attribute is missing the ‘of:` type.
56 57 58 |
# File 'lib/castkit/configuration.rb', line 56 def @enforce_array_options end |
#enforce_attribute_access ⇒ Boolean
Whether to raise an error if access mode is not recognized.
48 49 50 |
# File 'lib/castkit/configuration.rb', line 48 def enforce_attribute_access @enforce_attribute_access end |
#enforce_typing ⇒ Boolean
Whether to raise an error if values should be validated before deserializing, e.g. true -> “true”
44 45 46 |
# File 'lib/castkit/configuration.rb', line 44 def enforce_typing @enforce_typing end |
#enforce_unwrapped_prefix ⇒ Boolean
Whether to raise an error if a prefix is defined without ‘unwrapped: true`.
52 53 54 |
# File 'lib/castkit/configuration.rb', line 52 def enforce_unwrapped_prefix @enforce_unwrapped_prefix end |
#raise_type_errors ⇒ Boolean
Whether to raise an error for unknown and invalid type definitions.
60 61 62 |
# File 'lib/castkit/configuration.rb', line 60 def raise_type_errors @raise_type_errors end |
#strict_by_default ⇒ Boolean
Whether the strict flag is enabled by default for all DataObjects and Contracts.
68 69 70 |
# File 'lib/castkit/configuration.rb', line 68 def strict_by_default @strict_by_default end |
#types ⇒ Hash{Symbol => Castkit::Types::Generic} (readonly)
Returns registered types.
40 41 42 |
# File 'lib/castkit/configuration.rb', line 40 def types @types end |
Instance Method Details
#fetch_type(type) ⇒ Castkit::Types::Generic
Returns the type handler for a given type symbol.
115 116 117 118 119 |
# File 'lib/castkit/configuration.rb', line 115 def fetch_type(type) @types.fetch(type.to_sym) do raise Castkit::TypeError, "Unknown type `#{type.inspect}`" if raise_type_errors end end |
#register_type(type, klass, aliases: [], override: false) ⇒ void
This method returns an undefined value.
Registers a new type definition.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/castkit/configuration.rb', line 93 def register_type(type, klass, aliases: [], override: false) type = type.to_sym return if types.key?(type) && !override instance = klass.new unless instance.is_a?(Castkit::Types::Generic) raise Castkit::TypeError, "Expected subclass of Castkit::Types::Generic for `#{type}`" end types[type] = instance Castkit::Core::AttributeTypes.define_type_dsl(type) if Castkit::Core::AttributeTypes.respond_to?(:define_type_dsl) return unless aliases.any? aliases.each { |alias_type| register_type(alias_type, klass, override: override) } end |
#reset_types! ⇒ void
This method returns an undefined value.
Restores the type registry to its default state.
132 133 134 135 |
# File 'lib/castkit/configuration.rb', line 132 def reset_types! @types = DEFAULT_TYPES.dup apply_type_aliases! end |
#type_registered?(type) ⇒ Boolean
Returns whether a type is currently registered.
125 126 127 |
# File 'lib/castkit/configuration.rb', line 125 def type_registered?(type) @types.key?(type.to_sym) end |