Class: Castkit::Configuration

Inherits:
Object
  • Object
show all
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.

Returns:

{
  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.

Returns:

  • (Hash{Symbol => Symbol})
{
  collection: :array,
  bool: :boolean,
  int: :integer,
  map: :hash,
  number: :float,
  str: :string,
  timestamp: :datetime,
  uuid: :string
}.freeze

Instance Method Summary collapse

Constructor Details

#initializevoid

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.enforce_array_options = 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.

Parameters:

  • type (Symbol)

Returns:

Raises:



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.

Examples:

Loading as a default plugin

Castkit.configure do |config|
  config.register_plugin(:custom, CustomPlugin)
  config.default_plugins [:custom]
end

Loading it directly in a Castkit::DataObject

class UserDto < Castkit::DataObject
  enable_plugins :custom
end


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.

Parameters:

  • type (Symbol)

    the symbolic type name (e.g., :uuid)

  • klass (Class<Castkit::Types::Base>)

    the class to register

  • override (Boolean) (defaults to: false)

    whether to allow overwriting existing registration

Raises:

  • (Castkit::TypeError)

    if the type class is invalid or not a subclass of Castkit::Types::Base



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.

Parameters:

  • type (Symbol)

Returns:

  • (Boolean)


125
126
127
# File 'lib/castkit/configuration.rb', line 125

def type_registered?(type)
  types.key?(type.to_sym)
end