Class: Castkit::Configuration
- Inherits:
-
Object
- Object
- Castkit::Configuration
- Includes:
- Cattri
- 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 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.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/castkit/configuration.rb', line 55 def initialize super self.types = DEFAULT_TYPES.dup self.enforce_typing = true self.enforce_attribute_access = true self.enforce_unwrapped_prefix = true self. = true self.raise_type_errors = true self.enable_warnings = true self.strict_by_default = true self.default_plugins = [] apply_type_aliases! end |
Instance Method Details
#fetch_type(type) ⇒ Castkit::Types::Base
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_plugin(name, plugin) ⇒ Object
Register a custom plugin for use with Castkit::DataObject.
106 107 108 |
# File 'lib/castkit/configuration.rb', line 106 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.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/castkit/configuration.rb', line 77 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.
132 133 134 135 |
# File 'lib/castkit/configuration.rb', line 132 def reset_types! self.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 |