require "cgi"
require "erb"
Dir[File.join(File.dirname(__FILE__), "dt/**/*.rb")].each {|fn| require fn}
# Debug toolkit.
#
# Allows to output debug messages from anywhere in your Rails project.
#
# Configure your application:
# $ script/generate rails_dt
#
# Follow the instructions the generator gives you.
#
# Then anywhere in your application do a:
# DT.p "Hello, world!"
# DT.p "myvar", myvar
#
# , and see it in web output, console, and log/dt.log.
module DT #:doc:
# NOTE: Alphabetical order in this section.
# Clear messages.
def self.clear
@messages = []
end
# Return messages accumulated since last cleared.
def self.messages
@messages
end
# Dump a value or several values. Similar to Ruby's native p.
# p "my var: " + myvar.inspect
# p @myobj
def self.p(*args)
# Fetch caller information.
c = caller.first.split(":")
# Template variables. Documented in web_prefix=.
hc = {
:file => c[0],
:line => c[1],
:file_base => File.basename(c[0]),
:file_rel => Pathname(c[0]).relative_path_from(Rails.root).to_s,
}
##return hc
args.each do |r|
s = r.is_a?(String) ? r : r.inspect
# NOTE: "Canonical" order of imporance: web, console, log.
# To Web.
if self.web_prefix
pfx = ERB.new(self.web_prefix, nil, "-").result(_hash_kbinding(hc))
pcs = []
pcs << pfx
pcs << CGI.escapeHTML(s).gsub("\n", "
\n")
pcs << "
\n"
@messages << pcs.join
end
# To console.
if self.console_prefix
pfx = ERB.new(self.console_prefix, nil, "-").result(_hash_kbinding(hc))
puts [pfx, s].join
end
# To log.
if self.log_prefix and @log
pfx = ERB.new(self.log_prefix, nil, "-").result(_hash_kbinding(hc))
@log.info [pfx, s].join
end
end
# Be like puts -- more comfy when debugging in console.
nil
end
# Format accumulated messages as HTML.
# to_html # => Something like "