Class: Kleisli::Contracts::EitherOf

Inherits:
KContract
  • Object
show all
Defined in:
lib/kleisli/contracts.rb

Direct Known Subclasses

ValidationOf

Instance Method Summary collapse

Methods inherited from KContract

#initialize

Constructor Details

This class inherits a constructor from Kleisli::Contracts::KContract

Instance Method Details

#monad_valid(val) ⇒ Object



30
31
32
33
# File 'lib/kleisli/contracts.rb', line 30

def monad_valid(val)
  [Kleisli::Either::Left, Kleisli::Either::Right].
    include?(val.class)
end

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'lib/kleisli/contracts.rb', line 34

def valid?(val)
  return false unless monad_valid(val) &&
    @inner_types.size == 2
  left_valid, _ = Contract.valid?(val.left, @inner_types.first)
  right_valid, _ = Contract.valid?(val.right, @inner_types.last)
  (val.left.nil? && right_valid) ||
  (val.right.nil? && left_valid)
end