Class: Castkit::Configuration

Inherits:
Object
  • Object
show all
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::Generic.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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializevoid

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_warningsBoolean

Whether to emit warnings when Castkit detects misconfigurations.

Returns:

  • (Boolean)


64
65
66
# File 'lib/castkit/configuration.rb', line 64

def enable_warnings
  @enable_warnings
end

#enforce_array_optionsBoolean

Whether to raise an error if an array attribute is missing the ‘of:` type.

Returns:

  • (Boolean)


56
57
58
# File 'lib/castkit/configuration.rb', line 56

def enforce_array_options
  @enforce_array_options
end

#enforce_attribute_accessBoolean

Whether to raise an error if access mode is not recognized.

Returns:

  • (Boolean)


48
49
50
# File 'lib/castkit/configuration.rb', line 48

def enforce_attribute_access
  @enforce_attribute_access
end

#enforce_typingBoolean

Whether to raise an error if values should be validated before deserializing, e.g. true -> “true”

Returns:

  • (Boolean)


44
45
46
# File 'lib/castkit/configuration.rb', line 44

def enforce_typing
  @enforce_typing
end

#enforce_unwrapped_prefixBoolean

Whether to raise an error if a prefix is defined without ‘unwrapped: true`.

Returns:

  • (Boolean)


52
53
54
# File 'lib/castkit/configuration.rb', line 52

def enforce_unwrapped_prefix
  @enforce_unwrapped_prefix
end

#raise_type_errorsBoolean

Whether to raise an error for unknown and invalid type definitions.

Returns:

  • (Boolean)


60
61
62
# File 'lib/castkit/configuration.rb', line 60

def raise_type_errors
  @raise_type_errors
end

#strict_by_defaultBoolean

Whether the strict flag is enabled by default for all DataObjects and Contracts.

Returns:

  • (Boolean)


68
69
70
# File 'lib/castkit/configuration.rb', line 68

def strict_by_default
  @strict_by_default
end

#typesHash{Symbol => Castkit::Types::Generic} (readonly)

Returns registered types.

Returns:



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.

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_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::Generic>)

    the class to register

  • override (Boolean) (defaults to: false)

    whether to allow overwriting existing registration

Raises:



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.

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