Class: BloodContracts::Core::ContractFailure

Inherits:
Refined
  • Object
show all
Defined in:
lib/blood_contracts/core/contract_failure.rb

Overview

Refinement type which represents invalid data

Direct Known Subclasses

SumContractFailure, TupleContractFailure

Instance Attribute Summary

Attributes inherited from Refined

#context

Instance Method Summary collapse

Methods inherited from Refined

===, and_then, call, inherited, #invalid?, match, or_a, #unpack, #valid?

Constructor Details

#initialize(value = nil) ⇒ ContractFailure

Constructs a ContractsFailure using the given value (for ContractFailure value is an error)



9
10
11
12
13
14
# File 'lib/blood_contracts/core/contract_failure.rb', line 9

def initialize(value = nil, **)
  super
  @match = self
  return unless @value
  @context[:errors] = (@context[:errors].to_a << @value.to_h)
end

Instance Method Details

#errorsArray<Hash<BC::Refined, String>>

List of errors per type after the data matching process

Returns:



31
32
33
# File 'lib/blood_contracts/core/contract_failure.rb', line 31

def errors
  @context[:errors].to_a
end

#errors_hHash<BC::Refined, String> Also known as: to_h

Merged map of errors per type after the data matching process

Returns:



47
48
49
# File 'lib/blood_contracts/core/contract_failure.rb', line 47

def errors_h
  errors.reduce(:merge)
end

#matchBC::Refined

The type which is the result of validation (for ContractFailure is always self)

Returns:



57
58
59
# File 'lib/blood_contracts/core/contract_failure.rb', line 57

def match
  self
end

#merge!(contract_failure) ⇒ ContractFailure

Merge errors with the errors of another ContractFailure

Parameters:

  • contract_failure (ContractFailure)

    other errors container which to merge with

Returns:



22
23
24
25
# File 'lib/blood_contracts/core/contract_failure.rb', line 22

def merge!(contract_failure)
  @context[:errors] = @context[:errors].to_a + contract_failure.errors
  self
end

#messagesArray<String>

Flatten list of error messages

Returns:

  • (Array<String>)


39
40
41
# File 'lib/blood_contracts/core/contract_failure.rb', line 39

def messages
  errors.reduce(:merge).values.flatten!
end