Class: EasyTalk::Types::Tuple

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/easy_talk/types/tuple.rb

Overview

Represents a tuple type for arrays with positional type validation.

A tuple is an array where each position has a specific type. This class stores the types for each position.

Examples:

Basic tuple

T::Tuple[String, Integer]  # First item must be String, second must be Integer

With additional_items constraint

property :flags, T::Tuple[T::Boolean, T::Boolean], additional_items: false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*types) ⇒ Tuple

Returns a new instance of Tuple.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
# File 'lib/easy_talk/types/tuple.rb', line 28

def initialize(*types)
  raise ArgumentError, 'Tuple requires at least one type' if types.empty?
  raise ArgumentError, 'Tuple types cannot be nil' if types.any?(&:nil?)

  @types = types.freeze
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



21
22
23
# File 'lib/easy_talk/types/tuple.rb', line 21

def types
  @types
end

Instance Method Details

#nameObject



48
49
50
# File 'lib/easy_talk/types/tuple.rb', line 48

def name
  to_s
end

#to_sObject



39
40
41
42
# File 'lib/easy_talk/types/tuple.rb', line 39

def to_s
  type_names = @types.map { |t| (t.respond_to?(:name) && t.name) || t.to_s }
  "T::Tuple[#{type_names.join(', ')}]"
end