Class: HatiConfig::TypeChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/hati_config/type_checker.rb

Overview

This class is responsible for type checking in the Hati configuration.

Class Method Summary collapse

Class Method Details

.base_type(value, type) ⇒ Boolean

Validates if value matches the base type.

Examples:

TypeChecker.base_type(1, Integer) # => true
TypeChecker.base_type(1, [:int, :float, :big_decimal]) # => true

Parameters:

  • value (Object) —

    the value to validate

  • type (Class) —

    the type to validate against

Returns:

  • (Boolean) —

    true if the value matches the base type

Raises:



49
50
51
52
53
54
55
# File 'lib/hati_config/type_checker.rb', line 49

def base_type(value, type)
  type = fetch_type(type) if type.is_a?(Symbol)

  return type.call(value) if type.is_a?(Proc)

  type.is_a?(Array) ? one_of(value, type) : value.is_a?(type)
end

.call(value, type:, **_opts) ⇒ Boolean

Calls the appropriate validation method based on the type.

Examples:

TypeChecker.call(1, type: :int) # => true
TypeChecker.call(1, type: :numeric) # => true
TypeChecker.call("hello", type: [:str, Integer]) # => true
TypeChecker.call(CustomClass.new, type: CustomClass) # => true

Parameters:

  • value (Object) —

    The value to validate.

  • type (Symbol, Array<Symbol, Class>, Class) —

    The type(s) to validate against.

Returns:

  • (Boolean) —

    True if the value matches the type, false otherwise.

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hati_config/type_checker.rb', line 20

def call(value, type:, **_opts)
  case type
  when Symbol
    base_type(value, fetch_type(type))
  when Array
    if type.length == 1 && type.first.is_a?(Symbol)
      # Array type validation (e.g., [:string] for array of strings)
      return false unless value.is_a?(Array)

      value.all? { |v| call(v, type: type.first) }
    else
      # Union type validation (e.g., [:string, Integer] for string or integer)
      one_of(value, type)
    end
  else
    custom_type?(value, type)
  end
end

.custom_type?(value, type) ⇒ Boolean

Validates if value is of the specified custom type.

Examples:

TypeChecker.custom_type(1, Integer) # => true
TypeChecker.custom_type(CustomClass.new, CustomClass) # => true

Parameters:

  • value (Object) —

    the value to validate

  • type (Class) —

    the custom type to validate against

Returns:

  • (Boolean) —

    true if the value is of the specified custom type



81
82
83
# File 'lib/hati_config/type_checker.rb', line 81

def custom_type?(value, type)
  value.is_a?(type)
end

.fetch_type(type) ⇒ Class

Fetches the basic type from the type map.

Examples:

TypeChecker.fetch_type(:int) # => Integer
TypeChecker.fetch_type(:null) # => NilClass
TypeChecker.fetch_type(:bool) # => [:truthy, :falsy]

Parameters:

  • type (Symbol) —

    the type to fetch

Returns:

  • (Class) —

    the corresponding basic type

Raises:



95
96
97
98
99
100
# File 'lib/hati_config/type_checker.rb', line 95

def fetch_type(type)
  basic_type = TypeMap.get(type)
  raise HatiConfig::TypeCheckerError, type unless basic_type

  basic_type
end

.one_of(value, array_type) ⇒ Boolean

Checks if value matches any type in the array.

Examples:

TypeChecker.one_of(1, [Integer, :str]) # => true
TypeChecker.one_of("hello", [Integer, String, :sym]) # => true
TypeChecker.one_of(nil, [:null, Integer, String]) # => true
TypeChecker.one_of(1.5, [:int, :float, :big_decimal]) # => true

Parameters:

  • value (Object) —

    the value to validate

  • array_type (Array<Symbol, Class>) —

    the array of types to validate against

Returns:

  • (Boolean) —

    true if the value matches any type in the array



68
69
70
# File 'lib/hati_config/type_checker.rb', line 68

def one_of(value, array_type)
  array_type.any? { |type| type.is_a?(Symbol) ? base_type(value, type) : custom_type?(value, type) }
end