Class: NRSER::Types::TupleType

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

Overview

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

Constant Summary

Constants inherited from ArrayType

ArrayType::DEFAULT_SPLIT_WITH

Instance Attribute Summary

Attributes inherited from ArrayType

#item_type

Attributes inherited from IsA

#klass

Instance Method Summary collapse

Methods inherited from ArrayType

#from_s

Methods inherited from IsA

#from_data, #has_from_data?

Methods inherited from Type

#check, #from_data, #from_s, #has_from_data?, #has_to_data?, #name, #respond_to?, short_name, #to_data, #to_s

Constructor Details

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

Instantiate a new ‘TupleType`.



47
48
49
50
# File 'lib/nrser/types/tuples.rb', line 47

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

Instance Method Details

#default_nameObject



53
54
55
# File 'lib/nrser/types/tuples.rb', line 53

def default_name
  '[' + @types.map( &:name ).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.



87
88
89
# File 'lib/nrser/types/tuples.rb', line 87

def has_from_s?
  @types.all? &:has_from_s?
end

#items_from_strings(strings) ⇒ Array

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

Parameters:

  • strings (Array<String>)

Returns:

  • (Array)


100
101
102
103
104
# File 'lib/nrser/types/tuples.rb', line 100

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

#test(value) ⇒ return_type

TODO:

Document test method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nrser/types/tuples.rb', line 69

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