Class: RBS::Repository

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

Defined Under Namespace

Classes: GemRBS

Constant Summary collapse

DEFAULT_STDLIB_ROOT =
Pathname(_ = __dir__) + "../../stdlib"
VersionPath =
_ = Struct.new(:gem, :version, :path, keyword_init: true)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(no_stdlib: false) ⇒ Repository

Returns a new instance of Repository.



77
78
79
80
81
82
83
84
# File 'lib/rbs/repository.rb', line 77

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

  unless no_stdlib
    add(DEFAULT_STDLIB_ROOT)
  end
end

Instance Attribute Details

#dirsObject (readonly)

Returns the value of attribute dirs.



74
75
76
# File 'lib/rbs/repository.rb', line 74

def dirs
  @dirs
end

#gemsObject (readonly)

Returns the value of attribute gems.



75
76
77
# File 'lib/rbs/repository.rb', line 75

def gems
  @gems
end

Class Method Details

.defaultObject



86
87
88
89
90
# File 'lib/rbs/repository.rb', line 86

def self.default
  new().tap do |repo|
    repo.add(DEFAULT_STDLIB_ROOT)
  end
end

Instance Method Details

#add(dir) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/rbs/repository.rb', line 92

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) ⇒ Object



102
103
104
105
# File 'lib/rbs/repository.rb', line 102

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

#lookup_path(gem, version) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rbs/repository.rb', line 107

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

      [gem_rbs, set]
    end
  end
end