Module: Ruckt::StructGenerator::TypeValidation

Defined in:
lib/ruckt/struct_generator.rb

Overview

Module containing type validation functionality This is mixed into generated struct classes

Instance Method Summary collapse

Instance Method Details

#validate_type(key, value) ⇒ Object

Validates that a value matches the expected type for an attribute

Parameters:

  • key (Symbol)

    The attribute name

  • value (Object)

    The value to validate

Raises:

  • (TypeError)

    If the value doesn’t match the expected type



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ruckt/struct_generator.rb', line 112

def validate_type(key, value)
  expected_type = self.class.type_info[key]
  
  # Handle cases where multiple types are allowed (e.g., true/false)
  if expected_type.is_a?(Array)
    return if expected_type.any? { |type| value.is_a?(type) }
    raise TypeError, "Expected #{key} to be one of #{expected_type.join(', ')}, got #{value.class}"
  end

  # Handle single type validation
  return if value.is_a?(expected_type)
  raise TypeError, "Expected #{key} to be #{expected_type}, got #{value.class}"
end