module Safrano::Contract
Design: the Invalid
module and the Valid
class share exactly the same methods (ie. interface) so methods returning such objects can be post-processed in a declarative way Example:
something.do_stuff(param).tap_valid{|valid_result| ... } .tap_error{|error| ... }
Constants
- NOK
- NOT_OK
generic error return, when error value does not matter but usefull for control-flow
- OK
generic success return, when return value does not matter but usefull for control-flow
Public Class Methods
and(*contracts)
click to toggle source
# File lib/safrano/contract.rb, line 111 def self.and(*contracts) # pick the first error if any if (ff = contracts.find(&:error)) return ff end # return a new one with @result = list of the contracts's results # usually this then be reduced again with #collect_result! or # #if_valid_collect methods valid(contracts.map(&:result)) end
collect_result!(*contracts) { |*map(&:result)| ... }
click to toggle source
shortcut for Contract.and(*contracts)
.collect_result!
# File lib/safrano/contract.rb, line 123 def self.collect_result!(*contracts) # pick the first error if any if (ff = contracts.find(&:error)) return ff end # return a new one with @result = yield(*list of the contracts's results) valid(yield(*contracts.map(&:result))) end
valid(result)
click to toggle source
# File lib/safrano/contract.rb, line 107 def self.valid(result) Contract::Valid.new(result) end