Sha256: a596067354b273c912606120f8f7df5672862b26c2d0bb37e3000051f238f703

Contents?: true

Size: 1.91 KB

Versions: 20

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

require "active_support/inflector/methods"

module ActiveSupport
  # = 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!
  module Autoload
    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

    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

    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!
      if @_eagerloaded_constants
        @_eagerloaded_constants.each { |const_name| const_get(const_name) }
        @_eagerloaded_constants = nil
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 6 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/dependencies/autoload.rb
activesupport-7.1.5.1 lib/active_support/dependencies/autoload.rb
activesupport-7.1.5 lib/active_support/dependencies/autoload.rb
activesupport-7.1.4.2 lib/active_support/dependencies/autoload.rb
activesupport-7.1.4.1 lib/active_support/dependencies/autoload.rb
activesupport-7.1.4 lib/active_support/dependencies/autoload.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/dependencies/autoload.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/dependencies/autoload.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/dependencies/autoload.rb
activesupport-7.1.3.4 lib/active_support/dependencies/autoload.rb
activesupport-7.1.3.2 lib/active_support/dependencies/autoload.rb
activesupport-7.1.3.1 lib/active_support/dependencies/autoload.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3/lib/active_support/dependencies/autoload.rb
activesupport-7.1.3 lib/active_support/dependencies/autoload.rb
activesupport-7.1.2 lib/active_support/dependencies/autoload.rb
activesupport-7.1.1 lib/active_support/dependencies/autoload.rb
activesupport-7.1.0 lib/active_support/dependencies/autoload.rb
activesupport-7.1.0.rc2 lib/active_support/dependencies/autoload.rb
activesupport-7.1.0.rc1 lib/active_support/dependencies/autoload.rb
activesupport-7.1.0.beta1 lib/active_support/dependencies/autoload.rb