public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/josh/rails.git
Search Repo:
Added config.dependency_loading to enable or disable the dependency loader 
after initialization
josh (author)
Thu Jul 24 09:58:26 -0700 2008
commit  3fd9036fc554979e951422a79f0f77f061112bdc
tree    13704c4e6a1d8690fd11ef1f06573c307d18629e
parent  55adaa2efc08c892bf7be55d79ac571848068256
...
51
52
53
54
55
 
 
 
 
 
56
57
58
59
60
 
 
 
 
 
 
61
62
63
64
 
 
 
 
 
 
65
66
67
...
92
93
94
95
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
98
99
...
145
146
147
148
149
150
151
152
153
154
155
156
157
 
 
158
159
160
 
 
 
 
 
 
 
161
162
163
...
560
561
562
563
 
...
51
52
53
 
 
54
55
56
57
58
59
 
 
 
 
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
...
103
104
105
 
 
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
...
186
187
188
 
 
 
 
 
 
 
 
 
 
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
...
600
601
602
 
603
0
@@ -51,17 +51,28 @@ module ActiveSupport #:nodoc:
0
     module ModuleConstMissing #:nodoc:
0
       def self.included(base) #:nodoc:
0
         base.class_eval do
0
- # Rename the original handler so we can chain it to the new one
0
- alias_method :rails_original_const_missing, :const_missing
0
+ unless defined? const_missing_without_dependencies
0
+ alias_method_chain :const_missing, :dependencies
0
+ end
0
+ end
0
+ end
0
 
0
- # Use const_missing to autoload associations so we don't have to
0
- # require_association when using single-table inheritance.
0
- def const_missing(class_id)
0
- ActiveSupport::Dependencies.load_missing_constant self, class_id
0
+ def self.excluded(base) #:nodoc:
0
+ base.class_eval do
0
+ if defined? const_missing_without_dependencies
0
+ undef_method :const_missing
0
+ alias_method :const_missing, :const_missing_without_dependencies
0
+ undef_method :const_missing_without_dependencies
0
           end
0
         end
0
       end
0
 
0
+ # Use const_missing to autoload associations so we don't have to
0
+ # require_association when using single-table inheritance.
0
+ def const_missing_with_dependencies(class_id)
0
+ ActiveSupport::Dependencies.load_missing_constant self, class_id
0
+ end
0
+
0
       def unloadable(const_desc = self)
0
         super(const_desc)
0
       end
0
@@ -92,8 +103,38 @@ module ActiveSupport #:nodoc:
0
 
0
     # Object includes this module
0
     module Loadable #:nodoc:
0
- def load(file, *extras) #:nodoc:
0
- Dependencies.new_constants_in(Object) { super }
0
+ def self.included(base) #:nodoc:
0
+ base.class_eval do
0
+ unless defined? load_without_new_constant_marking
0
+ alias_method_chain :load, :new_constant_marking
0
+ end
0
+ end
0
+ end
0
+
0
+ def self.excluded(base) #:nodoc:
0
+ base.class_eval do
0
+ if defined? load_without_new_constant_marking
0
+ undef_method :load
0
+ alias_method :load, :load_without_new_constant_marking
0
+ undef_method :load_without_new_constant_marking
0
+ end
0
+ end
0
+ end
0
+
0
+ def require_or_load(file_name)
0
+ Dependencies.require_or_load(file_name)
0
+ end
0
+
0
+ def require_dependency(file_name)
0
+ Dependencies.depend_on(file_name)
0
+ end
0
+
0
+ def require_association(file_name)
0
+ Dependencies.associate_with(file_name)
0
+ end
0
+
0
+ def load_with_new_constant_marking(file, *extras) #:nodoc:
0
+ Dependencies.new_constants_in(Object) { load_without_new_constant_marking(file, *extras) }
0
       rescue Exception => exception # errors from loading file
0
         exception.blame_file! file
0
         raise
0
@@ -145,19 +186,18 @@ module ActiveSupport #:nodoc:
0
       end
0
     end
0
 
0
- def inject!
0
- Object.instance_eval do
0
- define_method(:require_or_load) { |file_name| Dependencies.require_or_load(file_name) } unless Object.respond_to?(:require_or_load)
0
- define_method(:require_dependency) { |file_name| Dependencies.depend_on(file_name) } unless Object.respond_to?(:require_dependency)
0
- define_method(:require_association) { |file_name| Dependencies.associate_with(file_name) } unless Object.respond_to?(:require_association)
0
-
0
- alias_method :load_without_new_constant_marking, :load
0
- include Loadable
0
- end
0
-
0
+ def hook!
0
+ Object.instance_eval { include Loadable }
0
       Module.instance_eval { include ModuleConstMissing }
