Class: Sorbet::Private::ConstantLookupCache

Inherits:
Object
  • Object
show all
Defined in:
lib/constant_cache.rb

Overview

This class walks global namespace to find all modules and discover all of their names. At the time you ask for it it, it takes a “spashot” of the world. If new modules were defined after an instance of this class was created, they won’t be visible through a previously returned instance.

Defined Under Namespace

Classes: ConstantEntry

Constant Summary collapse

DEPRECATED_CONSTANTS =

We won’t be able to see if someone mankeypatches these.

[
  '::Bignum',
  '::Config',
  '::Data',
  '::FALSE',
  '::Fixnum',
  '::NIL',
  '::TRUE',
  '::TimeoutError',
  'ERB::Compiler::SimpleScanner2',
  'Net::OpenSSL',
  'Object::Bignum',
  'Object::Config',
  'Object::Data',
  'Object::FALSE',
  'Object::Fixnum',
  'Object::NIL',
  'Object::TRUE',
  'Object::TimeoutError',
  'OpenSSL::Cipher::Cipher',
  'OpenSSL::Cipher::Digest',
  'OpenSSL::Digest::Cipher',
  'OpenSSL::Digest::Digest',
  'OpenSSL::SSL::SSLContext::METHODS',
  'Pry::Platform',
  'Pry::Prompt::MAP',
  'Sequel::BeforeHookFailed',
  'Sequel::Database::ResetIdentifierMangling',
  'Sequel::Error::AdapterNotFound',
  'Sequel::Error::InvalidOperation',
  'Sequel::Error::InvalidValue',
  'Sequel::Error::PoolTimeoutError',
  'Sequel::Error::Rollback',
  'Sequel::Model::ANONYMOUS_MODEL_CLASSES',
  'Sequel::Model::ANONYMOUS_MODEL_CLASSES_MUTEX',
  'Sequel::UnbindDuplicate',
  'Sequel::Unbinder',
  'YARD::Parser::Ruby::Legacy::RipperParser',
].freeze

Instance Method Summary collapse

Constructor Details

#initializeConstantLookupCache

Returns a new instance of ConstantLookupCache.



56
57
58
59
60
61
62
63
64
65
# File 'lib/constant_cache.rb', line 56

def initialize
  @all_constants = {}
  dfs_module(Object, nil, @all_constants, nil)
  Sorbet::Private::Status.done
  @consts_by_name = {}
  @all_constants.each_value do |struct|
    fill_primary_name(struct)
    @consts_by_name[struct.primary_name] = struct.const
  end
end

Instance Method Details

#all_module_aliasesObject



83
84
85
86
87
88
89
90
91
# File 'lib/constant_cache.rb', line 83

def all_module_aliases
  ret = {}

  @all_constants.map do |_key, struct|
    next if struct.nil? || !Sorbet::Private::RealStdlib.real_is_a?(struct.const, Module) || struct.aliases.size < 2
    ret[struct.primary_name] = struct.aliases.reject {|name| name == struct.primary_name}
  end
  ret
end

#all_module_namesObject



67
68
69
70
71
72
73
# File 'lib/constant_cache.rb', line 67

def all_module_names
  ret = @all_constants.select {|_k, v| Sorbet::Private::RealStdlib.real_is_a?(v.const, Module)}.map do |_key, struct|
    raise "should never happen" if !struct.primary_name
    struct.primary_name
  end
  ret
end

#all_named_modulesObject



75
76
77
78
79
80
81
# File 'lib/constant_cache.rb', line 75

def all_named_modules
  ret = @all_constants.select {|_k, v| Sorbet::Private::RealStdlib.real_is_a?(v.const, Module)}.map do |_key, struct|
    raise "should never happen" if !struct.primary_name
    struct.const
  end
  ret
end

#class_by_name(name) ⇒ Object



93
94
95
# File 'lib/constant_cache.rb', line 93

def class_by_name(name)
  @consts_by_name[name]
end

#name_by_class(klass) ⇒ Object



97
98
99
# File 'lib/constant_cache.rb', line 97

def name_by_class(klass)
  @all_constants[Sorbet::Private::RealStdlib.real_object_id(klass)]&.primary_name
end