rails_dt 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,171 @@
1
+ <head>
2
+ <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
3
+ <link href="dev/github.css" rel="stylesheet" type="text/css" />
4
+ </head>
5
+ <h1 id="rails-debug-toolkit">Rails Debug Toolkit</h1>
6
+
7
+ <h2 id="introduction">Introduction</h2>
8
+
9
+ <p><code>rails_dt</code> gem gives you <code>DT.p()</code> method you can use anywhere in your project to print your debug messages.</p>
10
+
11
+ <p>It’s somewhat similar to Ruby’s native <code>p()</code> with output being sent to log, console and web.</p>
12
+
13
+ <p>For example, <code>DT.p "hello, world!"</code> invoked in <code>RootController</code> will give you a:</p>
14
+
15
+ <pre><code>[DT app/controllers/root_controller.rb:3] hello, world!
16
+ </code></pre>
17
+
18
+ <h2 id="the-ideas-behind-it">The Ideas Behind It</h2>
19
+
20
+ <ul>
21
+ <li>Debug message printer must <strong>not require initialization</strong>.</li>
22
+ <li>Debug message printer must be <strong>nothing else</strong>, but a debug message printer.</li>
23
+ <li>Debug message printer must be simple and invoked <strong>always the same way</strong> regardless of where you call it from.</li>
24
+ <li>Debug message printer calls must be <strong>clearly visible</strong> in the code.</li>
25
+ <li>Debug message printer must <strong>print its location in code</strong> so you can find and modify/remove it as easy as possible.</li>
26
+ </ul>
27
+
28
+ <h2 id="express-setup-rails-3">Express Setup (Rails 3)</h2>
29
+
30
+ <p>In your <code>Gemfile</code>, add:</p>
31
+
32
+ <pre><code>gem "rails_dt"
33
+ </code></pre>
34
+
35
+ <p>Then do a <code>bundle install</code>.</p>
36
+
37
+ <p>This gives you an express (zero-conf) setup, which outputs messages to log, <code>log/dt.log</code> and console.</p>
38
+
39
+ <h2 id="express-setup-rails-2">Express Setup (Rails 2)</h2>
40
+
41
+ <pre><code>$ gem sources --add http://rubygems.org
42
+ $ gem install rails_dt
43
+ </code></pre>
44
+
45
+ <p>In your <code>config/environment.rb</code>, add:</p>
46
+
47
+ <pre><code>config.gem "rails_dt"
48
+ </code></pre>
49
+
50
+ <h2 id="setting-up-web-output-both-rails-3-and-rails-2">Setting Up Web Output (Both Rails 3 and Rails 2)</h2>
51
+
52
+ <p>In your application root, do a:</p>
53
+
54
+ <pre><code>$ rails generate rails_dt # Rails 3
55
+ $ script/generate rails_dt # Rails 2
56
+ </code></pre>
57
+
58
+ <p>Follow the instructions the generator gives you then. They are listed below.</p>
59
+
60
+ <p>Inside your <code>ApplicationController</code> class, add:</p>
61
+
62
+ <pre><code>handles_dt
63
+ </code></pre>
64
+
65
+ <p>Inside your <code>app/views/layouts/application.html.erb</code> <code>&lt;head&gt;</code> section, add:</p>
66
+
67
+ <pre><code>&lt;%= stylesheet_link_tag "dt" %&gt;
68
+ </code></pre>
69
+
70
+ <p>Inside your <code>app/views/layouts/application.html.erb</code> <code>&lt;body&gt;</code> section, add:</p>
71
+
72
+ <pre><code>&lt;div class="DT"&gt;
73
+ &lt;%= DT.web_messages_as_html %&gt;
74
+ &lt;/div&gt;
75
+ </code></pre>
76
+
77
+ <h2 id="checking-setup">Checking Setup</h2>
78
+
79
+ <p>Somewhere in your <code>app/views/layouts/application.html.erb</code>, add:</p>
80
+
81
+ <pre><code>&lt;% DT.p "hello, world!" %&gt;
82
+ </code></pre>
83
+
84
+ <p>Refresh the page. You should see “hello, world!”:</p>
85
+
86
+ <ul>
87
+ <li>In your application log.</li>
88
+ <li>In <code>log/dt.log</code>.</li>
89
+ <li>On the web page, if you’ve set it up (see above).</li>
90
+ </ul>
91
+
92
+ <h2 id="debugging">Debugging…</h2>
93
+
94
+ <h3 id="models">…Models</h3>
95
+
96
+ <pre><code>def before_save
97
+ DT.p "in before_save"
98
+ end
99
+ </code></pre>
100
+
101
+ <h3 id="controllers">…Controllers</h3>
102
+
103
+ <pre><code>def action
104
+ DT.p "hi, I'm #{action_name}"
105
+ end
106
+ </code></pre>
107
+
108
+ <h3 id="views">…Views</h3>
109
+
110
+ <pre><code>&lt;div class="body"&gt;
111
+ &lt;% DT.p "@users", @users %&gt;
112
+ &lt;/div&gt;
113
+ </code></pre>
114
+
115
+ <h3 id="filters">…Filters</h3>
116
+
117
+ <p>Insert debugging code:</p>
118
+
119
+ <pre><code>before_filter do
120
+ DT.p "in before_filter xyz"
121
+ end
122
+
123
+ after_filter do
124
+ DT.p "in after_filter xyz"
125
+ end
126
+ </code></pre>
127
+
128
+ <p>See it in action:</p>
129
+
130
+ <pre><code>$ tail -f log/dt.log
131
+ </code></pre>
132
+
133
+ <h3 id="anything">…Anything!</h3>
134
+
135
+ <p>Just use <code>DT.p</code> anywhere you want.</p>
136
+
137
+ <h2 id="customizing-output-format">Customizing Output Format</h2>
138
+
139
+ <p>Create a sample initializer, by doing a:</p>
140
+
141
+ <pre><code>$ rails generate rails_dt # Rails 3
142
+ $ script/generate rails_dt # Rails 2
143
+ </code></pre>
144
+
145
+ <p>In <code>config/initializers/dt.rb</code> you’ll see something like:</p>
146
+
147
+ <pre><code># Customize your DT output.
148
+ #DT.log_prefix = "[DT &lt;%= file_rel %&gt;:&lt;%= line %&gt;] "
149
+ #DT.console_prefix = "[DT &lt;%= file_rel %&gt;:&lt;%= line %&gt;] "
150
+ #DT.web_prefix = '&lt;a href="txmt://open?url=file://&lt;%= file %&gt;&amp;line=&lt;%= line %&gt;"&gt;&lt;%= file_rel %&gt;:&lt;%= line %&gt;&lt;/a&gt; '
151
+ </code></pre>
152
+
153
+ <p>Uncomment and edit lines appropriately. Restart server for changes to take effect.</p>
154
+
155
+ <p>Values are in ERB format. The following macros are available:</p>
156
+
157
+ <ul>
158
+ <li><code>file</code> – full path to file.</li>
159
+ <li><code>file_base</code> – file base name.</li>
160
+ <li><code>file_rel</code> – file name relative to Rails application root.</li>
161
+ <li><code>line</code> – line number.</li>
162
+ </ul>
163
+
164
+ <p>You can also disable particular output target by setting its prefix to <code>nil</code>:</p>
165
+
166
+ <pre><code>DT.console_prefix = nil # Disable console output.
167
+ </code></pre>
168
+
169
+ <h2 id="feedback">Feedback</h2>
170
+
171
+ <p>Send bug reports, suggestions and criticisms through <a href="http://github.com/dadooda/rails_dt">project’s page on GitHub</a>.</p>
data/README.md CHANGED
@@ -4,39 +4,70 @@ Rails Debug Toolkit
4
4
  Introduction