0
       Class.instance_eval { include ClassConstMissing }
0
       Exception.instance_eval { include Blamable }
0
+ true
0
+ end
0
+
0
+ def unhook!
0
+ ModuleConstMissing.excluded(Module)
0
+ Loadable.excluded(Object)
0
+ true
0
     end
0
 
0
     def load?
0
@@ -560,4 +600,4 @@ module ActiveSupport #:nodoc:
0
   end
0
 end
0
 
0
-ActiveSupport::Dependencies.inject!
0
+ActiveSupport::Dependencies.hook!
...
762
763
764
 
 
 
 
 
 
 
 
 
 
 
 
765
...
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
0
@@ -762,4 +762,16 @@ class DependenciesTest < Test::Unit::TestCase
0
   ensure
0
     ActiveSupport::Dependencies.load_once_paths = []
0
   end
0
+
0
+ def test_hook_called_multiple_times
0
+ assert_nothing_raised { ActiveSupport::Dependencies.hook! }
0
+ end
0
+
0
+ def test_unhook
0
+ ActiveSupport::Dependencies.unhook!
0
+ assert !Module.new.respond_to?(:const_missing_without_dependencies)
0
+ assert !Module.new.respond_to?(:load_without_new_constant_marking)
0
+ ensure
0
+ ActiveSupport::Dependencies.hook!
0
+ end
0
 end
...
171
172
173
174
 
175
176
 
 
 
177
178
179
...
525
526
527
 
 
 
 
 
 
528
529
530
...
659
660
661
 
 
 
 
 
 
 
 
 
 
 
662
663
664
...
707
708
709
 
710
711
712
...
876
877
878
879
880
 
 
881
882
883
...
171
172
173
 
174
175
176
177
178
179
180
181
182
...
528
529
530
531
532
533
534
535
536
537
538
539
...
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
...
727
728
729
730
731
732
733
...
897
898
899
 
 
900
901
902
903
904
0
@@ -171,9 +171,12 @@ module Rails
0
       # Load view path cache
0
       load_view_paths
0
 
0
- # load application classes
0
+ # Load application classes
0
       load_application_classes
0
 
0
+ # Disable dependency loading during request cycle
0
+ disable_dependency_loading
0
+
0
       # Flag initialized
0
       Rails.initialized = true
0
     end
0
@@ -525,6 +528,12 @@ Run `rake gems:install` to install the missing gems.
0
       Dispatcher.define_dispatcher_callbacks(configuration.cache_classes)
0
       Dispatcher.new(RAILS_DEFAULT_LOGGER).send :run_callbacks, :prepare_dispatch
0
     end
0
+
0
+ def disable_dependency_loading
0
+ if configuration.cache_classes && !configuration.dependency_loading
0
+ ActiveSupport::Dependencies.unhook!
0
+ end
0
+ end
0
   end
0
 
0
   # The Configuration class holds all the parameters for the Initializer and
0
@@ -659,6 +668,17 @@ Run `rake gems:install` to install the missing gems.
0
       !!@reload_plugins
0
     end
0
 
0
+ # Enables or disables dependency loading during the request cycle. Setting
0
+ # <tt>dependency_loading</tt> to true will allow new classes to be loaded
0
+ # during a request. Setting it to false will disable this behavior.
0
+ #
0
+ # Those who want to run in a threaded environment should disable this
0
+ # option and eager load or require all there classes on initialization.
0
+ #
0
+ # If <tt>cache_classes</tt> is disabled, dependency loaded will always be
0
+ # on.
0
+ attr_accessor :dependency_loading
0
+
0
     # An array of gems that this rails application depends on. Rails will automatically load
0
     # these gems during installation, and allow you to install any missing gems with:
0
     #
0
@@ -707,6 +727,7 @@ Run `rake gems:install` to install the missing gems.
0
       self.view_path = default_view_path
0
       self.controller_paths = default_controller_paths
0
       self.cache_classes = default_cache_classes
0
+ self.dependency_loading = default_dependency_loading
0
       self.whiny_nils = default_whiny_nils
0
       self.plugins = default_plugins
0
       self.plugin_paths = default_plugin_paths
0
@@ -876,8 +897,8 @@ Run `rake gems:install` to install the missing gems.
0
         paths
0
       end
0
 
0
- def default_dependency_mechanism
0
- :load
0
+ def default_dependency_loading
0
+ true
0
       end
0
 
0
       def default_cache_classes

Comments

    No one has commented yet.