Class: Mayak::WeakRef

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/mayak/weak_ref.rb

Defined Under Namespace

Classes: EmptyReferenceError

Constant Summary collapse

Value =
type_member
@@__map =
T.let(::ObjectSpace::WeakMap.new, ::ObjectSpace::WeakMap)

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ WeakRef

Returns a new instance of WeakRef.



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

def initialize(value)
  @@__map[self] = value
end

Instance Method Details

#derefObject



31
32
33
34
35
# File 'lib/mayak/weak_ref.rb', line 31

def deref
  Mayak::Monads::Maybe::Some[Value].new(deref!)
rescue
  Mayak::Monads::Maybe::None[Value].new
end

#deref!Object



22
23
24
25
26
27
28
# File 'lib/mayak/weak_ref.rb', line 22

def deref!
  if @@__map.key?(self)
    @@__map[self]
  else
    raise EmptyReferenceError.new("Reference is empty")
  end
end