aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/lazy_load_hooks.rb
diff options
context:
space:
mode:
authorwycats <wycats@gmail.com>2010-05-15 06:08:55 -0700
committerwycats <wycats@gmail.com>2010-05-15 06:09:07 -0700
commit9cfeefb637b603ce41d3019c8baa95ea984620d7 (patch)
tree8503a2498cb2b111e672d72ed70502a02053d9e3 /activesupport/lib/active_support/lazy_load_hooks.rb
parent458f5712dce6d7f23931effe01b7f34b66e4ab3b (diff)
downloadrails-9cfeefb637b603ce41d3019c8baa95ea984620d7.tar.gz
rails-9cfeefb637b603ce41d3019c8baa95ea984620d7.tar.bz2
rails-9cfeefb637b603ce41d3019c8baa95ea984620d7.zip
Reorganized initializers a bit to enable better hooks for common cases without the need for Railtie. Specifically, the following hooks were added:
* before_configuration: this hook is run immediately after the Application class comes into existence, but before the user has added any configuration. This is the appropriate place to set configuration for your plugin * before_initialize: This is run after all of the user's configuration has completed, but before any initializers have begun (in other words, it runs right after config/environments/{development,production,test}.rb) * after_initialize: This is run after all of the initializers have run. It is an appropriate place for forking in a preforking setup Each of these hooks may be used via ActiveSupport.on_load(name) { }. In all these cases, the context inside the block will be the Application object. This means that for simple cases, you can use these hooks without needing to create a Railtie.
Diffstat (limited to 'activesupport/lib/active_support/lazy_load_hooks.rb')
-rw-r--r--activesupport/lib/active_support/lazy_load_hooks.rb18
1 files changed, 14 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/lazy_load_hooks.rb b/activesupport/lib/active_support/lazy_load_hooks.rb
index 642a4c105c..3664431a28 100644
--- a/activesupport/lib/active_support/lazy_load_hooks.rb
+++ b/activesupport/lib/active_support/lazy_load_hooks.rb
@@ -2,16 +2,26 @@ module ActiveSupport
@load_hooks = Hash.new {|h,k| h[k] = [] }
@loaded = {}
- def self.on_load(name, &block)
+ def self.on_load(name, options = {}, &block)
if base = @loaded[name]
- base.instance_eval(&block)
+ execute_hook(base, options, block)
else
- @load_hooks[name] << block
+ @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)
- @load_hooks[name].each { |hook| base.instance_eval(&hook) }
@loaded[name] = base
+ @load_hooks[name].each do |hook, options|
+ execute_hook(base, options, hook)
+ end
end
end \ No newline at end of file