Class: Flows::Contract::Array

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

Overview

Makes a contract for Array from contract for array's element.

If underlying contract has transformation - each array element will be transformed.

Examples:

vector = Flows::Contract::Array.new(Numeric)

vector === 10
# => false

vector === [10, 20]
# => true

Since:

  • 0.4.0

Constant Summary collapse

CHECK_LIMIT =

Stop search for a new type mismatch in elements if CHECK_LIMIT errors already found.

Since:

  • 0.4.0

10
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(element_contract) ⇒ Array

Returns a new instance of Array.

Parameters:

  • element_contract (Contract, Object)

    contract for each element. For not-contract values CaseEq used.

Since:

  • 0.4.0



24
25
26
# File 'lib/flows/contract/array.rb', line 24

def initialize(element_contract)
  @contract = to_contract(element_contract)
end

Instance Method Details

#check!(other) ⇒ Object

Raises:

See Also:

Since:

  • 0.4.0



29
30
31
32
33
34
35
# File 'lib/flows/contract/array.rb', line 29

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

  raise Error.new(other, report_errors(other)) unless other.all?(&@contract)

  true
end

#transform!(other) ⇒ Object

See Also:

Since:

  • 0.4.0



38
39
40
41
42
# File 'lib/flows/contract/array.rb', line 38

def transform!(other)
  check!(other)

  other.map { |elem| @contract.transform!(elem) }
end