rails_dt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,80 @@
1
+ Rails Debug Toolkit
2
+ ===================
3
+
4
+ Introduction
5
+ ------------
6
+
7
+ `rails_dt` gem gives you `DT.p()` method you can use anywhere in your project to print variable dumps, debug messages etc.
8
+
9
+ It's similar to Ruby's native `p()` with output being sent to browser, console and log.
10
+
11
+
12
+ Setup
13
+ -----
14
+
15
+ $ gem sources --add http://rubygems.org
16
+ $ gem install rails_dt
17
+
18
+ In your application root, do a:
19
+
20
+ $ script/generate rails_dt
21
+
22
+ Follow the instructions the generator gives you (they are listed below):
23
+
24
+ In your `app/controllers/application_controller`, add:
25
+
26
+ handles_dt
27
+
28
+ In your `app/views/layouts/application.html.erb` `<head>` section, add:
29
+
30
+ <%= stylesheet_link_tag "dt" %>
31
+
32
+ Somewhere at the end of your `app/views/layouts/application.html.erb` `<body>` section, add:
33
+
34
+ <div class="DT">
35
+ <%= DT.to_html %>
36
+ </div>
37
+
38
+
39
+ Debugging...
40
+ ------------
41
+
42
+ ### ...models ###
43
+
44
+ def before_save
45
+ DT.p "in before_save"
46
+ end
47
+
48
+ ### ...controllers ###
49
+
50
+ def action
51
+ DT.p "hi, I'm #{action_name}"
52
+ end
53
+
54
+ ### ...views ###
55
+
56
+ <div class="body">
57
+ <% DT.p "@users", @users %>
58
+ </div>
59
+
60
+ ### ...filters ###
61
+
62
+ Insert debugging code:
63
+
64
+ before_filter do
65
+ DT.p "in before_filter xyz"
66
+ end
67
+
68
+ after_filter do
69
+ DT.p "in after_filter xyz"
70
+ end
71
+
72
+ See it in action:
73
+
74
+ $ tail -f log/dt.log
75
+
76
+
77
+ Feedback
78
+ --------
79
+
80
+ Send bug reports, suggestions and criticisms through [project's page on GitHub](http://github.com/dadooda/rails_dt).
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 0
3
+ :major: 0
4
+ :minor: 1
@@ -0,0 +1,10 @@
1
+ #class SessionGenerator < Rails::Generator::NamedBase # <-- was
2
+ class RailsDtGenerator < Rails::Generator::Base
3
+ def manifest
4
+ record do |m|
5
+ m.file "stylesheets/dt.css", "public/stylesheets/dt.css"
6
+ m.file "initializers/dt.rb", "config/initializers/dt.rb"
7
+ m.readme "INSTALL"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+
2
+ Finalizing DT installation
3
+ ==========================
4
+
5
+ In your `app/controllers/application_controller` add:
6
+
7
+ handles_dt
8
+
9
+ In your `app/views/layouts/application.html.erb` `<head>` section add:
10
+
11
+ <%= stylesheet_link_tag "dt" %>
12
+
13
+ Somewhere at the end of your `app/views/layouts/application.html.erb` `<body>` section add:
14
+
15
+ <div class="DT">
16
+ <%= DT.to_html %>
17
+ </div>
18
+
19
+ Happy debugging!
20
+
@@ -0,0 +1,4 @@
1
+ # Customize your DT output.
2
+ #DT.web_prefix = '<a href="txmt://open?url=file://<%= file %>&line=<%= line %>"><%= file_rel %>:<%= line %></a> '
3
+ #DT.console_prefix = "[DT <%= file_rel %>:<%= line %>] "
4
+ #DT.log_prefix = DT.console_prefix
@@ -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
+ }
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Rails plugin init.
2
+ require "rails_dt"
@@ -0,0 +1,189 @@
1
+ require "cgi"
2
+ require "erb"
3
+
4
+ Dir[File.join(File.dirname(__FILE__), "dt/**/*.rb")].each {|fn| require fn}
5
+
6
+ # Debug toolkit.
7
+ #
8
+ # Allows to output debug messages from anywhere in your Rails project.
9
+ #
10
+ # Configure your application:
11
+ # $ script/generate rails_dt
12
+ #
13
+ # Follow the instructions the generator gives you.
14
+ #
15
+ # Then anywhere in your application do a:
16
+ # DT.p "Hello, world!"
17
+ # DT.p "myvar", myvar
18
+ #
19
+ # , and see it in web output, console, and <tt>log/dt.log</tt>.
20
+ module DT #:doc:
21
+ # NOTE: Alphabetical order in this section.
22
+
23
+ # Clear messages.
24
+ def self.clear
25
+ @messages = []
26
+ end
27
+
28
+ # Return messages accumulated since last cleared.
29
+ def self.messages
30
+ @messages
31
+ end
32
+
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
+ c = caller.first.split(":")
39
+
40
+ # Template variables. Documented in web_prefix=.
41
+ hc = {
42
+ :file => c[0],
43
+ :line => c[1],
44
+ :file_base => File.basename(c[0]),
45
+ :file_rel => Pathname(c[0]).relative_path_from(Rails.root).to_s,
46
+ }
47
+
48
+ ##return hc
49
+
50
+ args.each do |r|
51
+ s = r.is_a?(String) ? r : r.inspect
52
+
53
+ # NOTE: "Canonical" order of imporance: web, console, log.
54
+
55
+ # To Web.
56
+ if self.web_prefix
57
+ pfx = ERB.new(self.web_prefix, nil, "-").result(_hash_kbinding(hc))
58
+
59
+ pcs = []
60
+ pcs << pfx
61
+ pcs << CGI.escapeHTML(s).gsub("\n", "<br/>\n")
62
+ pcs << "<br/>\n"
63
+ @messages << pcs.join
64
+ end
65
+
66
+ # To console.
67
+ if self.console_prefix
68
+ pfx = ERB.new(self.console_prefix, nil, "-").result(_hash_kbinding(hc))
69
+ puts [pfx, s].join
70
+ end
71
+
72
+ # To log.
73
+ if self.log_prefix and @log
74
+ pfx = ERB.new(self.log_prefix, nil, "-").result(_hash_kbinding(hc))
75
+ @log.info [pfx, s].join
76
+ end
77
+ end
78
+
79
+ # Be like puts -- more comfy when debugging in console.
80
+ nil
81
+ end
82
+
83
+ # Format accumulated messages as HTML.
84
+ # to_html # => Something like "<ul><li>A message!</li></ul>".
85
+ def self.to_html
86
+ pcs = []
87
+ pcs << "<ul>"
88
+ @messages.each do |s|
89
+ pcs << ["<li>", s, "</li>"].join
90
+ end
91
+ pcs << "</ul>"
92
+
93
+ pcs.join
94
+ end
95
+
96
+ #--------------------------------------- Control stuff
97
+
98
+ # Set message prefix for console. See <tt>web_prefix=</tt>.
99
+ def self.console_prefix=(s)
100
+ @console_prefix = s
101
+ end
102
+
103
+ def self.console_prefix
104
+ @console_prefix
105
+ end
106
+
107
+ # Set logger to use. Must be a <tt>Logger</tt>.
108
+ # log = Logger.new("log/my.log")
109
+ def self.log=(obj)
110
+ raise "Logger expected, #{obj.class} given" if not obj.is_a? Logger
111
+ @log = obj
112
+ end
113
+
114
+ def self.log
115
+ @log
116
+ end
117
+
118
+ # Set message prefix for log. See <tt>web_prefix=</tt>.
119
+ def self.log_prefix=(s)
120
+ @log_prefix = s
121
+ end
122
+
123
+ def self.log_prefix
124
+ @log_prefix
125
+ end
126
+
127
+ # Set message prefix for web. Syntax is ERB.
128
+ # web_prefix = '<a href="txmt://open?url=file://<%= file %>&line=<%= line %>"><%= file_rel %>:<%= line %></a> '
129
+ #
130
+ # Template variables:
131
+ # * <tt>file</tt> -- full path to file
132
+ # * <tt>line</tt> -- line number
133
+ # * <tt>file_base</tt> -- file base name
134
+ # * <tt>file_rel</tt> -- file name relative to Rails application root
135
+ #
136
+ # By setting prefix to <tt>nil</tt> you disable respective output.
137
+ # web_prefix = nil # Web output is disabled now.
138
+ def self.web_prefix=(s)
139
+ @web_prefix = s
140
+ end
141
+
142
+ def self.web_prefix
143
+ @web_prefix
144
+ end
145
+
146
+ #---------------------------------------
147
+
148
+ # NOTE: Singletons can't be private, so mark them syntactically.
149
+
150
+ # Turn hash's entries into locals and return binding.
151
+ # Useful for simple templating.
152
+ def self._hash_kbinding(h) #:nodoc:
153
+ # NOTE: This IS important, since assignment is eval'd in this context.
154
+ bnd = binding
155
+
156
+ _value = nil
157
+ h.each do |k, _value|
158
+ ##puts "-- k-#{k.inspect} v-#{_value.inspect}"
159
+ eval("#{k} = _value", bnd)
160
+ end
161
+
162
+ bnd
163
+ end
164
+
165
+ #--------------------------------------- Initialization
166
+
167
+ def self._init #:nodoc:
168
+ # Require Rails environment.
169
+ if not defined? Rails
170
+ raise "Rails environment not found. This module is meaningful in Rails only"
171
+ end
172
+
173
+ clear
174
+
175
+ # NOTE: Don't forget to update generator/initializers/dt.rb with these.
176
+ self.web_prefix = '<a href="txmt://open?url=file://<%= file %>&line=<%= line %>"><%= file_rel %>:<%= line %></a> '
177
+ self.console_prefix = "[DT <%= file_rel %>:<%= line %>] "
178
+ self.log_prefix = self.console_prefix
179
+
180
+ # In case of path problems @log will be nil.
181
+ @log = begin
182
+ Logger.new("log/dt.log")
183
+ rescue Exception
184
+ end
185
+ end
186
+
187
+ _init
188
+
189
+ end # DT
@@ -0,0 +1,28 @@
1
+ module DT
2
+ module ActionControllerExtensions #:nodoc:
3
+ def self.included(owner) #:nodoc:
4
+ owner.extend MetaClassMethods
5
+ end
6
+
7
+ module MetaClassMethods
8
+ # Inform framework that our controller handles DT.
9
+ # class ApplicationController < ActionController::Base
10
+ # handles_dt
11
+ # end
12
+ def handles_dt
13
+ # 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}
16
+ end
17
+ end # MetaClassMethods
18
+ end # ActionControllerExtensions
19
+ end # DT
20
+
21
+ # NOTE: This MUST be outside DT module, otherwise rdoc will produce ugly doc for DT module.
22
+ if defined? ::ActionController::Base
23
+ module ::ActionController #:nodoc:
24
+ class Base #:nodoc:
25
+ include DT::ActionControllerExtensions
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,2 @@
1
+ # A bridge between names.
2
+ require File.join(File.dirname(__FILE__), "dt")
@@ -0,0 +1,46 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rails_dt}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Alex Fortuna"]
12
+ s.date = %q{2010-02-27}
13
+ s.description = %q{Rails Debug Toolkit}
14
+ s.email = %q{alex.r@askit.org}
15
+ s.extra_rdoc_files = [
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ "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"
30
+ ]
31
+ s.homepage = %q{http://github.com/dadooda/rails_dt}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{Rails Debug Toolkit}
36
+
37
+ if s.respond_to? :specification_version then
38
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
42
+ else
43
+ end
44
+ else
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_dt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Fortuna
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-27 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Rails Debug Toolkit
17
+ email: alex.r@askit.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.md
24
+ files:
25
+ - README.md
26
+ - VERSION.yml
27
+ - generators/rails_dt/rails_dt_generator.rb
28
+ - generators/rails_dt/templates/INSTALL
29
+ - generators/rails_dt/templates/initializers/dt.rb
30
+ - generators/rails_dt/templates/stylesheets/dt.css
31
+ - init.rb
32
+ - lib/dt.rb
33
+ - lib/dt/action_controller_extensions.rb
34
+ - lib/rails_dt.rb
35
+ - rails_dt.gemspec
36
+ has_rdoc: true
37
+ homepage: http://github.com/dadooda/rails_dt
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --charset=UTF-8
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.5
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Rails Debug Toolkit
64
+ test_files: []
65
+