5
5
  ------------
6
6
 
7
- `rails_dt` gem gives you `DT.p()` method you can use anywhere in your project to print variable dumps, debug messages etc.
7
+ `rails_dt` gem gives you `DT.p()` method you can use anywhere in your project to print your debug messages.
8
8
 
9
- It's similar to Ruby's native `p()` with output being sent to browser, console and log.
9
+ It's somewhat similar to Ruby's native `p()` with output being sent to log, console and web.
10
10
 
11
+ For example, `DT.p "Hello, world!"` invoked in `RootController` will give you a:
11
12
 
12
- Setup
13
- -----
13
+ [DT app/controllers/root_controller.rb:3] Hello, world!
14
14
 
15
- $ gem sources --add http://rubygems.org
16
- $ gem install rails_dt
17
15
 
18
- In your application root, do a:
16
+ The Ideas Behind It
17
+ -------------------
18
+
19
+ * Debug message printer must **not require initialization**.
20
+ * Debug message printer must be **nothing else**, but a debug message printer.
21
+ * Debug message printer must be simple and invoked **always the same way** regardless of where you call it from.
22
+ * Debug message printer calls must be **clearly visible** in the code.
23
+ * Debug message printer must **print its location in code** so you can find and modify/remove it as easy as possible.
24
+
25
+
26
+ Express Setup (Rails 3)
27
+ -----------------------
28
+
29
+ In your `Gemfile`, add:
30
+
31
+ gem "rails_dt"
19
32
 
20
- $ script/generate rails_dt
33
+ Then do a `bundle install`.
21
34
 
