Module: T::Types::Simple::Private::Pool
- Defined in:
- lib/types/types/simple.rb
Constant Summary collapse
- CACHE_FROZEN_OBJECTS =
begin ObjectSpace::WeakMap.new[1] = 1 true # Ruby 2.7 and newer rescue ArgumentError # Ruby 2.6 and older false end
Class Method Summary collapse
Class Method Details
.type_for_module(mod) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/types/types/simple.rb', line 97 def self.type_for_module(mod) cached = @cache[mod] return cached if cached type = if mod == ::Array TypedArray::Untyped::Private::INSTANCE elsif mod == ::Hash TypedHash::Untyped::Private::INSTANCE elsif mod == ::Enumerable TypedEnumerable::Untyped.new elsif mod == ::Enumerator TypedEnumerator::Untyped.new elsif mod == ::Range TypedRange::Untyped.new elsif !Object.autoload?(:Set) && Object.const_defined?(:Set) && mod == ::Set TypedSet::Untyped.new else # ideally we would have a case mapping from ::Class -> T::Class and # ::Module -> T::Module here but for backwards compatibility we # don't have that, and instead have a special case in subtype_of_single? Simple.new(mod) end # Unfortunately, we still need to check if the module is frozen, # since on 2.6 and older WeakMap adds a finalizer to the key that is added # to the map, so that it can clear the map entry when the key is # garbage collected. # For a frozen object, though, adding a finalizer is not a valid # operation, so this still raises if `mod` is frozen. if CACHE_FROZEN_OBJECTS || (!mod.frozen? && !type.frozen?) @cache[mod] = type end type end |