Class: RBS::Repository

Inherits:
Object
  • Object
show all
Defined in:
sig/repository.rbs,
lib/rbs/repository.rb

Overview

Set of RBS repositories.

A repository object can handle multiple repository roots.

repo = RBS::Repository.new()
repo.add(Pathname("vendor/rbs/gem-rbs"))
repo.add(Pathname("vendor/rbs/internal-rbs"))
repo.add(Pathname("vendor/rbs/definitely-rbs"))

repo.lookup("minitest", "2.1.3") # => Pathname or nil

If one gem version can resolve to several directories, the last added dir wins.

Defined Under Namespace

Classes: GemRBS, VersionPath

Constant Summary collapse

DEFAULT_STDLIB_ROOT =

Returns:

  • (Pathname)
Pathname(_ = __dir__) + "../../stdlib"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(no_stdlib: false) ⇒ Repository

An optional keyword argument no_stdlib is to skip adding directory for stdlib classes. Passing truthy value will skip loading stdlib. (You can add the stdlib root by yourself.)

Parameters:

  • no_stdlib: (Boolean) (defaults to: false)


69
70
71
72
73
74
75
76
# File 'sig/repository.rbs', line 69

def initialize(no_stdlib: false)
  @dirs = []
  @gems = {}

  unless no_stdlib
    add(DEFAULT_STDLIB_ROOT)
  end
end

Instance Attribute Details

#dirsArray[Pathname] (readonly)

Array of _root dir_s of repositories.

Returns:

  • (Array[Pathname])


58
59
60
# File 'sig/repository.rbs', line 58

def dirs
  @dirs
end

#gemsHash[String, GemRBS] (readonly)

Returns the value of attribute gems.

Returns:



72
73
74
# File 'lib/rbs/repository.rb', line 72

def gems
  @gems
end

Class Method Details

.defaultinstance

Returns:

  • (instance)


83
84
85
# File 'lib/rbs/repository.rb', line 83

def self.default
  new()
end

.find_best_version(version, candidates) ⇒ Gem::Version

Parameters:

  • version (Gem::Version, nil)
  • candidates (Array[Gem::Version])

Returns:

  • (Gem::Version)


87
88
89
90
91
92
93
94
95
96
# File 'lib/rbs/repository.rb', line 87

def self.find_best_version(version, candidates)
  candidates = candidates.sort
  return candidates.last || raise unless version

  if v = candidates.reverse.bsearch {|v| v <= version ? true : false }
    v
  else
    candidates.first or raise
  end
end

Instance Method Details

#add(dir) ⇒ void

This method returns an undefined value.

Add new root dir to the repository set. If two repository dirs have exactly same gem-version definitions, the latter overwrites the prior.

Parameters:

  • dir (Pathname)


74
75
76
77
78
79
80
81
82
# File 'sig/repository.rbs', line 74

def add(dir)
  dirs << dir

  dir.each_child(false) do |child|
    gem_name = child.to_s
    gem_rbs = (gems[gem_name] ||= GemRBS.new(name: gem_name))
    gem_rbs.paths << dir + child
  end
end

#lookup(gem, version) ⇒ Pathname?

Returns a directory for given gem name and version. version can be nil for any version.

If the given gem cannot be found, it returns nil.

Parameters:

  • gem (String)
  • version (String, nil)

Returns:

  • (Pathname, nil)


81
82
83
84
# File 'sig/repository.rbs', line 81

def lookup(gem, version)
  _, set = lookup_path(gem, version)
  set&.path
end

#lookup_path(gem, version) ⇒ [GemRBS, VersionPath]?

Parameters:

  • gem (String)
  • version (String, nil)

Returns:



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rbs/repository.rb', line 113

def lookup_path(gem, version)
  if gem_rbs = gems[gem]
    unless gem_rbs.empty?
      set = if version and v = Gem::Version.create(version)&.release
        gem_rbs.find_best_version(v)
      else
        gem_rbs.latest_version
      end

      [gem_rbs, set]
    end
  end
end