Skip to content

Commit

Permalink
Merge pull request #44893 from ghousemohamed/add-docs-for-run-load-hooks
Browse files Browse the repository at this point in the history
Add API docs for `run_load_hooks` [ci-skip]

(cherry picked from commit cdabe88)
  • Loading branch information
jonathanhefner committed Apr 15, 2022
1 parent 47d0493 commit 2266802
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions activesupport/lib/active_support/lazy_load_hooks.rb
@@ -1,14 +1,14 @@
# frozen_string_literal: true

module ActiveSupport
# lazy_load_hooks allows Rails to lazily load a lot of components and thus
# LazyLoadHooks allows Rails to lazily load a lot of components and thus
# making the app boot faster. Because of this feature now there is no need to
# require <tt>ActiveRecord::Base</tt> at boot time purely to apply
# configuration. Instead a hook is registered that applies configuration once
# <tt>ActiveRecord::Base</tt> is loaded. Here <tt>ActiveRecord::Base</tt> is
# used as example but this feature can be applied elsewhere too.
#
# Here is an example where +on_load+ method is called to register a hook.
# Here is an example where on_load method is called to register a hook.
#
# initializer 'active_record.initialize_timezone' do
# ActiveSupport.on_load(:active_record) do
Expand All @@ -18,10 +18,14 @@ module ActiveSupport
# end
#
# When the entirety of +ActiveRecord::Base+ has been
# evaluated then +run_load_hooks+ is invoked. The very last line of
# evaluated then run_load_hooks is invoked. The very last line of
# +ActiveRecord::Base+ is:
#
# ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
#
# run_load_hooks will then execute all the hooks that were registered
# with the on_load method. In the case of the above example, it will
# execute the block of code that is in the +initializer+.
module LazyLoadHooks
def self.extended(base) # :nodoc:
base.class_eval do
Expand All @@ -46,6 +50,13 @@ def on_load(name, options = {}, &block)
@load_hooks[name] << [block, options]
end

# Executes all blocks registered to +name+ via on_load, using +base+ as the
# evaluation context.
#
# ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
#
# In the case of the above example, it will execute all hooks registered
# for +:active_record+ within the class +ActiveRecord::Base+.
def run_load_hooks(name, base = Object)
@loaded[name] << base
@load_hooks[name].each do |hook, options|
Expand Down

0 comments on commit 2266802

Please sign in to comment.