22
- Follow the instructions the generator gives you (they are listed below):
35
+ This gives you an express (zero-conf) setup, which outputs messages to log, `log/dt.log` and console.
36
+
37
+
38
+ Express Setup (Rails 2)
39
+ -----------------------
40
+
41
+ $ gem sources --add http://rubygems.org
42
+ $ gem install rails_dt
23
43
 
24
44
  In your `config/environment.rb`, add:
25
45
 
26
46
  config.gem "rails_dt"
27
47
 
48
+
49
+ Setting Up Web Output (Both Rails 3 and Rails 2)
50
+ ------------------------------------------------
51
+
52
+ In your application root, do a:
53
+
54
+ $ rails generate rails_dt # Rails 3
55
+ $ script/generate rails_dt # Rails 2
56
+
57
+ Follow the instructions the generator gives you then. They are listed below.
58
+
28
59
  Inside your `ApplicationController` class, add:
29
60
 
30
61
  handles_dt
31
62
 
32
- In your `app/views/layouts/application.html.erb` `<head>` section, add:
63
+ Inside your `app/views/layouts/application.html.erb` `<head>` section, add:
33
64
 
34
65
  <%= stylesheet_link_tag "dt" %>
35
66
 
36
- Somewhere at the end of your `app/views/layouts/application.html.erb` `<body>` section, add:
67
+ Inside your `app/views/layouts/application.html.erb` `<body>` section, add:
37
68
 
38
69
  <div class="DT">
39
- <%= DT.to_html %>
70
+ <%= DT.web_messages_as_html %>
40
71
  </div>
41
72
 
42
73
 
@@ -45,9 +76,13 @@ Checking Setup
45
76
 
46
77
  Somewhere in your `app/views/layouts/application.html.erb`, add:
47
78
 
48
- <% DT.p "hello, world" %>
79
+ <% DT.p "hello, world!" %>
49
80
 
50
- Refresh a page that uses this layout. You should see "hello, world" beneath your main page content.
81
+ Refresh the page. You should see "hello, world!":
82
+
83
+ * In your application log.
84
+ * In `log/dt.log`.
85
+ * On the web page, if you've set it up (see above).
51
86
 
52
87
 
53
88
  Debugging...
@@ -89,8 +124,36 @@ See it in action:
89
124
 
90
125
  ### ...Anything! ###
91
126
 
92
- To conclude it, `DT.p` is the universal method you can print your debug messages with.
127
+ Just use `DT.p` anywhere you want.
128
+
129
+
130
+ Customizing Output Format
131
+ -------------------------
132
+
133
+ Create a sample initializer, by doing a:
134
+
135
+ $ rails generate rails_dt # Rails 3
136
+ $ script/generate rails_dt # Rails 2
137
+
138
+ In `config/initializers/dt.rb` you'll see something like:
139
+
140
+ # Customize your DT output.
141
+ #DT.log_prefix = "[DT <%= file_rel %>:<%= line %>] "
142
+ #DT.console_prefix = "[DT <%= file_rel %>:<%= line %>] "
143
+ #DT.web_prefix = '<a href="txmt://open?url=file://<%= file %>&line=<%= line %>"><%= file_rel %>:<%= line %></a> '
144
+
145
+ Uncomment and edit lines appropriately. Restart server for changes to take effect.
146
+
147
+ Values are in ERB format. The following macros are available:
148
+
149
+ * `file` -- full path to file.
150
+ * `file_base` -- file base name.
151
+ * `file_rel` -- file name relative to Rails application root.
152
+ * `line` -- line number.
153
+
154
+ You can also disable particular output target by setting its prefix to `nil`:
93
155
 
156
+ DT.console_prefix = nil # Disable console output.
94
157
 
95
158
 
96
159
  Feedback
@@ -1,4 +1,5 @@
1
1
  ---
2
- :patch: 1
3
2
  :major: 0
4
3
  :minor: 1
4
+ :patch: 2
5
+ #:build: pre1
@@ -0,0 +1,11 @@
1
+ Description:
2
+ Generate files needed for DT web output.
3
+
4
+ Example:
5
+ rails generate rails_dt
6
+
7
+ This will create:
8
+
9
+ * `config/initializers/dt.rb`
10
+ * `public/stylesheets/dt.css`
11
+
@@ -0,0 +1,2 @@
1
+
2
+ WARNING: `USAGE` and `template/*` files are updated with respective files from Rails 3 generator every time gem is rebuilt. Do not edit these files here.
@@ -1,5 +1,4 @@
1
- #class SessionGenerator < Rails::Generator::NamedBase # <-- was
2
- class RailsDtGenerator < Rails::Generator::Base
1
+ class RailsDtGenerator < Rails::Generator::Base #:nodoc:
3
2
  def manifest
