Module: Kernel
- Defined in:
- lib/require-hooks/mode/kernel_patch.rb
Overview
Patch Kernel to hijack require/require_relative/load
Class Method Summary collapse
- .load(path, wrap = false) ⇒ Object
- .load_without_require_hooks ⇒ Object
- .require(path) ⇒ Object
- .require_relative(path) ⇒ Object
- .require_relative_without_require_hooks ⇒ Object
- .require_without_require_hooks ⇒ Object
Class Method Details
.load(path, wrap = false) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/require-hooks/mode/kernel_patch.rb', line 270 def load(path, wrap = false) if wrap warn "RequireHooks does not support `load(smth, wrap: ...)`. Falling back to original `Kernel#load`" if RequireHooks.print_warnings return load_without_require_hooks(path, wrap) end path = path.to_path if path.respond_to?(:to_path) raise TypeError unless path.respond_to?(:to_str) path = path.to_str raise TypeError unless path.is_a?(::String) realpath = if path =~ /^\.\.?\// path else RequireHooks::KernelPatch::Features.feature_path(path, implitic_ext: false) end return load_without_require_hooks(path, wrap) unless realpath RequireHooks::KernelPatch.load(realpath) rescue Errno::ENOENT, Errno::EACCES raise LoadError, "cannot load such file -- #{path}" rescue LoadError => e warn "RuquireHooks failed to load '#{path}': #{e.}" if RequireHooks.print_warnings load_without_require_hooks(path) end |
.load_without_require_hooks ⇒ Object
269 |
# File 'lib/require-hooks/mode/kernel_patch.rb', line 269 alias_method :load_without_require_hooks, :load |
.require(path) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/require-hooks/mode/kernel_patch.rb', line 198 def require(path) path = path.to_path if path.respond_to?(:to_path) raise TypeError unless path.respond_to?(:to_str) path = path.to_str raise TypeError unless path.is_a?(::String) realpath = nil feature = path # if extname == ".rb" => lookup feature -> resolve feature -> load # if extname != ".rb" => append ".rb" - lookup feature -> resolve feature -> lookup orig (no ext) -> resolve orig (no ext) -> load if File.extname(path) != ".rb" realpath = RequireHooks::KernelPatch::Features.feature_path(path + ".rb") if realpath feature = path + ".rb" end end realpath ||= RequireHooks::KernelPatch::Features.feature_path(path) return require_without_require_hooks(path) unless realpath ctx = RequireHooks.context_for(realpath) return require_without_require_hooks(path) if ctx.empty? return false if $LOADED_FEATURES.include?(realpath) RequireHooks::KernelPatch::Features::LOCK.lock_feature(feature) do |loaded| return false if loaded $LOADED_FEATURES << realpath RequireHooks::KernelPatch.load(realpath, ctx: ctx) true end rescue LoadError => e $LOADED_FEATURES.delete(realpath) if realpath warn "RequireHooks failed to require '#{path}': #{e.}" if RequireHooks.print_warnings require_without_require_hooks(path) rescue Errno::ENOENT, Errno::EACCES raise LoadError, "cannot load such file -- #{path}" rescue $LOADED_FEATURES.delete(realpath) if realpath raise end |
.require_relative(path) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/require-hooks/mode/kernel_patch.rb', line 248 def require_relative(path) path = path.to_path if path.respond_to?(:to_path) raise TypeError unless path.respond_to?(:to_str) path = path.to_str raise TypeError unless path.is_a?(::String) return require(path) if Pathname.new(path).absolute? loc = caller_locations(1..1).first from = loc.absolute_path || loc.path || File.join(Dir.pwd, "main") realpath = File.absolute_path( File.join( File.dirname(File.absolute_path(from)), path ) ) require(realpath) end |
.require_relative_without_require_hooks ⇒ Object
247 |
# File 'lib/require-hooks/mode/kernel_patch.rb', line 247 alias_method :require_relative_without_require_hooks, :require_relative |
.require_without_require_hooks ⇒ Object
196 |
# File 'lib/require-hooks/mode/kernel_patch.rb', line 196 alias_method :require_without_require_hooks, :require |