class Flows::Contract::Array

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

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

@example

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

vector === 10
# => false

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

Constants

ARRAY_CONTRACT
CHECK_LIMIT

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

Public Class Methods

new(element_contract) click to toggle source

@param element_contract [Contract, Object] contract for each element. For not-contract values {CaseEq} used.

# File lib/flows/contract/array.rb, line 24
def initialize(element_contract)
  @contract = to_contract(element_contract)
end

Public Instance Methods

check!(other) click to toggle source

@see Contract#check!

# 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) click to toggle source

@see Contract#transform!

# File lib/flows/contract/array.rb, line 38
def transform!(other)
  check!(other)

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

Private Instance Methods

report_errors(other) click to toggle source
# File lib/flows/contract/array.rb, line 46
def report_errors(other)
  other.reject(&@contract)[0..CHECK_LIMIT].map do |elem|
    element_error = @contract.check(elem).error

    merge_nested_errors("array element `#{elem.inspect}` is invalid:", element_error)
  end.join("\n")
end