Sha256: 0745ea84316b820b51f82980482d1ab4466a314eff15e18b01a9ed24e35d8dde

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

module Loader::Fetcher

  extend self
  extend ::Loader::Utils

  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

  def load(caller_class, name)
    folder_path = get_folder_path(caller_class)
    file_name = Loader::Utils.underscore(name)

    Loader.project_root_folders.each do |project_root|
      Dir.glob(File.join(project_root, '**', [folder_path, "#{file_name}.{rb,ru}"].compact)).each do |ruby_file_path|
        if Loader::Utils.require(ruby_file_path)
          c = fetch_constant(caller_class, name)

          return c unless c.nil?
        end
      end
    end

    return load_gem(caller_class, name)
  end


  protected

  def get_folder_path(caller_class)
    return if caller_class == ::Object
    caller_class.to_s.split('::').map { |camel_case| Loader::Utils.underscore(camel_case) }.join(File::Separator)
  end

  def fetch_constant(caller_class, name)
    class_name = ([caller_class, name] - [Object]).join('::')
    rgx = /^#{Regexp.escape(class_name)}$/

    ObjectSpace.each_object(Module) do |obj|
      return obj if !!(obj.to_s =~ rgx)
    end

    return nil
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
loader-3.0.1 lib/loader/fetcher.rb
loader-3.0.0 lib/loader/fetcher.rb