Class: TypedContracts::EitherOf

Inherits:
Contracts::CallableClass
  • Object
show all
Defined in:
lib/typed_contracts.rb

Instance Method Summary collapse

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_sObject



37
38
39
# File 'lib/typed_contracts.rb', line 37

def to_s
  "EitherOf[#{@vals.first}, #{@vals.last}]"
end

#valid?(val) ⇒ Boolean

Returns:

  • (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