Class: AwesomeLoader::Autoloader

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(root_depth: DEFAULT_ROOT_DEPTH, root_path: Dir.pwd, root_module: Object, eager_load: false) ⇒ Autoloader

Initialize a new AwesomeLoader::Autoloader.

Parameters:

  • (defaults to: DEFAULT_ROOT_DEPTH)

    Tells AwesomeLoader to start creating Modules for dirs after this level (default 2)

  • (defaults to: Dir.pwd)

    Path to root of the application (default Dir.pwd)

  • (defaults to: Object)

    Module to load your modules into (default Object). You'll probably always want to keep the default.

  • (defaults to: false)

    Make sure all files get loaded by the time the block finishes (default false)



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_depthInteger (readonly)

Returns root depth used for all paths unless otherwise specified.

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_loadBoolean (readonly)

Returns whether or not to automatically load all files once they're defined.

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_moduleModule (readonly)

Returns the root ruby Module.

Returns:

  • the root ruby Module



48
49
50
# File 'lib/awesome_loader/autoloader.rb', line 48

def root_module
  @root_module
end

#root_pathPathname (readonly)

Returns the application root.

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)

Parameters:

  • A glob pattern as an array.

Returns:

  • returns self, so you can chain calls



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.

Parameters:

  • A glob pattern as an array.

Returns:

  • returns self, so you can chain calls



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