2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2017-10-21 09:11:29 -04:00
|
|
|
require "active_support/inflector/methods"
|
2009-12-02 23:01:01 -05:00
|
|
|
|
|
|
|
module ActiveSupport
|
2012-08-01 12:23:11 -04:00
|
|
|
# Autoload and eager load conveniences for your library.
|
|
|
|
#
|
|
|
|
# This module allows you to define autoloads based on
|
|
|
|
# Rails conventions (i.e. no need to define the path
|
|
|
|
# it is automatically guessed based on the filename)
|
|
|
|
# and also define a set of constants that needs to be
|
|
|
|
# eager loaded:
|
|
|
|
#
|
|
|
|
# module MyLib
|
|
|
|
# extend ActiveSupport::Autoload
|
|
|
|
#
|
|
|
|
# autoload :Model
|
|
|
|
#
|
|
|
|
# eager_autoload do
|
|
|
|
# autoload :Cache
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Then your library can be eager loaded by simply calling:
|
|
|
|
#
|
|
|
|
# MyLib.eager_load!
|
2009-12-02 23:01:01 -05:00
|
|
|
module Autoload
|
2012-09-14 22:31:49 -04:00
|
|
|
def self.extended(base) # :nodoc:
|
2012-08-01 12:23:11 -04:00
|
|
|
base.class_eval do
|
|
|
|
@_autoloads = {}
|
|
|
|
@_under_path = nil
|
|
|
|
@_at_path = nil
|
|
|
|
@_eager_autoload = false
|
|
|
|
end
|
|
|
|
end
|
2009-12-02 23:01:01 -05:00
|
|
|
|
2012-08-01 12:23:11 -04:00
|
|
|
def autoload(const_name, path = @_at_path)
|
2012-03-12 02:18:03 -04:00
|
|
|
unless path
|
2013-01-01 12:35:43 -05:00
|
|
|
full = [name, @_under_path, const_name.to_s].compact.join("::")
|
2012-03-12 02:18:03 -04:00
|
|
|
path = Inflector.underscore(full)
|
|
|
|
end
|
2009-12-02 23:01:01 -05:00
|
|
|
|
2012-08-01 12:23:11 -04:00
|
|
|
if @_eager_autoload
|
|
|
|
@_autoloads[const_name] = path
|
2009-12-12 19:41:26 -05:00
|
|
|
end
|
2012-03-12 02:18:03 -04:00
|
|
|
|
|
|
|
super const_name, path
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def autoload_under(path)
|
2012-08-01 12:23:11 -04:00
|
|
|
@_under_path, old_path = path, @_under_path
|
2009-12-02 23:01:01 -05:00
|
|
|
yield
|
|
|
|
ensure
|
2012-08-01 12:23:11 -04:00
|
|
|
@_under_path = old_path
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def autoload_at(path)
|
2012-08-01 12:23:11 -04:00
|
|
|
@_at_path, old_path = path, @_at_path
|
2009-12-02 23:01:01 -05:00
|
|
|
yield
|
|
|
|
ensure
|
2012-08-01 12:23:11 -04:00
|
|
|
@_at_path = old_path
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
|
2009-12-22 18:27:37 -05:00
|
|
|
def eager_autoload
|
2012-08-01 12:23:11 -04:00
|
|
|
old_eager, @_eager_autoload = @_eager_autoload, true
|
2009-12-12 19:41:26 -05:00
|
|
|
yield
|
|
|
|
ensure
|
2012-08-01 12:23:11 -04:00
|
|
|
@_eager_autoload = old_eager
|
2009-12-12 19:41:26 -05:00
|
|
|
end
|
|
|
|
|
2012-08-01 12:23:11 -04:00
|
|
|
def eager_load!
|
2014-11-02 19:40:47 -05:00
|
|
|
@_autoloads.each_value { |file| require file }
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
|
2009-12-12 19:41:26 -05:00
|
|
|
def autoloads
|
2012-08-01 12:23:11 -04:00
|
|
|
@_autoloads
|
2009-12-12 19:41:26 -05:00
|
|
|
end
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
2009-12-12 19:41:26 -05:00
|
|
|
end
|