Class: Flows::Contract::Either
- Inherits:
-
Flows::Contract
- Object
- Flows::Contract
- Flows::Contract::Either
- Defined in:
- lib/flows/contract/either.rb
Overview
Allows to combine two or more contracts with "or" logic.
First matching contract from provided list will be used.
Instance Method Summary collapse
- #check!(other) ⇒ Object
-
#initialize(*contracts) ⇒ Either
constructor
A new instance of Either.
- #transform!(other) ⇒ Object
Methods inherited from Flows::Contract
#===, #check, make, #to_proc, #transform
Constructor Details
#initialize(*contracts) ⇒ Either
Returns a new instance of Either.
20 21 22 23 24 |
# File 'lib/flows/contract/either.rb', line 20 def initialize(*contracts) raise 'Contract list must not be empty' if contracts.length.zero? @contracts = contracts.map(&method(:to_contract)) end |
Instance Method Details
#check!(other) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/flows/contract/either.rb', line 27 def check!(other) errors = @contracts.each_with_object([]) do |con, errs| result = con.check(other) return true if result.ok? errs << result.error end raise Error.new(other, errors.join("\nOR ")) end |
#transform!(other) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/flows/contract/either.rb', line 40 def transform!(other) errors = @contracts.each_with_object([]) do |con, errs| result = con.transform(other) return result.unwrap if result.ok? errs << result.error end raise Error.new(other, errors.join("\nOR ")) end |