Class: Solargraph::RbsMap

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/solargraph/rbs_map.rb,
lib/solargraph/rbs_map/core_map.rb,
lib/solargraph/rbs_map/core_fills.rb,
lib/solargraph/rbs_map/stdlib_map.rb,
lib/solargraph/rbs_map/conversions.rb

Direct Known Subclasses

StdlibMap

Defined Under Namespace

Modules: CoreFills Classes: Conversions, CoreMap, StdlibMap

Constant Summary collapse

@@rbs_maps_hash =
{}

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

logger

Constructor Details

#initialize(library, version = nil, rbs_collection_config_path: nil, rbs_collection_paths: []) ⇒ RbsMap



27
28
29
30
31
32
33
34
35
36
# File 'lib/solargraph/rbs_map.rb', line 27

def initialize library, version = nil, rbs_collection_config_path: nil, rbs_collection_paths: []
  if rbs_collection_config_path.nil? && !rbs_collection_paths.empty?
    raise 'Please provide rbs_collection_config_path if you provide rbs_collection_paths'
  end
  @library = library
  @version = version
  @rbs_collection_config_path = rbs_collection_config_path
  @rbs_collection_paths = rbs_collection_paths
  add_library loader, library, version
end

Instance Attribute Details

#libraryObject (readonly)

Returns the value of attribute library.



18
19
20
# File 'lib/solargraph/rbs_map.rb', line 18

def library
  @library
end

#rbs_collection_config_pathObject (readonly)

Returns the value of attribute rbs_collection_config_path.



22
23
24
# File 'lib/solargraph/rbs_map.rb', line 22

def rbs_collection_config_path
  @rbs_collection_config_path
end

#rbs_collection_pathsObject (readonly)

Returns the value of attribute rbs_collection_paths.



20
21
22
# File 'lib/solargraph/rbs_map.rb', line 20

def rbs_collection_paths
  @rbs_collection_paths
end

Class Method Details

.from_gemspec(gemspec, rbs_collection_path, rbs_collection_config_path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/solargraph/rbs_map.rb', line 71

def self.from_gemspec gemspec, rbs_collection_path, rbs_collection_config_path
  rbs_map = RbsMap.new(gemspec.name, gemspec.version,
                       rbs_collection_paths: [rbs_collection_path].compact,
                       rbs_collection_config_path: rbs_collection_config_path)
  return rbs_map if rbs_map.resolved?

  # try any version of the gem in the collection

  RbsMap.new(gemspec.name, nil,
             rbs_collection_paths: [rbs_collection_path].compact,
             rbs_collection_config_path: rbs_collection_config_path)
end

.load(library) ⇒ RbsMap



117
118
119
# File 'lib/solargraph/rbs_map.rb', line 117

def self.load library
  @@rbs_maps_hash[library] ||= RbsMap.new(library)
end

Instance Method Details

#cache_keyObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/solargraph/rbs_map.rb', line 46

def cache_key
  @hextdigest ||= begin
    data = nil
    if rbs_collection_config_path
      lockfile_path = RBS::Collection::Config.to_lockfile_path(Pathname.new(rbs_collection_config_path))
      if lockfile_path.exist?
        collection_config = RBS::Collection::Config.from_path lockfile_path
        gem_config = collection_config.gem(library)
        data = gem_config&.to_s
      end
    end
    if data.nil? || data.empty?
      if resolved?
        # definitely came from the gem itself and not elsewhere -

        # only one version per gem

        'gem-export'
      else
        'unresolved'
      end
    else
      Digest::SHA1.hexdigest(data)
    end
  end
end

#path_pin(path, klass = Pin::Base) ⇒ generic<T>?



91
92
93
94
# File 'lib/solargraph/rbs_map.rb', line 91

def path_pin path, klass = Pin::Base
  pin = pins.find { |p| p.path == path }
  pin if pin&.is_a?(klass)
end

#path_pins(path) ⇒ Array<Pin::Base>



98
99
100
# File 'lib/solargraph/rbs_map.rb', line 98

def path_pins path
  pins.select { |p| p.path == path }
end

#pinsObject



83
84
85
# File 'lib/solargraph/rbs_map.rb', line 83

def pins
  @pins ||= resolved? ? conversions.pins : []
end

#repositoryObject



106
107
108
109
110
111
112
113
# File 'lib/solargraph/rbs_map.rb', line 106

def repository
  @repository ||= RBS::Repository.new(no_stdlib: false).tap do |repo|
    @rbs_collection_paths.each do |dir|
      dir_path = Pathname.new(dir)
      repo.add(dir_path) if dir_path.exist? && dir_path.directory?
    end
  end
end

#resolved?Boolean



102
103
104
# File 'lib/solargraph/rbs_map.rb', line 102

def resolved?
  @resolved
end