Module: Castkit::Core::Config

Included in:
Castkit::Contract::Base
Defined in:
lib/castkit/core/config.rb

Overview

Provides per-class configuration for a Castkit::DataObject, including root key handling, strict mode, and unknown key behavior.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/castkit/core/config.rb', line 8

def self.extended(base)
  super

  base.include(Cattri) unless base.is_a?(Class) && base < Cattri
  return unless base.respond_to?(:cattri)

  base.cattri :strict_flag, nil, scope: :class, expose: :read_write
  base.cattri :warn_on_unknown_flag, nil, scope: :class, expose: :read_write
  base.cattri :allow_unknown_flag, nil, scope: :class, expose: :read_write
end

Instance Method Details

#allow_unknown(value = nil) ⇒ Boolean?

Sets or retrieves whether to allow unknown keys when they are encountered.

Parameters:

  • value (Boolean, nil) (defaults to: nil)

Returns:

  • (Boolean, nil)


54
55
56
# File 'lib/castkit/core/config.rb', line 54

def allow_unknown(value = nil)
  value.nil? ? allow_unknown_flag : (self.allow_unknown_flag = value)
end

#ignore_unknown(value = nil) ⇒ void

This method returns an undefined value.

Enables or disables ignoring unknown keys during deserialization.

This is the inverse of strict.

Parameters:

  • value (Boolean) (defaults to: nil)


38
39
40
# File 'lib/castkit/core/config.rb', line 38

def ignore_unknown(value = nil)
  self.strict_flag = !value
end

#relaxed(warn_on_unknown: true) ⇒ Class

Returns a relaxed version of the current class with strict mode off.

Useful for tolerant parsing scenarios.

Parameters:

  • warn_on_unknown (Boolean) (defaults to: true)

Returns:

  • (Class)

    a subclass with relaxed rules



64
65
66
67
68
69
# File 'lib/castkit/core/config.rb', line 64

def relaxed(warn_on_unknown: true)
  klass = Class.new(self)
  klass.strict(false)
  klass.warn_on_unknown(warn_on_unknown)
  klass
end

#strict(value = nil) ⇒ Boolean

Sets or retrieves strict mode behavior.

In strict mode, unknown keys during deserialization raise errors. If unset, falls back to Castkit.configuration.strict_by_default.

Parameters:

  • value (Boolean, nil) (defaults to: nil)

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/castkit/core/config.rb', line 26

def strict(value = nil)
  return (strict_flag.nil? ? Castkit.configuration.strict_by_default : strict_flag) if value.nil?

  self.strict_flag = !!value
end

#validation_rulesHash{Symbol => Boolean}

Returns a hash of config settings used during validation.

Returns:

  • (Hash{Symbol => Boolean})


74
75
76
77
78
79
80
81
# File 'lib/castkit/core/config.rb', line 74

def validation_rules
  @validation_rules ||= {}
  @validation_rules[:strict] = strict
  @validation_rules[:allow_unknown] = allow_unknown
  @validation_rules[:warn_on_unknown] = warn_on_unknown

  @validation_rules
end

#warn_on_unknown(value = nil) ⇒ Boolean?

Sets or retrieves whether to emit warnings when unknown keys are encountered.

Parameters:

  • value (Boolean, nil) (defaults to: nil)

Returns:

  • (Boolean, nil)


46
47
48
# File 'lib/castkit/core/config.rb', line 46

def warn_on_unknown(value = nil)
  value.nil? ? warn_on_unknown_flag : (self.warn_on_unknown_flag = value)
end