Sha256: ac07e4eff1a4f408d30a562f7ae2d023525cf0ee7b7fde936d6082025ecc9df5

Contents?: true

Size: 1.69 KB

Versions: 187

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

require "active_support/inflector/methods"

module ActiveSupport
  # 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!
  module Autoload
    def self.extended(base) # :nodoc:
      base.class_eval do
        @_autoloads = {}
        @_under_path = nil
        @_at_path = nil
        @_eager_autoload = false
      end
    end

    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
        @_autoloads[const_name] = path
      end

      super const_name, path
    end

    def autoload_under(path)
      @_under_path, old_path = path, @_under_path
      yield
    ensure
      @_under_path = old_path
    end

    def autoload_at(path)
      @_at_path, old_path = path, @_at_path
      yield
    ensure
      @_at_path = old_path
    end

    def eager_autoload
      old_eager, @_eager_autoload = @_eager_autoload, true
      yield
    ensure
      @_eager_autoload = old_eager
    end

    def eager_load!
      @_autoloads.each_value { |file| require file }
    end

    def autoloads
      @_autoloads
    end
  end
end

Version data entries

187 entries across 173 versions & 22 rubygems

Version Path
activesupport-7.0.8.6 lib/active_support/dependencies/autoload.rb
activesupport-6.1.7.10 lib/active_support/dependencies/autoload.rb
activesupport-6.1.7.9 lib/active_support/dependencies/autoload.rb
activesupport-7.0.8.5 lib/active_support/dependencies/autoload.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/dependencies/autoload.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/dependencies/autoload.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/dependencies/autoload.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/dependencies/autoload.rb
activesupport-7.0.8.4 lib/active_support/dependencies/autoload.rb
activesupport-6.1.7.8 lib/active_support/dependencies/autoload.rb
activesupport-7.0.8.1 lib/active_support/dependencies/autoload.rb
activesupport-6.1.7.7 lib/active_support/dependencies/autoload.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/dependencies/autoload.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/dependencies/autoload.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/dependencies/autoload.rb
activesupport-7.0.8 lib/active_support/dependencies/autoload.rb
activesupport-7.0.7.2 lib/active_support/dependencies/autoload.rb
activesupport-6.1.7.6 lib/active_support/dependencies/autoload.rb
activesupport-7.0.7.1 lib/active_support/dependencies/autoload.rb
activesupport-6.1.7.5 lib/active_support/dependencies/autoload.rb