Skip to content
This repository has been archived by the owner on Oct 19, 2018. It is now read-only.

autoloading multiple files

Mitch VanDuyn edited this page Feb 26, 2017 · 1 revision

The rails autoload algorithm will only load the first file it finds.

a couple of solutions:

  1. for classes like ServerOp and ActiveRecord::Base we could during the class definition look for a second file in an alternative directory. for example
# app/operations/foo.rb
class Foo < ServerOp
  ...
end

when Foo is defined it automatically looks for a file named app/hyperloop/operations/foo.rb

probably using the directory path of the original foo and inserting 'hyperloop' (which could be defined in a config file)

  1. variant on the first where we have some directive like load_inteface 'hyperloop'
class Foo < ServerOp
  interface :hyperloop
end
  1. does the simple way work?
require 'hyperloop/operation/foo_public.rb`
class Foo < ServerOp
end

its just a little extra typing...

  1. modify rails autoload:

https://github.com/rails/rails/blob/f243e7f0d0b2cef350127fd518532fedb65f0bd0/activesupport/lib/active_support/dependencies.rb#423 (add search_for_file that is like search_for_files)

update https://github.com/rails/rails/blob/f243e7f0d0b2cef350127fd518532fedb65f0bd0/activesupport/lib/active_support/dependencies.rb#493 (load_missing_constant) so that it loads all the files returned by search_for_files.

Only trouble with this is perhaps sometimes libs and gems using shadowing on the autoload path?

Clone this wiki locally