diff options
author | Paul Rosania <paul.rosania@gmail.com> | 2010-01-27 01:08:50 -0500 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-01-29 19:25:58 -0800 |
commit | 766e857bea9bd29f27be1ccd888eec066f2e3f04 (patch) | |
tree | 8fb8934ba767997fd944c70379d01fcdbb6d6342 /railties/lib | |
parent | a06c47a52b90a5d812e64309488ad5926dc583ca (diff) | |
download | rails-766e857bea9bd29f27be1ccd888eec066f2e3f04.tar.gz rails-766e857bea9bd29f27be1ccd888eec066f2e3f04.tar.bz2 rails-766e857bea9bd29f27be1ccd888eec066f2e3f04.zip |
tsort-based dependency resolution for initializers
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'railties/lib')
-rw-r--r-- | railties/lib/rails/initializable.rb | 29 | ||||
-rw-r--r-- | railties/lib/rails/plugin.rb | 2 |
2 files changed, 13 insertions, 18 deletions
diff --git a/railties/lib/rails/initializable.rb b/railties/lib/rails/initializable.rb index d91f67823f..9a82e051e7 100644 --- a/railties/lib/rails/initializable.rb +++ b/railties/lib/rails/initializable.rb @@ -1,3 +1,5 @@ +require 'tsort' + module Rails module Initializable def self.included(base) @@ -30,29 +32,21 @@ module Rails end class Collection < Array + include TSort + + alias :tsort_each_node :each + def tsort_each_child(initializer, &block) + select { |i| i.before == initializer.name || i.name == initializer.after }.each(&block) + end + def initialize(initializers = []) - super() - initializers.each do |initializer| - if initializer.before - index = index_for(initializer.before) - elsif initializer.after - index = index_for(initializer.after) - index += 1 if index - else - index = length - end - insert(index || -1, initializer) - end + super(initializers) + replace(tsort) end def +(other) Collection.new(to_a + other.to_a) end - - def index_for(name) - initializer = find { |i| i.name == name } - initializer && index(initializer) - end end def run_initializers(*args) @@ -87,6 +81,7 @@ module Rails def initializer(name, opts = {}, &blk) raise ArgumentError, "A block must be passed when defining an initializer" unless blk + opts[:after] ||= initializers.last.name unless initializers.empty? || initializers.find { |i| i.name == opts[:before] } initializers << Initializer.new(name, nil, opts, &blk) end diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index 881c97f02d..64d03e7599 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -43,7 +43,7 @@ module Rails @config ||= Engine::Configuration.new end - initializer :load_init_rb do |app| + initializer :load_init_rb, :before => :load_application_initializers do |app| file = Dir["#{root}/{rails/init,init}.rb"].first config = app.config eval(File.read(file), binding, file) if file && File.file?(file) |