Module: Loader::Fetcher

Extended by:
Fetcher, Utils
Included in:
Fetcher
Defined in:
lib/loader/fetcher.rb

Instance Method Summary collapse

Methods included from Utils

pwd, require, underscore

Instance Method Details

#load(caller_class, name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/loader/fetcher.rb', line 21

def load(caller_class, name)
  Loader.project_root_folders.each do |project_root|
    lookup_and_load_file_in(project_root, caller_class, name)

    constant = fetch_constant(caller_class, name)

    return constant unless constant.nil?
  end

  return load_gem(caller_class, name)
end

#load_gem(caller_class, constant_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/loader/fetcher.rb', line 6

def load_gem(caller_class, constant_name)
  underscore_name = Loader::Utils.underscore(constant_name)
  gem_symbolic_name = underscore_name.sub(File::Separator, '-')

  begin
    require(underscore_name)
  rescue LoadError
    require(gem_symbolic_name)
  end

  return fetch_constant(caller_class, constant_name)
rescue LoadError
  nil
end

#lookup_and_load_file_in(project_root, caller_class, name) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/loader/fetcher.rb', line 33

def lookup_and_load_file_in(project_root, caller_class, name)
  file_name = Loader::Utils.underscore(name)

  get_folder_paths(caller_class).each do |folder_path|
    Dir.glob(File.join(project_root, '**', folder_path, "#{file_name}.{rb,ru}")).each do |ruby_file_path|
      return if Loader::Utils.require(ruby_file_path)
    end
  end
end