aboutsummaryrefslogblamecommitdiffstats
path: root/activesupport/lib/active_support/lazy_load_hooks.rb
blob: 3664431a2800cb855f6c0801c02b5a35786784f2 (plain) (tree)
1
2
3
4
5
6
7
8
                    

                                           
 
                                              
                           
                                        
        








                                             
       
     
 
                                              
                        


                                             

     
module ActiveSupport
  @load_hooks = Hash.new {|h,k| h[k] = [] }
  @loaded = {}

  def self.on_load(name, options = {}, &block)
    if base = @loaded[name]
      execute_hook(base, options, block)
    else
      @load_hooks[name] << [block, options]
    end
  end

  def self.execute_hook(base, options, block)
    if options[:yield]
      block.call(base)
    else
      base.instance_eval(&block)
    end
  end

  def self.run_load_hooks(name, base = Object)
    @loaded[name] = base
    @load_hooks[name].each do |hook, options|
      execute_hook(base, options, hook)
    end
  end
end