4
3
  record do |m|
5
4
  m.file "stylesheets/dt.css", "public/stylesheets/dt.css"
@@ -1,23 +1,20 @@
1
1
 
2
- Finalizing DT installation
3
- ==========================
4
-
5
- In your `config/environment.rb`, add:
6
-
7
- config.gem "rails_dt"
2
+ Setting Up Web Output
3
+ =====================
8
4
 
9
5
  Inside your `ApplicationController` class, add:
10
6
 
11
7
  handles_dt
12
8
 
13
- In your `app/views/layouts/application.html.erb` `<head>` section, add:
9
+ Inside your `app/views/layouts/application.html.erb` `<head>` section, add:
14
10
 
15
11
  <%= stylesheet_link_tag "dt" %>
16
12
 
17
- Somewhere at the end of your `app/views/layouts/application.html.erb` `<body>` section, add:
13
+ Inside your `app/views/layouts/application.html.erb` `<body>` section, add:
18
14
 
19
15
  <div class="DT">
20
- <%= DT.to_html %>
16
+ <%= DT.web_messages_as_html %>
21
17
  </div>
22
18
 
23
19
  Happy debugging!
20
+
@@ -1,4 +1,4 @@
1
1
  # Customize your DT output.
2
- #DT.web_prefix = '<a href="txmt://open?url=file://<%= file %>&line=<%= line %>"><%= file_rel %>:<%= line %></a> '
2
+ #DT.log_prefix = "[DT <%= file_rel %>:<%= line %>] "
3
3
  #DT.console_prefix = "[DT <%= file_rel %>:<%= line %>] "
4
- #DT.log_prefix = DT.console_prefix
4
+ #DT.web_prefix = '<a href="txmt://open?url=file://<%= file %>&line=<%= line %>"><%= file_rel %>:<%= line %></a> '
data/lib/dt.rb CHANGED
@@ -5,147 +5,206 @@ Dir[File.join(File.dirname(__FILE__), "dt/**/*.rb")].each {|fn| require fn}
5
5
 
6
6
  # Debug toolkit.
7
7
  #
8
- # Allows to output debug messages from anywhere in your Rails project.
8
+ # Allows to print debug messages from anywhere in your Rails project. Do a:
9
9
  #
10
- # Configure your application:
11
- # $ script/generate rails_dt
10
+ # DT.p "Hello, world!"
12
11
  #
13
- # Follow the instructions the generator gives you.
12
+ # , and see the message in log, <tt>log/dt.log</tt>, console and web.
14
13
  #
15
- # Then anywhere in your application do a:
16
- # DT.p "Hello, world!"
17
- # DT.p "myvar", myvar
14
+ # To set up web output, in your application root do a:
18
15
  #
19
- # , and see it in web output, console, and <tt>log/dt.log</tt>.
16
+ # $ rails generate rails_dt # Rails 3
17
+ # $ script/generate rails_dt # Rails 2
18
+ #
19
+ # Follow the instructions the generator gives you then.
20
20
  module DT #:doc:
21
- # NOTE: Alphabetical order in this section.
22
-
23
- # Clear messages.
24
- def self.clear
25
- @messages = []
26
- end
21
+ # Maximum number of stored messages, if they're not cleared.
22
+ # If web output is configured, messages are cleared before every request.
23
+ MAX_WEB_MESSAGES = 100
24
+
25
+ # Initializer.
26
+ def self._initialize #:nodoc:
27
+ clear_web_messages
28
+
29
+ # NOTES:
30
+ # * Stuffing is inserted in order to work around buggy RDoc parser.
31
+ # "a""bc" => "abc", just in case.
32
+ # * Don't forget to update generator/initializers/dt.rb with these.
33
+ # * "Canonical" order of imporance: log, console, web.
34
+ @log_prefix = "[DT <""%= file_rel %>:<""%= line %>] "
35
+ @console_prefix = @log_prefix.dup
36
+ @web_prefix = '<a href="txmt://open?url=file://<''%= file %>&line=<''%= line %>"><''%= file_rel %>:<''%= line %></a> '
27
37
 
28
- # Return messages accumulated since last cleared.
29
- def self.messages
30
- @messages
38
+ # In case of path problems @log will be nil.
39
+ @log = Logger.new(Rails.root + "log/dt.log") rescue nil
31
40
  end
32
41
 
