Module: DashOverlord::Requirable
Defined Under Namespace
Classes: Base
Instance Method Summary collapse
- #dir_list(path) ⇒ Object
- #figure_path(file) ⇒ Object
- #load(paths) ⇒ Object
- #load! ⇒ Object
- #load_relative(file, path) ⇒ Object
- #paths ⇒ Object
Instance Method Details
#dir_list(path) ⇒ Object
42 43 44 |
# File 'lib/dash_overlord/requirable.rb', line 42 def dir_list(path) Dir.glob(path).sort_by { |filename| filename.count('/') } end |
#figure_path(file) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/dash_overlord/requirable.rb', line 98 def figure_path(file) return file if Pathname.new(file).absolute? $LOAD_PATH.each do |path| found = File.join(path, file) return File.(found) if File.file?(found) end file end |
#load(paths) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/dash_overlord/requirable.rb', line 46 def load(paths) _current_file = nil files = [*paths].flatten.map { |path| dir_list(path) }.flatten.uniq until files.empty? last_file_counter = files.length fatal, error = nil, nil files.dup.each do |file| _current_file = figure_path(file) begin require figure_path(file) files.delete(file) rescue NameError, LoadError => error # puts "Cyclic dependency reload for #{error.class}: #{error.message} on file: #{file}" rescue Exception => fatal break end end if fatal exception = fatal || error # logger.exception exception, :short puts "Requirable:73 raised and exception: #{fatal}" raise exception end if last_file_counter == files.length && error _splitted = _current_file.split('/') _current_file_name = _splitted[-1] _current_file_dir = _splitted[0..-2].join('/') raise %Q{ An error occurred while loading #{_current_file_name} Error: #{error} Possible problems: - Syntax error on #{_current_file_name} - LOAD_PATH does not have app ROOT_PATH so it can find #{_current_file_name} inside #{_current_file_dir} } end end end |
#load! ⇒ Object
28 29 30 |
# File 'lib/dash_overlord/requirable.rb', line 28 def load! load @base.before_load_paths end |
#load_relative(file, path) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/dash_overlord/requirable.rb', line 32 def load_relative(file, path) app_dir = File.dirname(file) $LOAD_PATH.unshift(app_dir) unless $LOAD_PATH.include?(app_dir) Dir.chdir(app_dir) do load([path]) end end |