Class: RBS::EnvironmentLoader
- Inherits:
-
Object
- Object
- RBS::EnvironmentLoader
- Defined in:
- lib/rbs/environment_loader.rb
Defined Under Namespace
Classes: GemPath, LibraryPath, UnknownLibraryNameError
Constant Summary collapse
- STDLIB_ROOT =
Pathname(__dir__) + "../../stdlib"
Instance Attribute Summary collapse
-
#gem_vendor_path ⇒ Object
readonly
Returns the value of attribute gem_vendor_path.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
-
#stdlib_root ⇒ Object
readonly
Returns the value of attribute stdlib_root.
Class Method Summary collapse
Instance Method Summary collapse
- #add(path: nil, library: nil) ⇒ Object
- #each_signature(path = nil, immediate: true, &block) ⇒ Object
- #gem?(name, version) ⇒ Boolean
-
#initialize(stdlib_root: STDLIB_ROOT, gem_vendor_path: nil) ⇒ EnvironmentLoader
constructor
A new instance of EnvironmentLoader.
- #load(env:) ⇒ Object
- #no_builtin! ⇒ Object
- #no_builtin? ⇒ Boolean
- #stdlib?(name) ⇒ Boolean
Constructor Details
#initialize(stdlib_root: STDLIB_ROOT, gem_vendor_path: nil) ⇒ EnvironmentLoader
Returns a new instance of EnvironmentLoader.
27 28 29 30 31 32 |
# File 'lib/rbs/environment_loader.rb', line 27 def initialize(stdlib_root: STDLIB_ROOT, gem_vendor_path: nil) @stdlib_root = stdlib_root @gem_vendor_path = gem_vendor_path @paths = [] @no_builtin = false end |
Instance Attribute Details
#gem_vendor_path ⇒ Object (readonly)
Returns the value of attribute gem_vendor_path.
17 18 19 |
# File 'lib/rbs/environment_loader.rb', line 17 def gem_vendor_path @gem_vendor_path end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
15 16 17 |
# File 'lib/rbs/environment_loader.rb', line 15 def paths @paths end |
#stdlib_root ⇒ Object (readonly)
Returns the value of attribute stdlib_root.
16 17 18 |
# File 'lib/rbs/environment_loader.rb', line 16 def stdlib_root @stdlib_root end |
Class Method Details
.gem_sig_path(name, version) ⇒ Object
21 22 23 24 25 |
# File 'lib/rbs/environment_loader.rb', line 21 def self.gem_sig_path(name, version) Pathname(Gem::Specification.find_by_name(name, version).gem_dir) + "sig" rescue Gem::MissingSpecError nil end |
.parse_library(lib) ⇒ Object
52 53 54 |
# File 'lib/rbs/environment_loader.rb', line 52 def self.parse_library(lib) lib.split(/:/) end |
Instance Method Details
#add(path: nil, library: nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rbs/environment_loader.rb', line 34 def add(path: nil, library: nil) case when path @paths << path when library name, version = self.class.parse_library(library) case when !version && path = stdlib?(name) @paths << LibraryPath.new(name: name, path: path) when (path = gem?(name, version)) @paths << GemPath.new(name: name, version: version, path: path) else raise UnknownLibraryNameError.new(name: library) end end end |
#each_signature(path = nil, immediate: true, &block) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rbs/environment_loader.rb', line 78 def each_signature(path = nil, immediate: true, &block) if block_given? if path case when path.file? if path.extname == ".rbs" || immediate yield path end when path.directory? path.children.each do |child| each_signature child, immediate: false, &block end end else paths.each do |path| case path when Pathname each_signature path, immediate: immediate, &block when LibraryPath each_signature path.path, immediate: immediate, &block when GemPath each_signature path.path, immediate: immediate, &block end end end else enum_for :each_signature, path, immediate: immediate end end |
#gem?(name, version) ⇒ Boolean
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rbs/environment_loader.rb', line 65 def gem?(name, version) if gem_vendor_path # Try vendored RBS first gem_dir = gem_vendor_path + name if gem_dir.directory? return gem_dir end end # Try ruby gem library self.class.gem_sig_path(name, version) end |
#load(env:) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rbs/environment_loader.rb', line 116 def load(env:) signature_files = [] unless no_builtin? signature_files.push(*each_signature(stdlib_root + "builtin")) end each_signature do |path| signature_files.push path end signature_files.each do |file| buffer = Buffer.new(name: file.to_s, content: file.read) env.buffers.push(buffer) Parser.parse_signature(buffer).each do |decl| env << decl end end end |
#no_builtin! ⇒ Object
108 109 110 |
# File 'lib/rbs/environment_loader.rb', line 108 def no_builtin! @no_builtin = true end |
#no_builtin? ⇒ Boolean
112 113 114 |
# File 'lib/rbs/environment_loader.rb', line 112 def no_builtin? @no_builtin end |
#stdlib?(name) ⇒ Boolean
56 57 58 59 60 61 62 63 |
# File 'lib/rbs/environment_loader.rb', line 56 def stdlib?(name) if stdlib_root path = stdlib_root + name if path.directory? path end end end |