33
- # Dump a value or several values. Similar to Ruby's native <tt>p</tt>.
34
- # p "my var: " + myvar.inspect
35
- # p @myobj
36
- def self.p(*args)
37
- # Fetch caller information.
38
- # NOTE: May be lacking file information, e.g. when in an irb session.
39
- file, line = caller.first.split(":")
40
-
41
- # Template variables. Documented in web_prefix=.
42
- hc = {
43
- :file => file,
44
- :line => line,
45
- :file_base => (begin; File.basename(file); rescue; file; end),
46
- :file_rel => (begin; Pathname(file).relative_path_from(Rails.root).to_s; rescue; file; end),
47
- }
48
-
49
- ##return hc
50
-
51
- args.each do |r|
52
- s = r.is_a?(String) ? r : r.inspect
53
-
54
- # NOTE: "Canonical" order of imporance: web, console, log.
55
-
56
- # To Web.
57
- if self.web_prefix
58
- pfx = ERB.new(self.web_prefix, nil, "-").result(_hash_kbinding(hc))
59
-
60
- pcs = []
61
- pcs << pfx
62
- pcs << CGI.escapeHTML(s).gsub("\n", "<br/>\n")
63
- pcs << "<br/>\n"
64
- @messages << pcs.join
65
- end
66
-
67
- # To console.
68
- if self.console_prefix
69
- pfx = ERB.new(self.console_prefix, nil, "-").result(_hash_kbinding(hc))
70
- puts [pfx, s].join
71
- end
72
-
73
- # To log.
74
- if self.log_prefix and @log
75
- pfx = ERB.new(self.log_prefix, nil, "-").result(_hash_kbinding(hc))
76
- @log.info [pfx, s].join
42
+ # On-the-fly initializer.
43
+ def self._otf_init #:nodoc:
44
+ # Consider job done, replace self with a blank.
45
+ class_eval {
46
+ def self._otf_init #:nodoc:
77
47
  end
78
- end
79
-
80
- # Be like puts -- more comfy when debugging in console.
81
- nil
82
- end
83
-
84
- # Format accumulated messages as HTML.
85
- # to_html # => Something like "<ul><li>A message!</li></ul>".
86
- def self.to_html
87
- pcs = []
88
- pcs << "<ul>"
89
- @messages.each do |s|
90
- pcs << ["<li>", s, "</li>"].join
91
- end
92
- pcs << "</ul>"
48
+ }
93
49
 
94
- pcs.join
50
+ _initialize
95
51
  end
96
52
 
97
- #--------------------------------------- Control stuff
98
-
99
- # Set message prefix for console. See <tt>web_prefix=</tt>.
53
+ # Set message prefix for console. See <tt>log_prefix=</tt>.
100
54
  def self.console_prefix=(s)
55
+ _otf_init
101
56
  @console_prefix = s
102
57
  end
103
58
 
104
59
  def self.console_prefix
60
+ _otf_init
105
61
  @console_prefix
106
62
  end
107
63
 
108
64
  # Set logger to use. Must be a <tt>Logger</tt>.
65
+ #
109
66
  # log = Logger.new("log/my.log")
110
67
  def self.log=(obj)
68
+ _otf_init
111
69
  raise "Logger expected, #{obj.class} given" if not obj.is_a? Logger
112
70
  @log = obj
113
71
  end
114
72
 
115
73
  def self.log
74
+ _otf_init
116
75
  @log
117
76
  end
118
77
 
119
- # Set message prefix for log. See <tt>web_prefix=</tt>.
78
+ # Set message prefix for log. Syntax is ERB.
79
+ #
80
+ # log_prefix = "[DT <""%= file_rel %>:<""%= line %>] "
81
+ #
82
+ # NOTE: In the above example some stuffing was made to satisfy the buggy RDoc parser.
83
+ # Just in case, <tt>"a""bc"</tt> is <tt>"abc"</tt> in Ruby.
84
+ #
85
+ # Template variables:
86
+ #
87
+ # * <tt>file</tt> -- full path to file.
88
+ # * <tt>file_base</tt> -- file base name.
89
+ # * <tt>file_rel</tt> -- file name relative to Rails application root.
90
+ # * <tt>line</tt> -- line number.
91
+ #
92
+ # By setting prefix to <tt>nil</tt> you disable respective output.
93
+ #
94
+ # web_prefix = nil # Disable web output.
120
95
  def self.log_prefix=(s)
96
+ _otf_init
121
97
  @log_prefix = s
122
98
  end
123
99
 
124
100
  def self.log_prefix
101
+ _otf_init
125
102
  @log_prefix
126
103
  end
127
104
 
