Method: Autoloaded::Autoloader#autoload!

Defined in:
lib/autoloaded/autoloader.rb

#autoload!Array of Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Issues autoload statements for source files found in #from. The constants are renamed by #with and #only. The source files are filtered by #except and #only.

Returns:

  • (Array of Array)

    the arguments passed to each autoload statement made

See Also:

Since:

  • 1.3



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
97
98
99
100
101
102
103
104
105
# File 'lib/autoloaded/autoloader.rb', line 52

def autoload!
  result = []
  from_load_pathed_directory.each_source_filename do |source_filename|
    source_basename = ::File.basename(source_filename)
    next if specifications.except.any? { |spec| spec.match source_basename }

    unless specifications.only.empty? ||
           specifications.only.any? { |spec| spec.match source_basename }
      next
    end

    first_match = (specifications.with + specifications.only).inject(nil) do |match, spec|
      match || spec.match(source_basename)
    end
    constant_names = Array(first_match ||
                           Inflection.to_constant_name(source_basename))
    existing_source_filenames = constant_names.collect do |const|
      existing_autoload? const
    end
    if existing_source_filenames.all? { |file| file == source_filename }
      next
    end

    existing_source_filenames.zip(constant_names).each do |file, const|
      if file
        Warning.changing_autoload constant_name:        constant_full_name(const),
                                  old_source_filename:  file,
                                  new_source_filename:  source_filename,
                                  host_source_location: host_source_location
      end
    end

    if existing_source_filenames.compact.empty?
      constant_names.each do |const|
        next unless existing_constant?(const)

        # Don't warn about an existing MyAwesomeGem::VERSION constant since
        # probably it was loaded by a `require 'my_awesome_gem/version'`
        # statement in 'my_awesome_gem.gemspec'.
        next if (const == :VERSION)

        Warning.existing_constant constant_name:        constant_full_name(const),
                                  source_filename:      source_filename,
                                  host_source_location: host_source_location
      end
    end

    constant_names.each do |const|
      establish_autoload const, source_filename
      result << [const, source_filename]
    end
  end
  result
end