class Wallaby::Preloader
Preload files for eager load paths.
As Wallaby
is built upon the {Wallaby::Map} which will not be completed until all models and decorators/controllers/servicers/authorizers/paginators are loaded. Therefore, when Rails app is initialized, all files under eager load paths (mostly `app/*` folders), especially the files under `app/models`, need to be loaded before everything else.
Attributes
@!attribute [w] eager_load_paths
@!attribute [w] model_paths
Public Class Methods
Require all files @see all_file_paths
# File lib/wallaby/preloader.rb, line 16 def self.require_all new.all_file_paths.each(&method(:require_dependency)) end
Require models under {Wallaby::Configuration#model_paths} @see model_file_paths
# File lib/wallaby/preloader.rb, line 22 def self.require_models new.model_file_paths.each(&method(:require_dependency)) end
Public Instance Methods
@return [Array<String>] all files under Rails.configuration.eager_load_paths
# File lib/wallaby/preloader.rb, line 27 def all_file_paths sort all_eager_load_file_paths end
@return [Array<String>] model files under {Wallaby::Configuration#model_paths}
# File lib/wallaby/preloader.rb, line 32 def model_file_paths sort(all_eager_load_file_paths).select(&method(:indexed)) end
Private Instance Methods
@see sort
# File lib/wallaby/preloader.rb, line 68 def conditions_for(path) [indexed(path) || model_paths.length, path] end
Check if the path is in the {Wallaby::Configuration#model_paths}
# File lib/wallaby/preloader.rb, line 73 def indexed(path) model_paths.index(&path.method(:include?)) end
All files need to be sorted in the following orders:
-
{Wallaby::Configuration#model_paths} order
-
Alphabet order
# File lib/wallaby/preloader.rb, line 63 def sort(file_paths) file_paths.sort { |p1, p2| conditions_for(p1) <=> conditions_for(p2) } end