128
- # Set message prefix for web. Syntax is ERB.
129
- # web_prefix = '<a href="txmt://open?url=file://<%= file %>&line=<%= line %>"><%= file_rel %>:<%= line %></a> '
130
- #
131
- # Template variables:
132
- # * <tt>file</tt> -- full path to file
133
- # * <tt>line</tt> -- line number
134
- # * <tt>file_base</tt> -- file base name
135
- # * <tt>file_rel</tt> -- file name relative to Rails application root
136
- #
137
- # By setting prefix to <tt>nil</tt> you disable respective output.
138
- # web_prefix = nil # Web output is disabled now.
105
+ # Return messages accumulated since last cleared.
106
+ def self.web_messages
107
+ _otf_init
108
+ @web_messages
109
+ end
110
+
111
+ # Set message prefix for web. See <tt>log_prefix=</tt>.
139
112
  def self.web_prefix=(s)
113
+ _otf_init
140
114
  @web_prefix = s
141
115
  end
142
116
 
143
117
  def self.web_prefix
118
+ _otf_init
144
119
  @web_prefix
145
120
  end
146
121
 
147
122
  #---------------------------------------
148
123
 
124
+ # Clear messages.
125
+ def self.clear_web_messages
126
+ _otf_init
127
+ @web_messages = []
128
+ end
129
+
130
+ # Print a debug message or dump a value. Somewhat similar to Ruby's native <tt>p</tt>.
131
+ #
132
+ # p "Hello, world!"
133
+ # p "myvar", myvar
134
+ def self.p(*args)
135
+ _otf_init
136
+ # Fetch caller information.
137
+ # NOTE: May be lacking file information, e.g. when in an irb session.
138
+ file, line = caller.first.split(":")
139
+
140
+ # Assign template variables.
141
+ hc = {
142
+ :file => file,
143
+ :line => line,
144
+ :file_base => (begin; File.basename(file); rescue; file; end),
145
+ :file_rel => (begin; Pathname(file).relative_path_from(Rails.root).to_s; rescue; file; end),
146
+ }
147
+
148
+ args.each do |r|
149
+ s = r.is_a?(String) ? r : r.inspect
150
+
151
+ # To log.
152
+ if @log_prefix
153
+ ##Kernel.p "@log", @log #DEBUG
154
+ if @log
155
+ pfx = ERB.new(@log_prefix, nil, "-").result(_hash_kbinding(hc))
156
+ msg = [pfx, s].join
157
+ @log.info msg
158
+ Rails.logger.info msg rescue nil # In case something's wrong with `Rails.logger`.
159
+ end
160
+ end
161
+
162
+ # To console.
163
+ if @console_prefix
164
+ pfx = ERB.new(@console_prefix, nil, "-").result(_hash_kbinding(hc))
165
+ puts [pfx, s].join
166
+ end
167
+
168
+ # To web.
169
+ if @web_prefix
170
+ pfx = ERB.new(@web_prefix, nil, "-").result(_hash_kbinding(hc))
171
+
172
+ pcs = []
173
+ pcs << pfx
174
+ pcs << CGI.escapeHTML(s).gsub("\n", "<br/>\n")
175
+ @web_messages << pcs.join
176
+
177
+ # Rotate messages.
178
+ @web_messages.slice!(0..-(MAX_WEB_MESSAGES + 1))
179
+ end
180
+ end
181
+
182
+ # Be like `puts`, return nil.
183
+ nil
184
+ end
185
+
186
+ # Format accumulated web messages as HTML. Usually called from a view template.
187
+ #
188
+ # web_messages_as_html # => Something like "<ul><li>Message 1</li><li>Message 2</li>...</ul>".
189
+ def self.web_messages_as_html
190
+ _otf_init
191
+
192
+ pcs = []
193
+ pcs << "<ul>"
194
+ @web_messages.each do |s|
195
+ pcs << ["<li>", s, "</li>"].join
196
+ end
197
+ pcs << "</ul>"
198
+
199
+ if (out = pcs.join).respond_to? :html_safe
200
+ out.html_safe
201
+ else
202
+ out
203
+ end
204
+ end
205
+
206
+ #---------------------------------------
207
+
149
208
  # NOTE: Singletons can't be private, so mark them syntactically.
150
209
 
151
210
  # Turn hash's entries into locals and return binding.
@@ -155,36 +214,14 @@ module DT #:doc:
155
214
  bnd = binding
156
215
 
157
216
  _value = nil
158
- h.each do |k, _value|
159
- ##puts "-- k-#{k.inspect} v-#{_value.inspect}"
217
+ h.each do |k, v|
218
+ ##puts "-- k-#{k.inspect} v-#{_value.inspect}" #DEBUG
219
+ _value = v # IMPORTANT: Ruby 1.9 compatibility hack.
160
220
  eval("#{k} = _value", bnd)
161
221
  end
162
222
 
163
223
  bnd
164
224
  end
165
225
 
