Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 90 lines (72 sloc) 2.267 kb
766e857 Paul Rosania tsort-based dependency resolution for initializers
paulrosania authored
1 require 'tsort'
2
6d6ae08 Start moving the initializers into the application object
Carl Lerche authored
3 module Rails
4 module Initializable
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
5 def self.included(base)
6 base.extend ClassMethods
df95f16 Update initializable
Yehuda Katz + Carl Lerche authored
7 end
6d6ae08 Start moving the initializers into the application object
Carl Lerche authored
8
4369426 Add the ability to merge initializers from various objects in order.
Yehuda Katz + Carl Lerche authored
9 class Initializer
f70079e Joshua Peek Fix warnings in railties configuration and initializeable
josh authored
10 attr_reader :name, :block
4369426 Add the ability to merge initializers from various objects in order.
Yehuda Katz + Carl Lerche authored
11
12 def initialize(name, context, options, &block)
d68884f José Valim :group => :assets should only run in the assets environment.
josevalim authored
13 options[:group] ||= :default
4369426 Add the ability to merge initializers from various objects in order.
Yehuda Katz + Carl Lerche authored
14 @name, @context, @options, @block = name, context, options, block
15 end
16
17 def before
18 @options[:before]
19 end
20
21 def after
22 @options[:after]
23 end
24
eb367af José Valim `rake assets:precompile` loads the application but does not initialize i...
josevalim authored
25 def belongs_to?(group)
26 @options[:group] == group || @options[:group] == :all
27 end
28
4369426 Add the ability to merge initializers from various objects in order.
Yehuda Katz + Carl Lerche authored
29 def run(*args)
30 @context.instance_exec(*args, &block)
31 end
32
33 def bind(context)
34 return self if @context
35 Initializer.new(@name, context, @options, &block)
36 end
df95f16 Update initializable
Yehuda Katz + Carl Lerche authored
37 end
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
38
df95f16 Update initializable
Yehuda Katz + Carl Lerche authored
39 class Collection < Array
766e857 Paul Rosania tsort-based dependency resolution for initializers
paulrosania authored
40 include TSort
41
42 alias :tsort_each_node :each
43 def tsort_each_child(initializer, &block)
44 select { |i| i.before == initializer.name || i.name == initializer.after }.each(&block)
45 end
46
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
47 def +(other)
48 Collection.new(to_a + other.to_a)
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
49 end
df95f16 Update initializable
Yehuda Katz + Carl Lerche authored
50 end
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
51
d68884f José Valim :group => :assets should only run in the assets environment.
josevalim authored
52 def run_initializers(group=:default, *args)
f70079e Joshua Peek Fix warnings in railties configuration and initializeable
josh authored
53 return if instance_variable_defined?(:@ran)
25f1d5d Aaron Patterson use tsort_each to avoid intermediary array
tenderlove authored
54 initializers.tsort_each do |initializer|
d68884f José Valim :group => :assets should only run in the assets environment.
josevalim authored
55 initializer.run(*args) if initializer.belongs_to?(group)
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
56 end
57 @ran = true
df95f16 Update initializable
Yehuda Katz + Carl Lerche authored
58 end
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
59
4369426 Add the ability to merge initializers from various objects in order.
Yehuda Katz + Carl Lerche authored
60 def initializers
5d07869 José Valim Ensure all initializers are collections.
josevalim authored
61 @initializers ||= self.class.initializers_for(self)
4369426 Add the ability to merge initializers from various objects in order.
Yehuda Katz + Carl Lerche authored
62 end
63
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
64 module ClassMethods
65 def initializers
9f7874a Jeremy Kemper Move Collection responsibility from application to initializable
jeremy authored
66 @initializers ||= Collection.new
e4d7e50 Moving more initializers into the application object
Carl Lerche authored
67 end
68
98240c4 José Valim Get rid of initializers global and create i18n railtie.
josevalim authored
69 def initializers_chain
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
70 initializers = Collection.new
71 ancestors.reverse_each do |klass|
72 next unless klass.respond_to?(:initializers)
98240c4 José Valim Get rid of initializers global and create i18n railtie.
josevalim authored
73 initializers = initializers + klass.initializers
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
74 end
75 initializers
6d6ae08 Start moving the initializers into the application object
Carl Lerche authored
76 end
77
5d07869 José Valim Ensure all initializers are collections.
josevalim authored
78 def initializers_for(binding)
79 Collection.new(initializers_chain.map { |i| i.bind(binding) })
80 end
81
897164d Conceptually unify instance & global initializers
Yehuda Katz + Carl Lerche authored
82 def initializer(name, opts = {}, &blk)
fa8dfc7 Raise an exception if an initializer is defined without a block
Carlhuda authored
83 raise ArgumentError, "A block must be passed when defining an initializer" unless blk
766e857 Paul Rosania tsort-based dependency resolution for initializers
paulrosania authored
84 opts[:after] ||= initializers.last.name unless initializers.empty? || initializers.find { |i| i.name == opts[:before] }
98240c4 José Valim Get rid of initializers global and create i18n railtie.
josevalim authored
85 initializers << Initializer.new(name, nil, opts, &blk)
df95f16 Update initializable
Yehuda Katz + Carl Lerche authored
86 end
87 end
6d6ae08 Start moving the initializers into the application object
Carl Lerche authored
88 end
cdb8609 Jeremy Kemper Speed up boot by tsorting as infrequently as possible
jeremy authored
89 end
Something went wrong with that request. Please try again.