Module: OpenUSD::Types
- Defined in:
- lib/openusd/types.rb
Overview
Registry and validation for built-in USD value types.
Constant Summary collapse
- INTEGER_RANGES =
Integer ranges enforced when authoring.
{ "int" => (-(2**31)...(2**31)), "uint" => (0...(2**32)), "int64" => (-(2**63)...(2**63)), "uint64" => (0...(2**64)) }.freeze
- FLOAT_TYPES =
Floating-point scalar type names.
%w[half float double].freeze
- VECTOR_TYPES =
Vector and quaternion type names mapped to component counts.
{ "float2" => 2, "float3" => 3, "float4" => 4, "double2" => 2, "double3" => 3, "double4" => 4, "half2" => 2, "half3" => 3, "half4" => 4, "int2" => 2, "int3" => 3, "int4" => 4, "point3f" => 3, "normal3f" => 3, "color3f" => 3, "color4f" => 4, "texCoord2f" => 2, "texCoord3f" => 3, "quatf" => 4, "quatd" => 4, "quath" => 4 }.freeze
- MATRIX_TYPES =
Matrix type names mapped to dimensions.
{ "matrix2d" => 2, "matrix3d" => 3, "matrix4d" => 4 }.freeze
- SCALAR_TYPES =
Built-in scalar type names.
%w[bool int uint int64 uint64 half float double string token asset].freeze
- SIMPLE_COERCERS =
Internal dispatch table for scalar normalization.
{ "bool" => :coerce_bool, "half" => :coerce_float, "float" => :coerce_float, "double" => :coerce_float, "string" => :coerce_string, "token" => :coerce_token, "asset" => :coerce_asset }.freeze
Class Method Summary collapse
-
.array?(type_name) ⇒ Boolean
Whether the type is an array type.
-
.base_type(type_name) ⇒ Object
Remove the array suffix from a type name.
-
.coerce(type_name, value) ⇒ Object
Validate and normalize a Ruby value for a USD type.
-
.known?(type_name) ⇒ Boolean
Whether the registry recognizes a type name.
-
.validate!(type_name, value) ⇒ Object
Validate a Ruby value and return its normalized representation.
Class Method Details
.array?(type_name) ⇒ Boolean
Whether the type is an array type.
49 50 51 |
# File 'lib/openusd/types.rb', line 49 def array?(type_name) String(type_name).end_with?("[]") end |
.base_type(type_name) ⇒ Object
Remove the array suffix from a type name.
54 55 56 |
# File 'lib/openusd/types.rb', line 54 def base_type(type_name) String(type_name).delete_suffix("[]") end |
.coerce(type_name, value) ⇒ Object
Validate and normalize a Ruby value for a USD type. Unknown types are intentionally preserved for forward compatibility.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/openusd/types.rb', line 60 def coerce(type_name, value) name = String(type_name) return coerce_array(base_type(name), value) if array?(name) return value unless known?(name) coerce_scalar(name, value) rescue OpenUSD::TypeError raise rescue StandardError invalid!(name, value) end |
.known?(type_name) ⇒ Boolean
Whether the registry recognizes a type name.
43 44 45 46 |
# File 'lib/openusd/types.rb', line 43 def known?(type_name) name = base_type(type_name) SCALAR_TYPES.include?(name) || VECTOR_TYPES.key?(name) || MATRIX_TYPES.key?(name) end |
.validate!(type_name, value) ⇒ Object
Validate a Ruby value and return its normalized representation.
73 74 75 |
# File 'lib/openusd/types.rb', line 73 def validate!(type_name, value) coerce(type_name, value) end |