Module: ActiveSupport::Autoload
- Included in:
- ActiveSupport, NumberHelper
- Defined in:
- lib/active_support/dependencies/autoload.rb
Overview
Active Support Autoload
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!
Class Method Summary collapse
-
.extended(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #autoload(const_name, path = @_at_path) ⇒ Object
- #autoload_at(path) ⇒ Object
- #autoload_under(path) ⇒ Object
- #eager_autoload ⇒ Object
- #eager_load! ⇒ Object
Class Method Details
.extended(base) ⇒ Object
:nodoc:
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/active_support/dependencies/autoload.rb', line 30 def self.extended(base) # :nodoc: if RUBY_VERSION < "3" base.class_eval do @_autoloads = nil @_under_path = nil @_at_path = nil @_eager_autoload = false @_eagerloaded_constants = nil end end end |
Instance Method Details
#autoload(const_name, path = @_at_path) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/active_support/dependencies/autoload.rb', line 42 def autoload(const_name, path = @_at_path) unless path full = [name, @_under_path, const_name.to_s].compact.join("::") path = Inflector.underscore(full) end if @_eager_autoload @_eagerloaded_constants ||= [] @_eagerloaded_constants << const_name end super const_name, path end |
#autoload_at(path) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/active_support/dependencies/autoload.rb', line 63 def autoload_at(path) @_at_path, old_path = path, @_at_path yield ensure @_at_path = old_path end |
#autoload_under(path) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/active_support/dependencies/autoload.rb', line 56 def autoload_under(path) @_under_path, old_path = path, @_under_path yield ensure @_under_path = old_path end |
#eager_autoload ⇒ Object
70 71 72 73 74 75 |
# File 'lib/active_support/dependencies/autoload.rb', line 70 def eager_autoload old_eager, @_eager_autoload = @_eager_autoload, true yield ensure @_eager_autoload = old_eager end |
#eager_load! ⇒ Object
77 78 79 80 81 82 |
# File 'lib/active_support/dependencies/autoload.rb', line 77 def eager_load! if @_eagerloaded_constants @_eagerloaded_constants.each { |const_name| const_get(const_name) } @_eagerloaded_constants = nil end end |