rails_dt 0.1.4 → 1.2.0

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.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/.yardopts +2 -0
  4. data/Gemfile +14 -0
  5. data/MIT-LICENSE +1 -1
  6. data/README.md +43 -135
  7. data/lib/dt.rb +31 -215
  8. data/lib/dt/config.rb +74 -0
  9. data/lib/dt/instance.rb +117 -0
  10. data/lib/rails_dt.rb +3 -2
  11. data/lib/rails_dt/version.rb +4 -0
  12. data/rails_dt.gemspec +12 -50
  13. data/spec/lib/dt/config_spec.rb +76 -0
  14. data/spec/lib/dt/instance_spec.rb +173 -0
  15. data/spec/lib/dt_spec.rb +14 -0
  16. data/spec/lib/rails_dt_spec.rb +6 -0
  17. data/spec/lib/spec/alias_method_matcher_spec.rb +16 -0
  18. data/spec/lib/spec/context_when_spec.rb +32 -0
  19. data/spec/lib/spec/let_m_spec.rb +104 -0
  20. data/spec/lib/spec/use_custom_let_spec.rb +46 -0
  21. data/spec/spec_helper.rb +6 -0
  22. data/spec/support/00extend_x.rb +23 -0
  23. data/spec/support/alias_method_matcher.rb +20 -0
  24. data/spec/support/context_when.rb +39 -0
  25. data/spec/support/let_m.rb +27 -0
  26. data/spec/support/use_custom_let.rb +80 -0
  27. metadata +43 -52
  28. data/README.html +0 -171
  29. data/Rakefile +0 -68
  30. data/VERSION.yml +0 -4
  31. data/generators/rails_dt/USAGE +0 -11
  32. data/generators/rails_dt/WARNING +0 -2
  33. data/generators/rails_dt/rails_dt_generator.rb +0 -9
  34. data/generators/rails_dt/templates/INSTALL +0 -20
  35. data/generators/rails_dt/templates/dt.css +0 -27
  36. data/generators/rails_dt/templates/dt.rb +0 -4
  37. data/init.rb +0 -2
  38. data/lib/dt/action_controller_extensions.rb +0 -29
  39. data/lib/generators/rails_dt/USAGE +0 -11
  40. data/lib/generators/rails_dt/rails_dt_generator.rb +0 -9
  41. data/lib/generators/rails_dt/templates/INSTALL +0 -20
  42. data/lib/generators/rails_dt/templates/dt.css +0 -27
  43. data/lib/generators/rails_dt/templates/dt.rb +0 -4
@@ -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
@@ -0,0 +1,16 @@
1
+ # General Ruby, sorted by first letter.
2
+ /.bundle/
3
+ /doc/*
4
+ /.rspec
5
+ /vendor/bundle/
6
+ /.yardoc/
7
+
8
+ # Project-specific.
9
+ /Gemfile.lock
10
+ /log/
11
+
12
+ # Bak/old/orig etc.
13
+ *.old*
14
+ *.orig
15
+ /*.patch
16
+ *.ref*
@@ -0,0 +1,2 @@
1
+
2
+ --private
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+
2
+ source "https://rubygems.org"
3
+
4
+ # Self.
5
+ gem "rails_dt", path: (Pathname(__FILE__) + "..").realpath
6
+
7
+ group :development do
8
+ gem "rspec"
9
+
10
+ # YARD and friends.
11
+ gem "rdoc"
12
+ gem "redcarpet"
13
+ gem "yard"
14
+ end
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Alex Fortuna
1
+ Copyright (c) 2010-2017 Alex Fortuna
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,162 +1,70 @@
1
- Rails Debug Toolkit
2
- ===================
3
1
 
4
- Introduction
5
- ------------
2
+ Ruby/Rails debug toolkit
3
+ ========================
6
4
 
7
- `rails_dt` gem gives you `DT.p()` method you can use anywhere in your project to print your debug messages.
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
- For example, `DT.p "Hello, world!"` invoked in `RootController` will give you a:
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
- gem "rails_dt"
32
-
33
- Then do a `bundle install`.
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
- before_filter do
114
- DT.p "in before_filter xyz"
115
- end
16
+ Now, in your code, do something like:
116
17
 
117
- after_filter do
118
- DT.p "in after_filter xyz"
119
- end
18
+ ```ruby
19
+ DT.p "checkpoint 1"
20
+ DT.p "user", user
21
+ ```
120
22
 
121
- See it in action:
23
+ Debug messages are printed to:
122
24
 
123
- $ tail -f log/dt.log
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
- ### ...Anything! ###
29
+ This is often handy:
126
30
 
127
- Just use `DT.p` anywhere you want.
31
+ ```
32
+ $ tail -f log/dt.log
33
+ ```
128
34
 
129
35
 
130
- Customizing Output Format
131
- -------------------------
36
+ ## The ideas behind it
132
37
 
133
- Create a sample initializer, by doing a:
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
- In `config/initializers/dt.rb` you'll see something like:
45
+ ### A few out-of-the-box implementations review
139
46
 
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> '
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
- Uncomment and edit lines appropriately. Restart server for changes to take effect.
49
+ `Rails.logger`:
146
50
 
147
- Values are in ERB format. The following macros are available:
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
- * `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.
57
+ `Kernel::p`:
153
58
 
154
- You can also disable particular output target by setting its prefix to `nil`:
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
- Send bug reports, suggestions and criticisms through [project's page on GitHub](http://github.com/dadooda/rails_dt).
70
+ &mdash; Alex Fortuna, &copy; 2010-2017
data/lib/dt.rb CHANGED
@@ -1,227 +1,43 @@
1
- require "cgi"
2
- require "erb"
3
1
 
4
- Dir[File.join(File.dirname(__FILE__), "dt/**/*.rb")].each {|fn| require fn}
2
+ require "logger"
3
+ require "pathname"
5
4
 
6
- # Debug toolkit.
5
+ # Ruby/Rails debug toolkit.
7
6
  #
8
- # Allows to print debug messages from anywhere in your Rails project. Do a:
7
+ # Features:
9
8
  #
10
- # DT.p "Hello, world!"
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
- # , and see the message in log, <tt>log/dt.log</tt>, console and web.
13
- #
14
- # To set up web output, in your application root do a:
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
- # 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
22
+ # @return [Config]
23
+ def conf
24
+ @conf ||= Config.new
180
25
  end
181
26
 
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
27
+ # @return [Instance]
28
+ def instance
29
+ @instance ||= Instance.new
203
30
  end
204
- end
205
-
206
- #---------------------------------------
207
31
 
208
- # NOTE: Singletons can't be private, so mark them syntactically.
209
-
210
- # Turn hash's entries into locals and return binding.
211
- # Useful for simple templating.
212
- def self._hash_kbinding(h) #:nodoc:
213
- # NOTE: This IS important, since assignment is eval'd in this context.
214
- bnd = binding
215
-
216
- _value = nil
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
- bnd
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