Class: TypedContracts::MaybeOf

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

Direct Known Subclasses

ToMaybeOf, ToPresenceMaybeOf

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*vals) ⇒ MaybeOf

Returns a new instance of MaybeOf.



13
14
15
# File 'lib/typed_contracts.rb', line 13

def initialize(*vals)
  @vals = vals
end

Instance Attribute Details

#valsObject (readonly)

Returns the value of attribute vals.



12
13
14
# File 'lib/typed_contracts.rb', line 12

def vals
  @vals
end

Instance Method Details

#to_sObject



17
18
19
# File 'lib/typed_contracts.rb', line 17

def to_s
  "MaybeOf[#{@vals.first}]"
end

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/typed_contracts.rb', line 21

def valid?(val)
  return false unless val.is_a?(Kleisli::Maybe) && @vals.size == 1

  inner_valid, = Contract.valid?(val.value, @vals.first)
  inner_valid || val.value.nil?
end