rails_dt 0.1.4 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.yardopts +2 -0
- data/Gemfile +14 -0
- data/MIT-LICENSE +1 -1
- data/README.md +43 -135
- data/lib/dt.rb +31 -215
- data/lib/dt/config.rb +74 -0
- data/lib/dt/instance.rb +117 -0
- data/lib/rails_dt.rb +3 -2
- data/lib/rails_dt/version.rb +4 -0
- data/rails_dt.gemspec +12 -50
- data/spec/lib/dt/config_spec.rb +76 -0
- data/spec/lib/dt/instance_spec.rb +173 -0
- data/spec/lib/dt_spec.rb +14 -0
- data/spec/lib/rails_dt_spec.rb +6 -0
- data/spec/lib/spec/alias_method_matcher_spec.rb +16 -0
- data/spec/lib/spec/context_when_spec.rb +32 -0
- data/spec/lib/spec/let_m_spec.rb +104 -0
- data/spec/lib/spec/use_custom_let_spec.rb +46 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/support/00extend_x.rb +23 -0
- data/spec/support/alias_method_matcher.rb +20 -0
- data/spec/support/context_when.rb +39 -0
- data/spec/support/let_m.rb +27 -0
- data/spec/support/use_custom_let.rb +80 -0
- metadata +43 -52
- data/README.html +0 -171
- data/Rakefile +0 -68
- data/VERSION.yml +0 -4
- data/generators/rails_dt/USAGE +0 -11
- data/generators/rails_dt/WARNING +0 -2
- data/generators/rails_dt/rails_dt_generator.rb +0 -9
- data/generators/rails_dt/templates/INSTALL +0 -20
- data/generators/rails_dt/templates/dt.css +0 -27
- data/generators/rails_dt/templates/dt.rb +0 -4
- data/init.rb +0 -2
- data/lib/dt/action_controller_extensions.rb +0 -29
- data/lib/generators/rails_dt/USAGE +0 -11
- data/lib/generators/rails_dt/rails_dt_generator.rb +0 -9
- data/lib/generators/rails_dt/templates/INSTALL +0 -20
- data/lib/generators/rails_dt/templates/dt.css +0 -27
- data/lib/generators/rails_dt/templates/dt.rb +0 -4
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 758529d91d7f145d04459c0c1d7b6226ce3c5930
|
4
|
+
data.tar.gz: 1e5173df99b36bc35fa87eafdd072c7b5bcdf85e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d86a581e0b288354b8e7159e56bd962e3270a5f92b8ebc474035eaabfd6bd3fa37a66501c03764d30e7280680b016e67ea430ba01297c87456bdc00609099396
|
7
|
+
data.tar.gz: ff96eb4fad398507f708c2bbe599969140e51a5bde1a9aa3db8dd20a627546d5377e09e667ecdd210e01420d53adbe7e6b804edd6a468623295a7825337bc08f
|
data/.gitignore
ADDED
data/.yardopts
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,162 +1,70 @@
|
|
1
|
-
Rails Debug Toolkit
|
2
|
-
===================
|
3
1
|
|
4
|
-
|
5
|
-
|
2
|
+
Ruby/Rails debug toolkit
|
3
|
+
========================
|
6
4
|
|
7
|
-
`rails_dt` gem gives you `DT.p()` method
|
5
|
+
`rails_dt` gem gives you the `DT.p()` method to print debug messages.
|
8
6
|
|
9
|
-
It's somewhat similar to Ruby's native `p()` with output being sent to log, console and web.
|
10
7
|
|
11
|
-
|
12
|
-
|
13
|
-
[DT app/controllers/root_controller.rb:3] Hello, world!
|
14
|
-
|
15
|
-
|
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
|
-
-----------------------
|
8
|
+
## Usage
|
28
9
|
|
29
10
|
In your `Gemfile`, add:
|
30
11
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
43
|
-
|
44
|
-
In your `config/environment.rb`, add:
|
45
|
-
|
46
|
-
config.gem "rails_dt"
|
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
|
-
|
59
|
-
Inside your `ApplicationController` class, add:
|
60
|
-
|
61
|
-
handles_dt
|
62
|
-
|
63
|
-
Inside your `app/views/layouts/application.html.erb` `<head>` section, add:
|
64
|
-
|
65
|
-
<%= stylesheet_link_tag "dt" %>
|
66
|
-
|
67
|
-
Inside your `app/views/layouts/application.html.erb` `<body>` section, add:
|
68
|
-
|
69
|
-
<div class="DT">
|
70
|
-
<%= DT.web_messages_as_html %>
|
71
|
-
</div>
|
72
|
-
|
73
|
-
|
74
|
-
Checking Setup
|
75
|
-
--------------
|
76
|
-
|
77
|
-
Somewhere in your `app/views/layouts/application.html.erb`, add:
|
78
|
-
|
79
|
-
<% DT.p "hello, world!" %>
|
80
|
-
|
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).
|
86
|
-
|
87
|
-
|
88
|
-
Debugging...
|
89
|
-
------------
|
90
|
-
|
91
|
-
### ...Models ###
|
92
|
-
|
93
|
-
def before_save
|
94
|
-
DT.p "in before_save"
|
95
|
-
end
|
96
|
-
|
97
|
-
### ...Controllers ###
|
98
|
-
|
99
|
-
def action
|
100
|
-
DT.p "hi, I'm #{action_name}"
|
101
|
-
end
|
102
|
-
|
103
|
-
### ...Views ###
|
104
|
-
|
105
|
-
<div class="body">
|
106
|
-
<% DT.p "@users", @users %>
|
107
|
-
</div>
|
108
|
-
|
109
|
-
### ...Filters ###
|
110
|
-
|
111
|
-
Insert debugging code:
|
12
|
+
```ruby
|
13
|
+
gem "rails_dt", "git: https://github.com/dadooda/rails_dt.git"
|
14
|
+
```
|
112
15
|
|
113
|
-
|
114
|
-
DT.p "in before_filter xyz"
|
115
|
-
end
|
16
|
+
Now, in your code, do something like:
|
116
17
|
|
117
|
-
|
118
|
-
|
119
|
-
|
18
|
+
```ruby
|
19
|
+
DT.p "checkpoint 1"
|
20
|
+
DT.p "user", user
|
21
|
+
```
|
120
22
|
|
121
|
-
|
23
|
+
Debug messages are printed to:
|
122
24
|
|
123
|
-
|
25
|
+
* `Rails.logger` in Rails mode (auto-detected);
|
26
|
+
* `STDERR` in non-Rails mode;
|
27
|
+
* `log/dt.log` if `log/` exists in project root and is writable.
|
124
28
|
|
125
|
-
|
29
|
+
This is often handy:
|
126
30
|
|
127
|
-
|
31
|
+
```
|
32
|
+
$ tail -f log/dt.log
|
33
|
+
```
|
128
34
|
|
129
35
|
|
130
|
-
|
131
|
-
-------------------------
|
36
|
+
## The ideas behind it
|
132
37
|
|
133
|
-
|
38
|
+
1. Debug message printer **must not require initialization**.
|
39
|
+
2. Debug message printer **must be nothing else**, but a debug message printer.
|
40
|
+
3. Debug message printer **must be invoked the same way** regardless of place of invocation.
|
41
|
+
4. Debug message printer calls **must be clearly visible** in code.
|
42
|
+
5. Debug message printer **must print its location in code** so you can easily remove the call once debugging is over.
|
134
43
|
|
135
|
-
$ rails generate rails_dt # Rails 3
|
136
|
-
$ script/generate rails_dt # Rails 2
|
137
44
|
|
138
|
-
|
45
|
+
### A few out-of-the-box implementations review
|
139
46
|
|
140
|
-
|
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> '
|
47
|
+
Let me check a few popular out-of-the box implementation used by many of developers against "the ideas" items listed above.
|
144
48
|
|
145
|
-
|
49
|
+
`Rails.logger`:
|
146
50
|
|
147
|
-
|
51
|
+
1. Fail. It only works in Rails. Rails requires initialization.
|
52
|
+
2. (!) Fail. Logger is a production facility.
|
53
|
+
3. So-so. It's not possible to use Rails logger to debug parts of Rails itself.
|
54
|
+
4. (!) Fail. Debugging logger calls look the same as production logger calls.
|
55
|
+
5. Fail. Location in code is not printed.
|
148
56
|
|
149
|
-
|
150
|
-
* `file_base` -- file base name.
|
151
|
-
* `file_rel` -- file name relative to Rails application root.
|
152
|
-
* `line` -- line number.
|
57
|
+
`Kernel::p`:
|
153
58
|
|
154
|
-
|
59
|
+
1. OK.
|
60
|
+
2. OK.
|
61
|
+
3. OK.
|
62
|
+
4. So-so. `p` calls hide well among lines of meaningful code and it isn't always easy to spot them.
|
63
|
+
5. Fail. Location in code is not printed.
|
155
64
|
|
156
|
-
DT.console_prefix = nil # Disable console output.
|
157
65
|
|
66
|
+
## Cheers!
|
158
67
|
|
159
|
-
Feedback
|
160
|
-
--------
|
68
|
+
Feedback of any kind is greatly appreciated.
|
161
69
|
|
162
|
-
|
70
|
+
— Alex Fortuna, © 2010-2017
|
data/lib/dt.rb
CHANGED
@@ -1,227 +1,43 @@
|
|
1
|
-
require "cgi"
|
2
|
-
require "erb"
|
3
1
|
|
4
|
-
|
2
|
+
require "logger"
|
3
|
+
require "pathname"
|
5
4
|
|
6
|
-
#
|
5
|
+
# Ruby/Rails debug toolkit.
|
7
6
|
#
|
8
|
-
#
|
7
|
+
# Features:
|
9
8
|
#
|
10
|
-
#
|
9
|
+
# * As simple as possible.
|
10
|
+
# * Suits Rails projects and stand-alone Ruby projects.
|
11
|
+
# * Has none or minimal dependencies.
|
12
|
+
# * Compatible with Ruby 1.9 and up.
|
11
13
|
#
|
12
|
-
#
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
module DT #:doc:
|
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> '
|
37
|
-
|
38
|
-
# In case of path problems @log will be nil.
|
39
|
-
@log = Logger.new(Rails.root + "log/dt.log") rescue nil
|
40
|
-
end
|
41
|
-
|
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:
|
47
|
-
end
|
48
|
-
}
|
49
|
-
|
50
|
-
_initialize
|
51
|
-
end
|
52
|
-
|
53
|
-
# Set message prefix for console. See <tt>log_prefix=</tt>.
|
54
|
-
def self.console_prefix=(s)
|
55
|
-
_otf_init
|
56
|
-
@console_prefix = s
|
57
|
-
end
|
58
|
-
|
59
|
-
def self.console_prefix
|
60
|
-
_otf_init
|
61
|
-
@console_prefix
|
62
|
-
end
|
63
|
-
|
64
|
-
# Set logger to use. Must be a <tt>Logger</tt>.
|
65
|
-
#
|
66
|
-
# log = Logger.new("log/my.log")
|
67
|
-
def self.log=(obj)
|
68
|
-
_otf_init
|
69
|
-
raise "Logger expected, #{obj.class} given" if not obj.is_a? Logger
|
70
|
-
@log = obj
|
71
|
-
end
|
72
|
-
|
73
|
-
def self.log
|
74
|
-
_otf_init
|
75
|
-
@log
|
76
|
-
end
|
77
|
-
|
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.
|
95
|
-
def self.log_prefix=(s)
|
96
|
-
_otf_init
|
97
|
-
@log_prefix = s
|
98
|
-
end
|
99
|
-
|
100
|
-
def self.log_prefix
|
101
|
-
_otf_init
|
102
|
-
@log_prefix
|
103
|
-
end
|
104
|
-
|
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>.
|
112
|
-
def self.web_prefix=(s)
|
113
|
-
_otf_init
|
114
|
-
@web_prefix = s
|
115
|
-
end
|
116
|
-
|
117
|
-
def self.web_prefix
|
118
|
-
_otf_init
|
119
|
-
@web_prefix
|
120
|
-
end
|
14
|
+
# @see DT.p
|
15
|
+
module DT
|
16
|
+
require_relative "dt/config"
|
17
|
+
require_relative "dt/instance"
|
121
18
|
|
122
|
-
|
19
|
+
class << self
|
20
|
+
attr_writer :conf, :instance
|
123
21
|
|
124
|
-
|
125
|
-
|
126
|
-
|
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
|
22
|
+
# @return [Config]
|
23
|
+
def conf
|
24
|
+
@conf ||= Config.new
|
180
25
|
end
|
181
26
|
|
182
|
-
#
|
183
|
-
|
184
|
-
|
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
|
27
|
+
# @return [Instance]
|
28
|
+
def instance
|
29
|
+
@instance ||= Instance.new
|
203
30
|
end
|
204
|
-
end
|
205
|
-
|
206
|
-
#---------------------------------------
|
207
31
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
#
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
h.each do |k, v|
|
218
|
-
##puts "-- k-#{k.inspect} v-#{_value.inspect}" #DEBUG
|
219
|
-
_value = v # IMPORTANT: Ruby 1.9 compatibility hack.
|
220
|
-
eval("#{k} = _value", bnd)
|
32
|
+
# Print a debug message, dump values etc.
|
33
|
+
#
|
34
|
+
# DT.p "checkpoint 1"
|
35
|
+
# DT.p "user", user
|
36
|
+
#
|
37
|
+
# @return [nil]
|
38
|
+
# @see Instance#_p
|
39
|
+
def p(*args)
|
40
|
+
instance._p(caller, *args)
|
221
41
|
end
|
222
|
-
|
223
|
-
|
224
|
-
end
|
225
|
-
|
226
|
-
# DO NOT invoke `_initialize` load-time, it won't see Rails3 stuff.
|
227
|
-
end # DT
|
42
|
+
end # class << self
|
43
|
+
end
|