Sha256: 32b0d9ba06625ed7cd19d4023ee045d58987e5b4829ff89c9a5c60d158a7e0e6

Contents?: true

Size: 1.66 KB

Versions: 80

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.values.each { |file| require file }
    end

    def autoloads
      @_autoloads
    end
  end
end

Version data entries

80 entries across 80 versions & 5 rubygems

Version Path
activesupport-4.1.16 lib/active_support/dependencies/autoload.rb
activesupport-4.1.16.rc1 lib/active_support/dependencies/autoload.rb
activesupport-4.1.15 lib/active_support/dependencies/autoload.rb
activesupport-4.1.15.rc1 lib/active_support/dependencies/autoload.rb
activesupport-4.1.14.2 lib/active_support/dependencies/autoload.rb
activesupport-4.1.14.1 lib/active_support/dependencies/autoload.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/dependencies/autoload.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/dependencies/autoload.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/dependencies/autoload.rb
activesupport-4.1.14 lib/active_support/dependencies/autoload.rb
activesupport-4.1.14.rc2 lib/active_support/dependencies/autoload.rb
activesupport-4.1.14.rc1 lib/active_support/dependencies/autoload.rb
activesupport-4.1.13 lib/active_support/dependencies/autoload.rb
activesupport-4.1.13.rc1 lib/active_support/dependencies/autoload.rb
activesupport-4.1.12 lib/active_support/dependencies/autoload.rb
activesupport-4.1.12.rc1 lib/active_support/dependencies/autoload.rb
activesupport-4.1.11 lib/active_support/dependencies/autoload.rb
activesupport-4.1.10 lib/active_support/dependencies/autoload.rb
activesupport-4.1.10.rc4 lib/active_support/dependencies/autoload.rb
activesupport-4.1.10.rc3 lib/active_support/dependencies/autoload.rb