Module: Requirable

Extended by:
Requirable
Included in:
Requirable
Defined in:
lib/requirable.rb,
lib/requirable/version.rb

Constant Summary collapse

VERSION =
'0.0.3'

Instance Method Summary collapse

Instance Method Details

#dir_list(path) ⇒ Object



71
72
73
# File 'lib/requirable.rb', line 71

def dir_list(path)
  Dir.glob(path).sort_by { |filename| filename.count('/') }
end

#figure_path(file) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# 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) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



54
55
56
57
58
# File 'lib/requirable.rb', line 54

def load!(*folder_names)
  folder_names.each do |name|
    Requirable.load(["./#{name}/**{,/*/**}/*.rb"])
  end
end