166
- #--------------------------------------- Initialization
167
-
168
- def self._init #:nodoc:
169
- # Require Rails environment.
170
- if not defined? Rails
171
- raise "Rails environment not found. This module is meaningful in Rails only"
172
- end
173
-
174
- clear
175
-
176
- # NOTE: Don't forget to update generator/initializers/dt.rb with these.
177
- self.web_prefix = '<a href="txmt://open?url=file://<%= file %>&line=<%= line %>"><%= file_rel %>:<%= line %></a> '
178
- self.console_prefix = "[DT <%= file_rel %>:<%= line %>] "
179
- self.log_prefix = self.console_prefix
180
-
181
- # In case of path problems @log will be nil.
182
- @log = begin
183
- Logger.new("log/dt.log")
184
- rescue Exception
185
- end
186
- end
187
-
188
- _init
189
-
226
+ # DO NOT invoke `_initialize` load-time, it won't see Rails3 stuff.
190
227
  end # DT
@@ -5,14 +5,15 @@ module DT
5
5
  end
6
6
 
7
7
  module MetaClassMethods
8
- # Inform framework that our controller handles DT.
8
+ # Enable DT in the controller.
9
+ #
9
10
  # class ApplicationController < ActionController::Base
10
11
  # handles_dt
11
12
  # end
12
13
  def handles_dt
13
14
  # NOTE: after_filter() is nicer and lets debug more, but... It's not getting control when there's a rendering exception, such as 404.
14
- # Use console and log to debug complex stuff like prefilters.
15
- before_filter {DT.clear}
15
+ # Use log and console to debug complex stuff like prefilters.
16
+ before_filter {DT.clear_web_messages}
16
17
  end
17
18
  end # MetaClassMethods
18
19
  end # ActionControllerExtensions
@@ -0,0 +1,11 @@
1
+ Description:
2
+ Generate files needed for DT web output.
3
+
4
+ Example:
5
+ rails generate rails_dt
6
+
7
+ This will create:
8
+
9
+ * `config/initializers/dt.rb`
10
+ * `public/stylesheets/dt.css`
11
+
@@ -0,0 +1,9 @@
1
+ class RailsDtGenerator < Rails::Generators::Base #:nodoc:
2
+ source_root File.expand_path("../templates", __FILE__)
3
+
4
+ def go
5
+ copy_file (fn = "dt.css"), "public/stylesheets/#{fn}"
6
+ copy_file (fn = "dt.rb"), "config/initializers/#{fn}"
7
+ readme "INSTALL"
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+
2
+ Setting Up Web Output
3
+ =====================
4
+
5
+ Inside your `ApplicationController` class, add:
6
+
7
+ handles_dt
8
+
9
+ Inside your `app/views/layouts/application.html.erb` `<head>` section, add:
10
+
11
+ <%= stylesheet_link_tag "dt" %>
12
+
13
+ Inside your `app/views/layouts/application.html.erb` `<body>` section, add:
14
+
15
+ <div class="DT">
16
+ <%= DT.web_messages_as_html %>
17
+ </div>
18
+
19
+ Happy debugging!
20
+
@@ -0,0 +1,27 @@
1
+ /* DT styles. */
2
+
3
+ .DT {
4
+ margin: 10px 0 0 0;
5
+ border-top: 1px dotted;
6
+ padding: 0;
7
+ background: #dfd;
8
+ }
9
+
10
+ .DT ul {
11
+ list-style: none;
12
+ margin: 0;
13
+ padding: 0;
14
+ font-family: monospace;
15
+ font-size: 12px;
16
+ }
17
+
18
+ .DT ul li {
19
+ margin: 2px 0 2px 0;
20
+ }
21
+
22
+ .DT a {
23
+ text-decoration: none;
24
+ color: Black;
25
+ background: #edd;
26
+ padding: 0 2px 0 2px;
27
+ }
@@ -0,0 +1,4 @@
1
+ # Customize your DT output.
2
+ #DT.log_prefix = "[DT <%= file_rel %>:<%= line %>] "
3
+ #DT.console_prefix = "[DT <%= file_rel %>:<%= line %>] "
4
+ #DT.web_prefix = '<a href="txmt://open?url=file://<%= file %>&line=<%= line %>"><%= file_rel %>:<%= line %></a> '
@@ -1,46 +1,54 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rails_dt}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alex Fortuna"]
12
- s.date = %q{2010-03-01}
12
+ s.date = %q{2011-07-10}
13
13
  s.description = %q{Rails Debug Toolkit}
14
14
  s.email = %q{alex.r@askit.org}
15
15
  s.extra_rdoc_files = [
16
+ "README.html",
16
17
  "README.md"
17
18
  ]
