module Requirable

Constants

VERSION

Public Instance Methods

dir_list(path) click to toggle source
# File lib/requirable.rb, line 71
def dir_list(path)
  Dir.glob(path).sort_by { |filename| filename.count('/') }
end
figure_path(file) click to toggle source
# File lib/requirable.rb, line 60
def figure_path(file)
  return file if Pathname.new(file).absolute?

  $LOAD_PATH.each do |path|
    found = File.join(path, file)
    return File.expand_path(found) if File.file?(found)
  end

  file
end
load(paths) click to toggle source
# File lib/requirable.rb, line 5
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

      puts "Requirable::32 raised and exception: #{fatal}"

      raise exception
    end

    if last_file_counter == files.length && error
      split = current_file.split('/')
      current_file_name = split[-1]
      current_file_dir  = split[0..-2].join('/')

      raise %Q{
        An error occurred while loading #{current_file}
        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!(*folder_names) click to toggle source
# File lib/requirable.rb, line 54
def load!(*folder_names)
  folder_names.each do |name|
    Requirable.load(["./#{name}/**{,/*/**}/*.rb"])
  end
end