Sha256: c731f1477342e15db021f2c1bcb59d5eed41ebea6a4d3593a7d4ed4bbf2cd484

Contents?: true

Size: 1.66 KB

Versions: 127

Compression:

Stored size: 1.66 KB

Contents

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

127 entries across 121 versions & 16 rubygems

Version Path
lazy_record-0.1.0 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/dependencies/autoload.rb
activesupport-5.0.2 lib/active_support/dependencies/autoload.rb
activesupport-5.0.2.rc1 lib/active_support/dependencies/autoload.rb
activesupport-5.1.0.beta1 lib/active_support/dependencies/autoload.rb
activesupport-4.2.8 lib/active_support/dependencies/autoload.rb
activesupport-4.2.8.rc1 lib/active_support/dependencies/autoload.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies/autoload.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies/autoload.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies/autoload.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies/autoload.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies/autoload.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies/autoload.rb
autocompl-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies/autoload.rb
abaci-0.3.0 vendor/bundle/gems/activesupport-5.0.1/lib/active_support/dependencies/autoload.rb
abaci-0.3.0 vendor/bundle/gems/activesupport-5.0.0/lib/active_support/dependencies/autoload.rb
activesupport-5.0.1 lib/active_support/dependencies/autoload.rb
activesupport-5.0.1.rc2 lib/active_support/dependencies/autoload.rb
activesupport-5.0.1.rc1 lib/active_support/dependencies/autoload.rb
second_step-0.1.2 secondstep-notify-1.0.0-osx/lib/ruby/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies/autoload.rb
activesupport-5.0.0.1 lib/active_support/dependencies/autoload.rb