Class: RBS::Repository
- Inherits:
-
Object
- Object
- RBS::Repository
- 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 =
Pathname(_ = __dir__) + "../../stdlib"
Instance Attribute Summary collapse
-
#dirs ⇒ Array[Pathname]
readonly
Array of _root dir_s of repositories.
-
#gems ⇒ Hash[String, GemRBS]
readonly
Returns the value of attribute gems.
Class Method Summary collapse
Instance Method Summary collapse
-
#add(dir) ⇒ void
Add new root dir to the repository set.
-
#initialize(no_stdlib: false) ⇒ Repository
constructor
An optional keyword argument
no_stdlibis to skip adding directory for stdlib classes. -
#lookup(gem, version) ⇒ Pathname?
Returns a directory for given
gemname andversion. - #lookup_path(gem, version) ⇒ [GemRBS, VersionPath]?
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.)
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
#dirs ⇒ Array[Pathname] (readonly)
Array of _root dir_s of repositories.
58 59 60 |
# File 'sig/repository.rbs', line 58 def dirs @dirs end |
#gems ⇒ Hash[String, GemRBS] (readonly)
Returns the value of attribute gems.
72 73 74 |
# File 'lib/rbs/repository.rb', line 72 def gems @gems end |
Class Method Details
.default ⇒ instance
83 84 85 |
# File 'lib/rbs/repository.rb', line 83 def self.default new() end |
.find_best_version(version, candidates) ⇒ 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.
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.
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]?
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 |