Add the has_many_polymorphs plugin
[cs356-p2-videostore.git] / vendor / plugins / has_many_polymorphs / lib / has_many_polymorphs / debugging_tools.rb
1
2 =begin rdoc
3
4 Debugging tools for Has_many_polymorphs. 
5
6 Enable the different tools by setting the environment variable HMP_DEBUG. Settings with special meaning are <tt>"ruby-debug"</tt>, <tt>"trace"</tt>, and <tt>"dependencies"</tt>.
7
8 == Code generation
9
10 Enabled by default when HMP_DEBUG is set.
11
12 Ouputs a folder <tt>generated_models/</tt> in RAILS_ROOT containing valid Ruby files explaining all the ActiveRecord relationships set up by the plugin, as well as listing the line in the plugin that called each particular association method.
13
14 == Ruby-debug
15
16 Enable by setting HMP_DEBUG to <tt>"ruby-debug"</tt>.
17
18 Starts <tt>ruby-debug</tt> for the life of the process.
19
20 == Trace
21
22 Enable by setting HMP_DEBUG to <tt>"ruby-debug"</tt>.
23
24 Outputs an indented trace of relevant method calls as they occur.
25
26 == Dependencies
27
28 Enable by setting HMP_DEBUG to <tt>"dependencies"</tt>.
29
30 Turns on Rails' default dependency logging.
31
32 =end
33
34 _logger_warn "debug mode enabled"
35
36 class << ActiveRecord::Base
37   COLLECTION_METHODS = [:belongs_to, :has_many, :has_and_belongs_to_many, :has_one, 
38       :has_many_polymorphs, :acts_as_double_polymorphic_join].each do |method_name|
39     alias_method "original_#{method_name}".to_sym, method_name
40     undef_method method_name
41   end      
42
43   unless defined? GENERATED_CODE_DIR
44     GENERATED_CODE_DIR = "#{RAILS_ROOT}/generated_models"
45   
46     begin
47       system "rm -rf #{GENERATED_CODE_DIR}" 
48       Dir.mkdir GENERATED_CODE_DIR
49     rescue Errno::EACCES
50       _logger_warn "no permissions for generated code dir: #{GENERATED_CODE_DIR}"
51     end
52
53     if File.exist? GENERATED_CODE_DIR
54       alias :original_method_missing :method_missing
55       def method_missing(method_name, *args, &block)
56         if COLLECTION_METHODS.include? method_name.to_sym
57           Dir.chdir GENERATED_CODE_DIR do
58             filename = "#{demodulate(self.name.underscore)}.rb"
59             contents = File.open(filename).read rescue "\nclass #{self.name}\n\nend\n"
60             line = caller[1][/\:(\d+)\:/, 1]
61             contents[-5..-5] = "\n  #{method_name} #{args[0..-2].inspect[1..-2]},\n     #{args[-1].inspect[1..-2].gsub(" :", "\n     :").gsub("=>", " => ")}\n#{ block ? "     #{block.inspect.sub(/\@.*\//, '@')}\n" : ""}     # called from line #{line}\n\n"
62             File.open(filename, "w") do |file| 
63               file.puts contents             
64             end
65           end
66           # doesn't actually display block contents
67           self.send("original_#{method_name}", *args, &block)
68         else
69           self.send(:original_method_missing, method_name, *args, &block)
70         end
71       end
72     end      
73     
74   end
75 end
76
77 case ENV['HMP_DEBUG']
78
79   when "ruby-debug"
80     require 'rubygems'
81     require 'ruby-debug'
82     Debugger.start
83     _logger_warn "ruby-debug enabled."
84
85   when "trace"
86     _logger_warn "method tracing enabled"  
87     $debug_trace_indent = 0
88     set_trace_func (proc do |event, file, line, id, binding, classname|
89       if id.to_s =~ /instantiate/ #/IRB|Wirble|RubyLex|RubyToken|Logger|ConnectionAdapters|SQLite3|MonitorMixin|Benchmark|Inflector|Inflections/ 
90         if event == 'call'
91           puts (" " * $debug_trace_indent) + "#{event}ed #{classname}\##{id} from #{file.split('/').last}::#{line}"
92           $debug_trace_indent += 1
93         elsif event == 'return'
94           $debug_trace_indent -= 1 unless $debug_trace_indent == 0
95           puts (" " * $debug_trace_indent) + "#{event}ed #{classname}\##{id}"
96         end
97       end
98     end)
99     
100   when "dependencies"
101     _logger_warn "dependency activity being logged"
102     (::Dependencies.log_activity = true) rescue nil
103 end