18
19
  s.files = [
20
+ "README.html",
19
21
  "README.md",
20
- "VERSION.yml",
21
- "generators/rails_dt/rails_dt_generator.rb",
22
- "generators/rails_dt/templates/INSTALL",
23
- "generators/rails_dt/templates/initializers/dt.rb",
24
- "generators/rails_dt/templates/stylesheets/dt.css",
25
- "init.rb",
26
- "lib/dt.rb",
27
- "lib/dt/action_controller_extensions.rb",
28
- "lib/rails_dt.rb",
29
- "rails_dt.gemspec"
22
+ "VERSION.yml",
23
+ "generators/rails_dt/USAGE",
24
+ "generators/rails_dt/WARNING",
25
+ "generators/rails_dt/rails_dt_generator.rb",
26
+ "generators/rails_dt/templates/INSTALL",
27
+ "generators/rails_dt/templates/dt.css",
28
+ "generators/rails_dt/templates/dt.rb",
29
+ "init.rb",
30
+ "lib/dt.rb",
31
+ "lib/dt/action_controller_extensions.rb",
32
+ "lib/generators/rails_dt/USAGE",
33
+ "lib/generators/rails_dt/rails_dt_generator.rb",
34
+ "lib/generators/rails_dt/templates/INSTALL",
35
+ "lib/generators/rails_dt/templates/dt.css",
36
+ "lib/generators/rails_dt/templates/dt.rb",
37
+ "lib/rails_dt.rb",
38
+ "rails_dt.gemspec"
30
39
  ]
31
40
  s.homepage = %q{http://github.com/dadooda/rails_dt}
32
- s.rdoc_options = ["--charset=UTF-8"]
33
41
  s.require_paths = ["lib"]
34
- s.rubygems_version = %q{1.3.5}
42
+ s.rubygems_version = %q{1.6.2}
35
43
  s.summary = %q{Rails Debug Toolkit}
36
44
 
37
45
  if s.respond_to? :specification_version then
38
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
39
46
  s.specification_version = 3
40
47
 
41
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
49
  else
43
50
  end
44
51
  else
45
52
  end
46
53
  end
54
+
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_dt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ prerelease:
5
+ version: 0.1.2
5
6
  platform: ruby
6
7
  authors:
7
8
  - Alex Fortuna
@@ -9,7 +10,7 @@ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2010-03-01 00:00:00 +03:00
13
+ date: 2011-07-10 00:00:00 +04:00
13
14
  default_executable:
14
15
  dependencies: []
15
16
 
@@ -20,17 +21,26 @@ executables: []
20
21
  extensions: []
21
22
 
22
23
  extra_rdoc_files:
24
+ - README.html
23
25
  - README.md
24
26
  files:
27
+ - README.html
25
28
  - README.md
26
29
  - VERSION.yml
30
+ - generators/rails_dt/USAGE
31
+ - generators/rails_dt/WARNING
27
32
  - generators/rails_dt/rails_dt_generator.rb
28
33
  - generators/rails_dt/templates/INSTALL
29
- - generators/rails_dt/templates/initializers/dt.rb
30
- - generators/rails_dt/templates/stylesheets/dt.css
34
+ - generators/rails_dt/templates/dt.css
35
+ - generators/rails_dt/templates/dt.rb
31
36
  - init.rb
32
37
  - lib/dt.rb
33
38
  - lib/dt/action_controller_extensions.rb
39
+ - lib/generators/rails_dt/USAGE
40
+ - lib/generators/rails_dt/rails_dt_generator.rb
41
+ - lib/generators/rails_dt/templates/INSTALL
42
+ - lib/generators/rails_dt/templates/dt.css
43
+ - lib/generators/rails_dt/templates/dt.rb
34
44
  - lib/rails_dt.rb
35
45
  - rails_dt.gemspec
36
46
  has_rdoc: true
@@ -38,26 +48,26 @@ homepage: http://github.com/dadooda/rails_dt
38
48
  licenses: []
39
49
 
40
50
  post_install_message:
41
- rdoc_options:
42
- - --charset=UTF-8
51
+ rdoc_options: []
52
+
43
53
  require_paths:
44
54
  - lib
45
55
  required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
46
57
  requirements:
47
58
  - - ">="
48
59
  - !ruby/object:Gem::Version
49
60
  version: "0"
50
- version:
51
61
  required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
52
63
  requirements:
53
64
  - - ">="
54
65
  - !ruby/object:Gem::Version
55
66
  version: "0"
56
- version:
57
67
  requirements: []
58
68
 
59
69
  rubyforge_project:
60
- rubygems_version: 1.3.5
70
+ rubygems_version: 1.6.2
61
71
  signing_key:
62
72
  specification_version: 3
63
73
  summary: Rails Debug Toolkit