Class: HatiConfig::TypeChecker
- Inherits:
-
Object
- Object
- HatiConfig::TypeChecker
- Defined in:
- lib/hati_config/type_checker.rb
Overview
This class is responsible for type checking in the Hati configuration.
Class Method Summary collapse
-
.base_type(value, type) ⇒ Boolean
Validates if value matches the base type.
-
.call(value, type:, **_opts) ⇒ Boolean
Calls the appropriate validation method based on the type.
-
.custom_type?(value, type) ⇒ Boolean
Validates if value is of the specified custom type.
-
.fetch_type(type) ⇒ Class
Fetches the basic type from the type map.
-
.one_of(value, array_type) ⇒ Boolean
Checks if value matches any type in the array.
Class Method Details
.base_type(value, type) ⇒ Boolean
Validates if value matches the base type.
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.
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.
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.
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.
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 |