Add the has_many_polymorphs plugin
[cs356-p2-videostore.git] / vendor / plugins / has_many_polymorphs / lib / has_many_polymorphs / dependencies.rb
1
2 =begin rdoc
3 Adds a minimal dependency injection framework so that owners of polymorphic relationships reload after their children, reinjecting the child helper methods.
4
5 Overrides Dependencies#<tt>new_constants_in</tt>.
6 =end
7
8 module Dependencies
9     
10   mattr_accessor :injection_graph
11   self.injection_graph = Hash.new([])
12
13   # Add a dependency for this target.
14   def inject_dependency(target, *requirements)
15     target, requirements = target.to_s, requirements.map(&:to_s)    
16     injection_graph[target] = ((injection_graph[target] + requirements).uniq - [target])
17     requirements.each {|requirement| mark_for_unload requirement }
18     _logger_debug "injection graph: #{injection_graph.inspect}" if Dependencies.log_activity
19   end
20
21   # Make sure any dependent constants of the constants added by <tt>yield</tt> are reloaded.
22   def new_constants_in_with_injection(*descs, &block) # chain
23
24     if Dependencies.log_activity
25       _logger_debug "autoloaded constants: #{autoloaded_constants.inspect}"
26       _logger_debug "explicitly unloadable constants: #{explicitly_unloadable_constants.inspect}" 
27     end
28     
29     returning(new_constants_in_without_injection(*descs, &block)) do |found|
30       _logger_debug "new constants: #{found.inspect}" if Dependencies.log_activity and found.any?
31       found.each do |constant|
32         injection_graph[constant].each do |requirement| 
33           requirement.constantize
34            _logger_debug "constantized #{requirement}" if Dependencies.log_activity
35         end
36       end    
37     end
38   end
39   alias_method_chain :new_constants_in, :injection
40    
41 end