Module: Zeroload
- Defined in:
- lib/Zeroload.rb,
lib/Zeroload/Version.rb,
lib/Zeroload/no_patch.rb
Overview
Register a module to be zero-loaded.
Use Module#zeroload! instead.
Constant Summary collapse
- Patch =
true- Registry =
{}
- Name =
Module.instance_method(:name)
- Version =
Gem::Version.new("0.0.2")
Class Method Summary collapse
- .module(mod, directory = nil) ⇒ Object
- .patch! ⇒ Object
-
.preload!(mod = nil, recursive = true) ⇒ Object
Preload all zeroloaded constants for all or a given module.
Class Method Details
.module(mod, directory = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/Zeroload.rb', line 63 def self.module(mod, directory=nil) directory ||= caller_locations.first.absolute_path.sub(/\.[^.]*\z/, "".freeze) mod_name = Name.bind(mod).call rescue nil # some modules don't have a name Registry[mod_name] ||= {} if mod_name Dir.glob("#{directory}/*.{rb,so,bundle,dll}") do |path| name = File.basename(path) if /\A[A-Z]/ =~ name name = :"#{name.sub(/\.[^.]*\z/, "".freeze)}" warn "#{mod} autoloads #{mod}::#{name} from #{path}" if $VERBOSE Registry[mod_name][name] = path if mod_name mod.autoload name, path end end mod end |
.patch! ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/Zeroload.rb', line 38 def self.patch! ::Module.class_eval do def zeroload!(directory=nil, *args) directory ||= caller_locations.first.absolute_path.sub(/\.[^.]*\z/, "") Zeroload.module(self, directory, *args) end end self end |
.preload!(mod = nil, recursive = true) ⇒ Object
Note:
Module#constants properly lists all constants, even if the file has not yet been loaded.
Preload all zeroloaded constants for all or a given module.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/Zeroload.rb', line 97 def self.preload!(mod=nil, recursive=true) if mod nested_mod_name = Name.bind(mod).call rescue nil # some modules don't have a name preload = Registry[nested_mod_name] if preload preload.each_key do |name| loaded = mod.const_get(name) preload!(loaded, true) if recursive end end else Registry.dup.each do |nested_mod_name, zeroloaded| loaded = Object.const_get(nested_mod_name) # Object.const_get does not like Symbols for nested modules. Zeroload.preload!(loaded, true) end end nil end |