Class: NRSER::Types::Tuple

Inherits:
ArrayType show all
Defined in:
lib/nrser/types/tuples.rb

Overview

Note:

Construct Tuple types using the Tuple factory.

Tuple type - array of fixed length and types (though those could be Top).

Constant Summary

Constants inherited from ArrayType

ArrayType::DEFAULT_SPLIT_WITH

Instance Attribute Summary collapse

Attributes inherited from IsA

#mod

Instance Method Summary collapse

Methods inherited from ArrayType

#custom_from_s, #item_type

Methods inherited from IsA

#==, #custom_from_data, #has_from_data?, #init_from_data?

Methods inherited from Type

#===, #builtin_inspect, #check, #check!, #from_data, #from_s, #has_from_data?, #has_to_data?, #inspect, #intersection, #name, #not, #respond_to?, #symbolic, #test, #to_data, #to_proc, #to_s, #union, #xor

Constructor Details

#initialize(*types, **options) ⇒ Tuple

Instantiate a new ‘Tuple`.

Parameters:



56
57
58
59
# File 'lib/nrser/types/tuples.rb', line 56

def initialize *types, **options
  super **options
  @types = types.map( &NRSER::Types.method(:make) ).freeze
end

Instance Attribute Details

#typesArray<Type> (readonly)

The types of each of the tuple indexes.

Returns:



39
40
41
# File 'lib/nrser/types/tuples.rb', line 39

def types
  @types
end

Instance Method Details

#default_nameObject



62
63
64
# File 'lib/nrser/types/tuples.rb', line 62

def default_name
  'Array<(' + types.map( &:name ).join( ', ' ) + ')>'
end

#default_symbolicObject



67
68
69
# File 'lib/nrser/types/tuples.rb', line 67

def default_symbolic
  '[' + types.map( &:symbolic ).join( ', ' ) + ']'
end

#explainString

Returns:



75
76
77
# File 'lib/nrser/types/tuples.rb', line 75

def explain
  'Array<(' + types.map( &:explain ).join( ', ' ) + ')>'
end

#has_from_s?Boolean

Returns ‘true` if this type can load values from a string, which is true if all it’s types can load values from strings.

Returns:

  • (Boolean)

    ‘true` if this type can load values from a string, which is true if all it’s types can load values from strings.



107
108
109
# File 'lib/nrser/types/tuples.rb', line 107

def has_from_s?
  @from_s || types.all?( &:has_from_s? )
end

#items_from_strings(strings) ⇒ Array

Load each value in an array of strings split out by NRSER::Types::Type#from_s by passing each value to ‘#from_s` in the type of the corresponding index.

Parameters:

Returns:



120
121
122
123
124
# File 'lib/nrser/types/tuples.rb', line 120

def items_from_strings strings
  types.each_with_index.map { |type, index|
    type.from_s strings[index]
  }
end

#test?(value) ⇒ Boolean

Test value for membership.

Parameters:

  • value (Object)

    Value to test for type satisfaction.

Returns:

  • (Boolean)

    ‘true` if the `value` satisfies the type.

Raises:



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nrser/types/tuples.rb', line 89

def test? value
  # Test the super class first
  return false unless super( value )
  
  # If it's not the right length then it doesn't pass
  return false unless value.length == types.length
  
  # Test each item type
  types.each_with_index.all? { |type, index|
    type.test value[index]
  }
end