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::Base.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
-
#default_plugins ⇒ Array<Symbol>
Set default plugins that will be used globally in all Castkit::DataObject subclasses.
-
#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::Base}
readonly
Registered types.
Instance Method Summary collapse
-
#fetch_type(type) ⇒ Castkit::Types::Base
Returns the type handler for a given type symbol.
-
#initialize ⇒ void
constructor
Initializes the configuration with default types and enforcement flags.
-
#register_plugin(name, plugin) ⇒ Object
Register a custom plugin for use with Castkit::DataObject.
-
#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.
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/castkit/configuration.rb', line 79 def initialize @types = DEFAULT_TYPES.dup @enforce_typing = true @enforce_attribute_access = true @enforce_unwrapped_prefix = true = true @raise_type_errors = true @enable_warnings = true @strict_by_default = true @default_plugins = [] apply_type_aliases! end |
Instance Attribute Details
#default_plugins ⇒ Array<Symbol>
Set default plugins that will be used globally in all Castkit::DataObject subclasses. This is equivalent to calling ‘enable_plugins` in every class.
46 47 48 |
# File 'lib/castkit/configuration.rb', line 46 def default_plugins @default_plugins end |
#enable_warnings ⇒ Boolean
Whether to emit warnings when Castkit detects misconfigurations.
70 71 72 |
# File 'lib/castkit/configuration.rb', line 70 def enable_warnings @enable_warnings end |
#enforce_array_options ⇒ Boolean
Whether to raise an error if an array attribute is missing the ‘of:` type.
62 63 64 |
# File 'lib/castkit/configuration.rb', line 62 def end |
#enforce_attribute_access ⇒ Boolean
Whether to raise an error if access mode is not recognized.
54 55 56 |
# File 'lib/castkit/configuration.rb', line 54 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”
50 51 52 |
# File 'lib/castkit/configuration.rb', line 50 def enforce_typing @enforce_typing end |
#enforce_unwrapped_prefix ⇒ Boolean
Whether to raise an error if a prefix is defined without ‘unwrapped: true`.
58 59 60 |
# File 'lib/castkit/configuration.rb', line 58 def enforce_unwrapped_prefix @enforce_unwrapped_prefix end |
#raise_type_errors ⇒ Boolean
Whether to raise an error for unknown and invalid type definitions.
66 67 68 |
# File 'lib/castkit/configuration.rb', line 66 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.
74 75 76 |
# File 'lib/castkit/configuration.rb', line 74 def strict_by_default @strict_by_default end |
#types ⇒ Hash{Symbol => Castkit::Types::Base} (readonly)
40 41 42 |
# File 'lib/castkit/configuration.rb', line 40 def types @types end |
Instance Method Details
#fetch_type(type) ⇒ Castkit::Types::Base
Returns the type handler for a given type symbol.
138 139 140 141 142 |
# File 'lib/castkit/configuration.rb', line 138 def fetch_type(type) @types.fetch(type.to_sym) do raise Castkit::TypeError, "Unknown type `#{type.inspect}`" if raise_type_errors end end |
#register_plugin(name, plugin) ⇒ Object
Register a custom plugin for use with Castkit::DataObject.
129 130 131 |
# File 'lib/castkit/configuration.rb', line 129 def register_plugin(name, plugin) Castkit::Plugins.register(name, plugin) end |
#register_type(type, klass, aliases: [], override: false) ⇒ void
This method returns an undefined value.
Registers a new type definition.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/castkit/configuration.rb', line 100 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::Base) raise Castkit::TypeError, "Expected subclass of Castkit::Types::Base 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.
155 156 157 158 |
# File 'lib/castkit/configuration.rb', line 155 def reset_types! @types = DEFAULT_TYPES.dup apply_type_aliases! end |
#type_registered?(type) ⇒ Boolean
Returns whether a type is currently registered.
148 149 150 |
# File 'lib/castkit/configuration.rb', line 148 def type_registered?(type) @types.key?(type.to_sym) end |