Class: TypedContracts::EitherOf
- Inherits:
-
Contracts::CallableClass
- Object
- Contracts::CallableClass
- TypedContracts::EitherOf
- Defined in:
- lib/typed_contracts.rb
Instance Method Summary collapse
-
#initialize(*vals) ⇒ EitherOf
constructor
A new instance of EitherOf.
- #monad_valid(val) ⇒ Object
- #to_s ⇒ Object
- #valid?(val) ⇒ Boolean
Constructor Details
#initialize(*vals) ⇒ EitherOf
Returns a new instance of EitherOf.
33 34 35 |
# File 'lib/typed_contracts.rb', line 33 def initialize(*vals) @vals = vals end |
Instance Method Details
#monad_valid(val) ⇒ Object
41 42 43 |
# File 'lib/typed_contracts.rb', line 41 def monad_valid(val) [Kleisli::Either::Left, Kleisli::Either::Right].include?(val.class) end |
#to_s ⇒ Object
37 38 39 |
# File 'lib/typed_contracts.rb', line 37 def to_s "EitherOf[#{@vals.first}, #{@vals.last}]" end |
#valid?(val) ⇒ Boolean
45 46 47 48 49 50 51 |
# File 'lib/typed_contracts.rb', line 45 def valid?(val) return false unless monad_valid(val) && @vals.size == 2 left_valid, = Contract.valid?(val.left, @vals.first) right_valid, = Contract.valid?(val.right, @vals.last) (val.left.nil? && right_valid) || (val.right.nil? && left_valid) end |