Class: Flows::Contract::Tuple

Inherits:
Flows::Contract show all
Defined in:
lib/flows/contract/tuple.rb

Overview

Makes a contract fixed size array.

Underlying contracts' transformations are applied.

Examples:

name_age = Flows::Contract::Tuple.new(String, Integer)

name_age === ['Roman', 29]
# => true

name_age === [10, 20]
# => false

Since:

  • 0.4.0

Constant Summary collapse

ARRAY_CONTRACT =

Since:

  • 0.4.0

CaseEq.new(::Array)

Instance Method Summary collapse

Methods inherited from Flows::Contract

#===, #check, make, #to_proc, #transform

Constructor Details

#initialize(*contracts) ⇒ Tuple

Returns a new instance of Tuple.

Parameters:

  • contracts (Array<Contract, Object>)

    contract list. CaseEq applied to non-contract values.

Since:

  • 0.4.0



19
20
21
# File 'lib/flows/contract/tuple.rb', line 19

def initialize(*contracts)
  @contracts = contracts.map(&method(:to_contract))
end

Instance Method Details

#check!(other) ⇒ Object

Raises:

See Also:

Since:

  • 0.4.0



24
25
26
27
28
29
30
31
32
# File 'lib/flows/contract/tuple.rb', line 24

def check!(other)
  ARRAY_CONTRACT.check!(other)
  check_length(other)

  errors = collect_errors(other)
  return true if errors.empty?

  raise Error.new(other, render_errors(other, errors))
end

#transform!(other) ⇒ Object

See Also:

Since:

  • 0.4.0



35
36
37
38
39
40
41
# File 'lib/flows/contract/tuple.rb', line 35

def transform!(other)
  check!(other)

  other.map.with_index do |elem, index|
    @contracts[index].transform!(elem)
  end
end