module ActiveSupport::LazyLoadHooks
def self.extended(base) # :nodoc:
def self.extended(base) # :nodoc: base.class_eval do @load_hooks = Hash.new { |h, k| h[k] = [] } @loaded = Hash.new { |h, k| h[k] = [] } @run_once = Hash.new { |h, k| h[k] = [] } end end
def execute_hook(name, base, options, block)
def execute_hook(name, base, options, block) with_execution_control(name, block, options[:run_once]) do if options[:yield] block.call(base) else if base.is_a?(Module) base.class_eval(&block) else base.instance_eval(&block) end end end end
def on_load(name, options = {}, &block)
* :yield - Yields the object that run_load_hooks to +block+.
Options:
immediately.
loaded. If the component has already loaded, the block is executed
Declares a block that will be executed when a \Rails component is fully
def on_load(name, options = {}, &block) @loaded[name].each do |base| execute_hook(name, base, options, block) end @load_hooks[name] << [block, options] end
def run_load_hooks(name, base = Object)
In the case of the above example, it will execute all hooks registered
ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
evaluation context.
Executes all blocks registered to +name+ via on_load, using +base+ as the
def run_load_hooks(name, base = Object) @loaded[name] << base @load_hooks[name].each do |hook, options| execute_hook(name, base, options, hook) end end
def with_execution_control(name, block, once)
def with_execution_control(name, block, once) unless @run_once[name].include?(block) @run_once[name] << block if once yield end end