Class: Fried::Typings::TupleOf

Inherits:
Object
  • Object
show all
Includes:
MetaType, Type
Defined in:
lib/fried/typings/tuple_of.rb

Overview

Checks if Array fields match types specified on a 1-to-1 relationship. So ‘[123, “test”, nil]` matches `TupleOf[Numeric, String, NilClass]`

Instance Method Summary collapse

Methods included from Type

#<=>, #call, included

Methods included from MetaType

included

Constructor Details

#initialize(*types) ⇒ TupleOf

Returns a new instance of TupleOf.



19
20
21
# File 'lib/fried/typings/tuple_of.rb', line 19

def initialize(*types)
  @types = types
end

Instance Method Details

#valid?(ary) ⇒ Boolean

Returns:



23
24
25
26
27
28
29
# File 'lib/fried/typings/tuple_of.rb', line 23

def valid?(ary)
  return false unless ary.size == types.size

  types.each.with_index.all? do |type, index|
    Is[type].valid?(ary[index])
  end
end