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

eager_load_paths[W]

@!attribute [w] eager_load_paths

model_paths[W]

@!attribute [w] model_paths

Public Class Methods

require_all() click to toggle source

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() click to toggle source

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

all_file_paths() click to toggle source

@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
model_file_paths() click to toggle source

@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

conditions_for(path) click to toggle source

@see sort

# File lib/wallaby/preloader.rb, line 68
def conditions_for(path)
  [indexed(path) || model_paths.length, path]
end
indexed(path) click to toggle source

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
sort(file_paths) click to toggle source

All files need to be sorted in the following orders:

  1. {Wallaby::Configuration#model_paths} order

  2. 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