Class: BloodContracts::Core::ContractFailure
- Defined in:
- lib/blood_contracts/core/contract_failure.rb
Overview
Refinement type which represents invalid data
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Refined
Instance Method Summary collapse
-
#errors ⇒ Array<Hash<BC::Refined, String>>
List of errors per type after the data matching process.
-
#errors_h ⇒ Hash<BC::Refined, String>
(also: #to_h)
Merged map of errors per type after the data matching process.
-
#initialize(value = nil) ⇒ ContractFailure
constructor
Constructs a ContractsFailure using the given value (for ContractFailure value is an error).
-
#match ⇒ BC::Refined
The type which is the result of validation (for ContractFailure is always self).
-
#merge!(contract_failure) ⇒ ContractFailure
Merge errors with the errors of another ContractFailure.
-
#messages ⇒ Array<String>
Flatten list of error messages.
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
#errors ⇒ Array<Hash<BC::Refined, String>>
List of errors per type after the data matching process
31 32 33 |
# File 'lib/blood_contracts/core/contract_failure.rb', line 31 def errors @context[:errors].to_a end |
#errors_h ⇒ Hash<BC::Refined, String> Also known as: to_h
Merged map of errors per type after the data matching process
47 48 49 |
# File 'lib/blood_contracts/core/contract_failure.rb', line 47 def errors_h errors.reduce(:merge) end |
#match ⇒ BC::Refined
The type which is the result of validation (for ContractFailure is always self)
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
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 |
#messages ⇒ Array<String>
Flatten list of error messages
39 40 41 |
# File 'lib/blood_contracts/core/contract_failure.rb', line 39 def errors.reduce(:merge).values.flatten! end |