Class: ConsciousConcern::EagerLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/conscious_concern/eager_loader.rb

Overview

Recursively finds Ruby Classes in a given directory.

Class Method Summary collapse

Class Method Details

.load_class_at_path(path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/conscious_concern/eager_loader.rb', line 35

def self.load_class_at_path(path)
  # Silence harmless 'already initialized constant' warnings that might
  # occur due to Rails autoload having previously loaded some constants.
  # Use load if require returns false for reloads in Rails development.
  silence_warnings do
    load path
    puts "eager loaded class at #{path}" if debug
  end
rescue LoadError, StandardError, TypeError => e
  return puts(e.message) if debug
  raise e if e.message !~ /(previous def|define multiple|class mismatch)/
end

.load_classes_in_dir(dir) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/conscious_concern/eager_loader.rb', line 23

def self.load_classes_in_dir(dir)
  unless File.exist?(dir)
    puts "eager-loading directory #{dir} not found" if debug
    return false
  end
  Dir.foreach(dir) do |entry|
    path = File.join(dir, entry)
    load_class_at_path(path) if path.end_with?('.rb')
    load_classes_in_dir(path) if File.directory?(path) && entry[0] != '.'
  end
end

.load_classes_in_engine_dirs(engine, *dir_names) ⇒ Object



17
18
19
20
21
# File 'lib/conscious_concern/eager_loader.rb', line 17

def self.load_classes_in_engine_dirs(engine, *dir_names)
  dir_names.each do |dir_name|
    load_classes_in_dir(engine.root.join('app', dir_name))
  end
end

.load_classes_in_rails_dirs(*dir_names) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/conscious_concern/eager_loader.rb', line 8

def self.load_classes_in_rails_dirs(*dir_names)
  rails = Object.const_defined?('Rails') ? 'Rails'.constantize : nil
  unless rails && rails.root
    puts 'no initialized Rails application' if debug
    return false
  end
  load_classes_in_engine_dirs(rails, *dir_names)
end