diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-07-13 01:57:54 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-13 01:57:54 -0300 |
commit | 9be45aceba8db8a734d2d4b00ccabe0ada31d40b (patch) | |
tree | bcf1ffe68711421dd7048c00dd5a0e4198af3c65 /activesupport/lib | |
parent | fc50f1fd50b05dc7e0f259dbe840e6d292e85aef (diff) | |
parent | 949485550f4fbd2f194c7851c5ca8b8026fe2cbb (diff) | |
download | rails-9be45aceba8db8a734d2d4b00ccabe0ada31d40b.tar.gz rails-9be45aceba8db8a734d2d4b00ccabe0ada31d40b.tar.bz2 rails-9be45aceba8db8a734d2d4b00ccabe0ada31d40b.zip |
Merge pull request #25790 from mrageh/wrap-lazy-load-hooks-in-module
Wrap module around lazy load hooks
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/lazy_load_hooks.rb | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/activesupport/lib/active_support/lazy_load_hooks.rb b/activesupport/lib/active_support/lazy_load_hooks.rb index e2b8f0f648..67b54b45ea 100644 --- a/activesupport/lib/active_support/lazy_load_hooks.rb +++ b/activesupport/lib/active_support/lazy_load_hooks.rb @@ -20,29 +20,37 @@ module ActiveSupport # +activerecord/lib/active_record/base.rb+ is: # # ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base) - @load_hooks = Hash.new { |h,k| h[k] = [] } - @loaded = Hash.new { |h,k| h[k] = [] } - - def self.on_load(name, options = {}, &block) - @loaded[name].each do |base| - execute_hook(base, options, block) + module LazyLoadHooks + def self.extended(base) # :nodoc: + base.class_eval do + @load_hooks = Hash.new { |h,k| h[k] = [] } + @loaded = Hash.new { |h,k| h[k] = [] } + end end - @load_hooks[name] << [block, options] - end + def on_load(name, options = {}, &block) + @loaded[name].each do |base| + execute_hook(base, options, block) + end - def self.execute_hook(base, options, block) - if options[:yield] - block.call(base) - else - base.instance_eval(&block) + @load_hooks[name] << [block, options] 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) + def execute_hook(base, options, block) + if options[:yield] + block.call(base) + else + base.instance_eval(&block) + end + end + + def run_load_hooks(name, base = Object) + @loaded[name] << base + @load_hooks[name].each do |hook, options| + execute_hook(base, options, hook) + end end end + + extend LazyLoadHooks end |