Class: AwesomeLoader::Autoloader
- Inherits:
-
Object
- Object
- AwesomeLoader::Autoloader
- Defined in:
- lib/awesome_loader/autoloader.rb
Overview
The autoloader. Normally it's used indirectly through AwesomeLoader.autoload, but you can use it directly if you like:
AwesomeLoader::Autoloader.new(root_depth: 2).
paths(['app', 'routes', '**', '*.rb'], root_depth: 1).
paths(['app', '**', '*.rb']).
finalize!
Constant Summary collapse
- DEFAULT_ROOT_DEPTH =
The default root_dept for new instances
2
Instance Attribute Summary collapse
-
#default_root_depth ⇒ Integer
readonly
Root depth used for all paths unless otherwise specified.
-
#eager_load ⇒ Boolean
readonly
Whether or not to automatically load all files once they're defined.
-
#root_module ⇒ Module
readonly
The root ruby Module.
-
#root_path ⇒ Pathname
readonly
The application root.
Instance Method Summary collapse
-
#finalize! ⇒ Object
Perform any final operations or cleanup.
-
#initialize(root_depth: DEFAULT_ROOT_DEPTH, root_path: Dir.pwd, root_module: Object, eager_load: false) ⇒ Autoloader
constructor
Initialize a new AwesomeLoader::Autoloader.
-
#paths(glob, root_depth: default_root_depth) ⇒ AwesomeLoader::Autoloader
Set a glob pattern of files to be autoloaded.
-
#require(glob) ⇒ AwesomeLoader::Autoloader
Same as Ruby's built-in require, except that it accepts a blob and requires all matching files.
Constructor Details
#initialize(root_depth: DEFAULT_ROOT_DEPTH, root_path: Dir.pwd, root_module: Object, eager_load: false) ⇒ Autoloader
Initialize a new AwesomeLoader::Autoloader.
63 64 65 66 67 |
# File 'lib/awesome_loader/autoloader.rb', line 63 def initialize(root_depth: DEFAULT_ROOT_DEPTH, root_path: Dir.pwd, root_module: Object, eager_load: false) @root_path, @root_module = Pathname.new(root_path.to_s), root_module @default_root_depth, @eager_load = root_depth, eager_load @all_files = Set.new end |
Instance Attribute Details
#default_root_depth ⇒ Integer (readonly)
Returns root depth used for all paths unless otherwise specified.
44 45 46 |
# File 'lib/awesome_loader/autoloader.rb', line 44 def default_root_depth @default_root_depth end |
#eager_load ⇒ Boolean (readonly)
Returns whether or not to automatically load all files once they're defined.
50 51 52 |
# File 'lib/awesome_loader/autoloader.rb', line 50 def eager_load @eager_load end |
#root_module ⇒ Module (readonly)
Returns the root ruby Module.
48 49 50 |
# File 'lib/awesome_loader/autoloader.rb', line 48 def root_module @root_module end |
#root_path ⇒ Pathname (readonly)
Returns the application root.
46 47 48 |
# File 'lib/awesome_loader/autoloader.rb', line 46 def root_path @root_path end |
Instance Method Details
#finalize! ⇒ Object
Perform any final operations or cleanup. If eager_load is true, this is where they're loaded.
110 111 112 113 114 |
# File 'lib/awesome_loader/autoloader.rb', line 110 def finalize! all_files.each { |f| Kernel.require f } if eager_load all_files.clear self end |
#paths(glob, root_depth: default_root_depth) ⇒ AwesomeLoader::Autoloader
Set a glob pattern of files to be autoloaded.
autoloader.paths %w(app models ** *.rb)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/awesome_loader/autoloader.rb', line 78 def paths(glob, root_depth: default_root_depth) builder = ModuleBuilder.new(root_depth: root_depth, root_module: root_module) blank_str = '' Dir.glob(File.join root_path.to_s, *glob).each do |full_path| next if all_files.include? full_path all_files << full_path rel_path = full_path.sub root_path.to_s, blank_str dir_path, file_name = File.split rel_path const_name = Utils.camelize file_name[0, file_name.size - 3] builder.module(dir_path).autoload const_name, full_path end self end |
#require(glob) ⇒ AwesomeLoader::Autoloader
Same as Ruby's built-in require, except that it accepts a blob and requires all matching files. The require is immediate, the path is relative to the root_path, and no dir modules are created.
100 101 102 103 104 105 |
# File 'lib/awesome_loader/autoloader.rb', line 100 def require(glob) Dir.glob(File.join root_path.to_s, *glob).each do |file| Kernel.require file end self end |