awesome_print 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +10 -0
- data/README.md +41 -22
- data/Rakefile +9 -29
- data/VERSION +1 -1
- data/init.rb +1 -1
- data/lib/ap.rb +17 -10
- data/lib/ap/awesome_print.rb +35 -15
- data/lib/ap/core_ext/array.rb +8 -4
- data/lib/ap/core_ext/class.rb +2 -2
- data/lib/ap/core_ext/kernel.rb +1 -0
- data/lib/ap/core_ext/object.rb +2 -2
- data/lib/ap/core_ext/string.rb +8 -7
- data/lib/ap/mixin/action_view.rb +2 -23
- data/lib/ap/mixin/mongo_mapper.rb +54 -0
- data/lib/awesome_print.rb +14 -12
- data/rails/init.rb +0 -4
- data/spec/action_view_spec.rb +16 -26
- data/spec/active_record_spec.rb +73 -71
- data/spec/awesome_print_spec.rb +55 -2
- data/spec/colorization_spec.rb +84 -0
- data/spec/logger_spec.rb +1 -1
- data/spec/methods_spec.rb +19 -0
- data/spec/mongo_mapper_spec.rb +63 -0
- data/spec/spec_helper.rb +37 -14
- metadata +22 -18
data/CHANGELOG
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
0.4.0
|
2
|
+
- 'ap object' now returns the object (Stephan Hagemann)
|
3
|
+
- Added :html => true option to enable HTML colors rather that ANSI (ex. Sinatra templates)
|
4
|
+
- Added AwesomePrint.force_colors! to allow color output on demand (Andrew O'Brien)
|
5
|
+
- Added MongoMapper formatter mixin (Elpizo Choi)
|
6
|
+
- Fixed formatting of methods array when object#method is overridden
|
7
|
+
- Fixed potential stack errors by checking whether AwesomePrint is already loaded
|
8
|
+
- Improved Ruby 1.8.6 and 1.8.7 compatibility
|
9
|
+
- Improved Windows compatibility (Viktar Basharymau)
|
10
|
+
|
1
11
|
0.3.2
|
2
12
|
- Make sure Rails mixins get loaded in Rails console when required from .irbrc
|
3
13
|
- Fixed an issue with classes that define their own #send method (ex: Socket)
|
data/README.md
CHANGED
@@ -15,15 +15,16 @@ objects and usage within Rails templates are supported via included mixins.
|
|
15
15
|
|
16
16
|
### Usage ###
|
17
17
|
|
18
|
-
require "
|
18
|
+
require "awesome_print"
|
19
19
|
ap object, options = {}
|
20
20
|
|
21
21
|
Default options:
|
22
22
|
|
23
|
-
:multiline => true, # Display in
|
24
|
-
:plain
|
25
|
-
:indent
|
26
|
-
:index
|
23
|
+
:multiline => true, # Display in multiple lines.
|
24
|
+
:plain => false, # Use colors.
|
25
|
+
:indent => 4, # Indent using 4 spaces.
|
26
|
+
:index => true, # Display array indices.
|
27
|
+
:html => false, # Use ANSI color codes rather than HTML.
|
27
28
|
:sorted_hash_keys => false, # Do not sort hash keys.
|
28
29
|
:color => {
|
29
30
|
:array => :white,
|
@@ -48,7 +49,7 @@ Supported color names:
|
|
48
49
|
|
49
50
|
### Examples ###
|
50
51
|
$ cat > 1.rb
|
51
|
-
require "
|
52
|
+
require "awesome_print"
|
52
53
|
data = [ false, 42, %w(forty two), { :now => Time.now, :class => Time.now.class, :distance => 42e42 } ]
|
53
54
|
ap data
|
54
55
|
^D
|
@@ -68,7 +69,7 @@ Supported color names:
|
|
68
69
|
]
|
69
70
|
|
70
71
|
$ cat > 2.rb
|
71
|
-
require "
|
72
|
+
require "awesome_print"
|
72
73
|
data = { :now => Time.now, :class => Time.now.class, :distance => 42e42 }
|
73
74
|
ap data, :indent => -2 # <-- Left align hash keys.
|
74
75
|
^D
|
@@ -80,7 +81,7 @@ Supported color names:
|
|
80
81
|
}
|
81
82
|
|
82
83
|
$ cat > 3.rb
|
83
|
-
require "
|
84
|
+
require "awesome_print"
|
84
85
|
data = [ false, 42, %w(forty two) ]
|
85
86
|
data << data # <-- Nested array.
|
86
87
|
ap data, :multiline => false
|
@@ -89,7 +90,7 @@ Supported color names:
|
|
89
90
|
[ false, 42, [ "forty", "two" ], [...] ]
|
90
91
|
|
91
92
|
$ cat > 4.rb
|
92
|
-
require "
|
93
|
+
require "awesome_print"
|
93
94
|
class Hello
|
94
95
|
def self.world(x, y, z = nil, &blk)
|
95
96
|
end
|
@@ -102,7 +103,7 @@ Supported color names:
|
|
102
103
|
]
|
103
104
|
|
104
105
|
$ cat > 5.rb
|
105
|
-
require "
|
106
|
+
require "awesome_print"
|
106
107
|
ap (''.methods - Object.methods).grep(/!/)
|
107
108
|
^D
|
108
109
|
$ ruby 5.rb
|
@@ -129,10 +130,17 @@ Supported color names:
|
|
129
130
|
[19] upcase!() String
|
130
131
|
]
|
131
132
|
|
133
|
+
$ cat > 6.rb
|
134
|
+
require "awesome_print"
|
135
|
+
ap 42 == ap(42)
|
136
|
+
^D
|
137
|
+
$ ruby 6.rb
|
138
|
+
42
|
139
|
+
true
|
140
|
+
|
132
141
|
### Example (Rails console) ###
|
133
|
-
$
|
134
|
-
|
135
|
-
rails> require "ap"
|
142
|
+
$ rails console
|
143
|
+
rails> require "awesome_print"
|
136
144
|
rails> ap Account.all(:limit => 2)
|
137
145
|
[
|
138
146
|
[0] #<Account:0x1033220b8> {
|
@@ -192,7 +200,7 @@ To use awesome_print as default formatter in irb and Rails console add the follo
|
|
192
200
|
lines into your ~/.irbrc file:
|
193
201
|
|
194
202
|
require "rubygems"
|
195
|
-
require "
|
203
|
+
require "awesome_print"
|
196
204
|
|
197
205
|
unless IRB.version.include?('DietRB')
|
198
206
|
IRB::Irb.class_eval do
|
@@ -209,25 +217,30 @@ lines into your ~/.irbrc file:
|
|
209
217
|
end
|
210
218
|
|
211
219
|
### Logger Convenience Method ###
|
212
|
-
awesome_print adds
|
213
|
-
|
220
|
+
awesome_print adds the 'ap' method to the Logger and ActiveSupport::BufferedLogger classes
|
221
|
+
letting you call:
|
214
222
|
|
215
223
|
logger.ap object
|
216
224
|
|
217
|
-
By default, this logs at the :debug level. You can override that globally with
|
225
|
+
By default, this logs at the :debug level. You can override that globally with:
|
218
226
|
|
219
227
|
:log_level => :info
|
220
228
|
|
221
|
-
in the custom defaults (see below)
|
229
|
+
in the custom defaults (see below). You can also override on a per call basis with:
|
222
230
|
|
223
231
|
logger.ap object, :warn
|
224
232
|
|
225
233
|
### ActionView Convenience Method ###
|
226
|
-
awesome_print adds
|
234
|
+
awesome_print adds the 'ap' method to the ActionView::Base class making it available
|
227
235
|
within Rails templates. For example:
|
228
236
|
|
229
237
|
<%= ap @accounts.first %>
|
230
238
|
|
239
|
+
With other web frameworks (ex: in Sinatra templates) you can explicitly request HTML
|
240
|
+
formatting:
|
241
|
+
|
242
|
+
<%= ap @accounts.first, :html => true %>
|
243
|
+
|
231
244
|
### Setting Custom Defaults ###
|
232
245
|
You can set your own default options by creating ``.aprc`` file in your home
|
233
246
|
directory. Within that file assign your defaults to ``AwesomePrint.defaults``.
|
@@ -244,9 +257,9 @@ For example:
|
|
244
257
|
|
245
258
|
### Running Specs ###
|
246
259
|
|
247
|
-
$
|
248
|
-
$
|
249
|
-
$ rspec spec/logger_spec.rb
|
260
|
+
$ gem install rspec # RSpec 2.x is the requirement.
|
261
|
+
$ rake spec # Run the entire spec suite.
|
262
|
+
$ rspec spec/logger_spec.rb # Run individual spec file.
|
250
263
|
|
251
264
|
### Note on Patches/Pull Requests ###
|
252
265
|
* Fork the project on Github.
|
@@ -257,16 +270,22 @@ For example:
|
|
257
270
|
|
258
271
|
### Contributors ###
|
259
272
|
|
273
|
+
* Andrew O'Brien -- https://github.com/AndrewO
|
260
274
|
* Daniel Bretoi -- http://github.com/danielb2
|
261
275
|
* Eloy Duran -- http://github.com/alloy
|
276
|
+
* Elpizo Choi -- https://github.com/fuJiin
|
262
277
|
* Benoit Daloze -- http://github.com/eregon
|
263
278
|
* Sean Gallagher -- http://github.com/torandu
|
279
|
+
* Stephan Hagemann -- https://github.com/shageman
|
264
280
|
* Tim Harper -- http://github.com/timcharper
|
265
281
|
* Tobias Crawley -- http://github.com/tobias
|
282
|
+
* Viktar Basharymau -- https://github.com/DNNX
|
266
283
|
|
267
284
|
### License ###
|
268
285
|
Copyright (c) 2010-2011 Michael Dvorkin
|
286
|
+
|
269
287
|
twitter.com/mid
|
288
|
+
|
270
289
|
%w(mike dvorkin.net) * "@" || %w(mike fatfreecrm.com) * "@"
|
271
290
|
|
272
291
|
Released under the MIT license. See LICENSE file for details.
|
data/Rakefile
CHANGED
@@ -11,13 +11,8 @@ begin
|
|
11
11
|
gem.email = "mike@dvorkin.net"
|
12
12
|
gem.homepage = "http://github.com/michaeldv/awesome_print"
|
13
13
|
gem.authors = ["Michael Dvorkin"]
|
14
|
-
|
15
|
-
gem.add_development_dependency "rspec", ">= 2.0.0"
|
16
|
-
else
|
17
|
-
gem.add_development_dependency "rspec", ">= 1.3.0"
|
18
|
-
end
|
14
|
+
gem.add_development_dependency "rspec", ">= 2.5.0"
|
19
15
|
gem.files = FileList["[A-Z]*", "lib/**/*.rb", "rails/*.rb", "spec/*", "init.rb"]
|
20
|
-
gem.has_rdoc = false
|
21
16
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
22
17
|
end
|
23
18
|
Jeweler::GemcutterTasks.new
|
@@ -25,30 +20,15 @@ rescue LoadError
|
|
25
20
|
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
26
21
|
end
|
27
22
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
36
|
-
spec.rcov = true
|
37
|
-
spec.rcov_opts = %q[--exclude "spec"]
|
38
|
-
end
|
39
|
-
else
|
40
|
-
require 'spec/rake/spectask'
|
41
|
-
Spec::Rake::SpecTask.new(:spec) do |spec|
|
42
|
-
spec.libs << 'lib' << 'spec'
|
43
|
-
spec.spec_opts = ['--color']
|
44
|
-
spec.spec_files = FileList['spec/**/*_spec.rb']
|
45
|
-
end
|
23
|
+
require "rspec/core/rake_task"
|
24
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
25
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
26
|
+
spec.rspec_opts = ['--color']
|
27
|
+
end
|
46
28
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
spec.rcov = true
|
51
|
-
end
|
29
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
30
|
+
spec.rcov = true
|
31
|
+
spec.rcov_opts = %q[--exclude "spec"]
|
52
32
|
end
|
53
33
|
|
54
34
|
task :spec => :check_dependencies
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/init.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "lib", "
|
1
|
+
require File.join(File.dirname(__FILE__), "lib", "awesome_print")
|
data/lib/ap.rb
CHANGED
@@ -3,15 +3,22 @@
|
|
3
3
|
# Awesome Print is freely distributable under the terms of MIT license.
|
4
4
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
5
|
#------------------------------------------------------------------------------
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
#
|
7
|
+
# AwesomePrint might be loaded implicitly through ~/.irbrc so do nothing
|
8
|
+
# for subsequent requires.
|
9
|
+
#
|
10
|
+
unless defined?(AwesomePrint)
|
11
|
+
%w(array string method object class kernel).each do |file|
|
12
|
+
require File.dirname(__FILE__) + "/ap/core_ext/#{file}"
|
13
|
+
end
|
9
14
|
|
10
|
-
require File.dirname(__FILE__) + "/ap/awesome_print"
|
11
|
-
require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(Logger)
|
12
|
-
require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(ActionView)
|
15
|
+
require File.dirname(__FILE__) + "/ap/awesome_print"
|
16
|
+
require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(Logger)
|
17
|
+
require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(ActionView)
|
13
18
|
|
14
|
-
# Load the following under normal circumstatnces as well as in Rails
|
15
|
-
# console when required from ~/.irbrc.
|
16
|
-
require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV'])
|
17
|
-
require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV'])
|
19
|
+
# Load the following under normal circumstatnces as well as in Rails
|
20
|
+
# console when required from ~/.irbrc.
|
21
|
+
require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV'])
|
22
|
+
require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV'])
|
23
|
+
require File.dirname(__FILE__) + "/ap/mixin/mongo_mapper" if defined?(MongoMapper)
|
24
|
+
end
|
data/lib/ap/awesome_print.rb
CHANGED
@@ -3,19 +3,22 @@
|
|
3
3
|
# Awesome Print is freely distributable under the terms of MIT license.
|
4
4
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
5
|
#------------------------------------------------------------------------------
|
6
|
+
require "cgi"
|
6
7
|
require "shellwords"
|
7
8
|
|
8
9
|
class AwesomePrint
|
9
10
|
AP = :__awesome_print__ unless defined?(AwesomePrint::AP)
|
10
11
|
CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational, :struct, :method, :unboundmethod ] unless defined?(AwesomePrint::CORE)
|
12
|
+
@@force_colors = false
|
11
13
|
|
12
14
|
def initialize(options = {})
|
13
15
|
@options = {
|
14
|
-
:multiline => true,
|
15
|
-
:plain => false,
|
16
|
-
:indent => 4,
|
17
|
-
:index => true,
|
18
|
-
:
|
16
|
+
:multiline => true, # Display in multiple lines.
|
17
|
+
:plain => false, # Use colors.
|
18
|
+
:indent => 4, # Indent using 4 spaces.
|
19
|
+
:index => true, # Display array indices.
|
20
|
+
:html => false, # Use ANSI color codes rather than HTML.
|
21
|
+
:sorted_hash_keys => false, # Do not sort hash keys.
|
19
22
|
:color => {
|
20
23
|
:array => :white,
|
21
24
|
:bigdecimal => :blue,
|
@@ -43,7 +46,8 @@ class AwesomePrint
|
|
43
46
|
@indentation = @options[:indent].abs
|
44
47
|
Thread.current[AP] ||= []
|
45
48
|
end
|
46
|
-
|
49
|
+
|
50
|
+
|
47
51
|
private
|
48
52
|
|
49
53
|
# Format an array.
|
@@ -103,7 +107,7 @@ class AwesomePrint
|
|
103
107
|
end
|
104
108
|
end
|
105
109
|
|
106
|
-
# Format a Struct. If @options[:indent]
|
110
|
+
# Format a Struct. If @options[:indent] is negative left align hash keys.
|
107
111
|
#------------------------------------------------------------------------------
|
108
112
|
def awesome_struct(s)
|
109
113
|
h = {}
|
@@ -176,13 +180,15 @@ class AwesomePrint
|
|
176
180
|
def methods_array(a)
|
177
181
|
object = a.instance_variable_get('@__awesome_methods__')
|
178
182
|
tuples = a.map do |name|
|
179
|
-
if object.respond_to?(name, true)
|
180
|
-
|
181
|
-
|
183
|
+
tuple = if object.respond_to?(name, true) # Is this a regular method?
|
184
|
+
the_method = object.method(name) rescue nil # Avoid potential ArgumentError if object#method is overridden.
|
185
|
+
if the_method && the_method.respond_to?(:arity) # Is this original object#method?
|
186
|
+
method_tuple(the_method) # Yes, we are good.
|
187
|
+
end
|
188
|
+
elsif object.respond_to?(:instance_method) # Is this an unbound method?
|
182
189
|
method_tuple(object.instance_method(name))
|
183
|
-
else # WTF method.
|
184
|
-
[ name.to_s, '(?)', '' ]
|
185
190
|
end
|
191
|
+
tuple || [ name.to_s, '(?)', '' ] # Return WTF default if all the above fails.
|
186
192
|
end
|
187
193
|
|
188
194
|
width = (tuples.size - 1).to_s.size
|
@@ -241,10 +247,11 @@ class AwesomePrint
|
|
241
247
|
# Pick the color and apply it to the given string as necessary.
|
242
248
|
#------------------------------------------------------------------------------
|
243
249
|
def colorize(s, type)
|
244
|
-
|
245
|
-
|
250
|
+
s = CGI.escapeHTML(s) if @options[:html]
|
251
|
+
if @options[:plain] || !@options[:color][type] || !colorize?
|
252
|
+
@options[:html] ? "<pre>#{s}</pre>" : s
|
246
253
|
else
|
247
|
-
s.send(@options[:color][type])
|
254
|
+
s.send(@options[:color][type], @options[:html])
|
248
255
|
end
|
249
256
|
end
|
250
257
|
|
@@ -319,6 +326,19 @@ class AwesomePrint
|
|
319
326
|
$stderr.puts "Could not load #{dotfile}: #{e}"
|
320
327
|
end
|
321
328
|
|
329
|
+
# Return true if we are to colorize the output.
|
330
|
+
#------------------------------------------------------------------------------
|
331
|
+
def colorize?
|
332
|
+
@@force_colors || (STDOUT.tty? && ((ENV['TERM'] && ENV['TERM'] != 'dumb') || ENV['ANSICON']))
|
333
|
+
end
|
334
|
+
|
335
|
+
# Class accessor to force colorized output (ex. forked subprocess where TERM
|
336
|
+
# might be dumb).
|
337
|
+
#------------------------------------------------------------------------------
|
338
|
+
def self.force_colors!(value = true)
|
339
|
+
@@force_colors = value
|
340
|
+
end
|
341
|
+
|
322
342
|
# Class accessors for custom defaults.
|
323
343
|
#------------------------------------------------------------------------------
|
324
344
|
def self.defaults
|
data/lib/ap/core_ext/array.rb
CHANGED
@@ -14,12 +14,13 @@
|
|
14
14
|
#
|
15
15
|
class Array #:nodoc:
|
16
16
|
[ :-, :& ].each do |operator|
|
17
|
-
|
17
|
+
original_operator = instance_method(operator)
|
18
|
+
|
18
19
|
define_method operator do |*args|
|
19
|
-
arr = self.
|
20
|
+
arr = original_operator.bind(self).call(*args)
|
20
21
|
if self.instance_variable_defined?('@__awesome_methods__')
|
21
22
|
arr.instance_variable_set('@__awesome_methods__', self.instance_variable_get('@__awesome_methods__'))
|
22
|
-
arr.sort!
|
23
|
+
arr.sort! { |a, b| a.to_s <=> b.to_s } # Need the block since Ruby 1.8.x can't sort arrays of symbols.
|
23
24
|
end
|
24
25
|
arr
|
25
26
|
end
|
@@ -56,7 +57,10 @@ class Array #:nodoc:
|
|
56
57
|
arr = unless blk
|
57
58
|
original_grep(pattern)
|
58
59
|
else
|
59
|
-
original_grep(pattern)
|
60
|
+
original_grep(pattern) do |match|
|
61
|
+
eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding)
|
62
|
+
yield match
|
63
|
+
end
|
60
64
|
end
|
61
65
|
if self.instance_variable_defined?('@__awesome_methods__')
|
62
66
|
arr.instance_variable_set('@__awesome_methods__', self.instance_variable_get('@__awesome_methods__'))
|
data/lib/ap/core_ext/class.rb
CHANGED
@@ -6,10 +6,10 @@
|
|
6
6
|
class Class #:nodoc:
|
7
7
|
# Remaining public/private etc. '_methods' are handled in core_ext/object.rb.
|
8
8
|
%w(instance_methods private_instance_methods protected_instance_methods public_instance_methods).each do |name|
|
9
|
-
|
9
|
+
original_method = instance_method(name)
|
10
10
|
|
11
11
|
define_method name do |*args|
|
12
|
-
methods = self.
|
12
|
+
methods = original_method.bind(self).call(*args)
|
13
13
|
methods.instance_variable_set('@__awesome_methods__', self) # Evil?!
|
14
14
|
methods.sort!
|
15
15
|
end
|
data/lib/ap/core_ext/kernel.rb
CHANGED
data/lib/ap/core_ext/object.rb
CHANGED
@@ -6,10 +6,10 @@
|
|
6
6
|
class Object #:nodoc:
|
7
7
|
# Remaining instance '_methods' are handled in core_ext/class.rb.
|
8
8
|
%w(methods private_methods protected_methods public_methods singleton_methods).each do |name|
|
9
|
-
|
9
|
+
original_method = instance_method(name)
|
10
10
|
|
11
11
|
define_method name do |*args|
|
12
|
-
methods = self.
|
12
|
+
methods = original_method.bind(self).call(*args)
|
13
13
|
methods.instance_variable_set('@__awesome_methods__', self) # Evil?!
|
14
14
|
methods.sort!
|
15
15
|
end
|
data/lib/ap/core_ext/string.rb
CHANGED
@@ -11,13 +11,14 @@ class String
|
|
11
11
|
# 1 => bright
|
12
12
|
# 0 => normal
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
%w(gray red green yellow blue purple cyan white).zip(
|
15
|
+
%w(black darkred darkgreen brown navy darkmagenta darkcyan slategray)).each_with_index do |(color, shade), i|
|
16
|
+
define_method color do |*html|
|
17
|
+
html[0] ? %Q|<pre style="color:#{color}">#{self}</pre>| : "\033[1;#{30+i}m#{self}\033[0m"
|
18
|
+
end
|
19
|
+
|
20
|
+
define_method "#{color}ish" do |*html|
|
21
|
+
html[0] ? %Q|<pre style="color:#{shade}">#{self}</pre>| : "\033[0;#{30+i}m#{self}\033[0m"
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
data/lib/ap/mixin/action_view.rb
CHANGED
@@ -5,30 +5,9 @@
|
|
5
5
|
#------------------------------------------------------------------------------
|
6
6
|
module AwesomePrintActionView
|
7
7
|
|
8
|
-
|
9
|
-
unless base.const_defined?(:AP_ANSI_TO_HTML)
|
10
|
-
hash = {} # Build ANSI => HTML color map.
|
11
|
-
[ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i|
|
12
|
-
hash["\033[1;#{30+i}m"] = color
|
13
|
-
end
|
14
|
-
[ :black, :darkred, :darkgreen, :brown, :navy, :darkmagenta, :darkcyan, :slategray ].each_with_index do |color, i|
|
15
|
-
hash["\033[0;#{30+i}m"] = color
|
16
|
-
end
|
17
|
-
base.const_set(:AP_ANSI_TO_HTML, hash.freeze)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
8
|
+
# Use HTML colors and add default "debug_dump" class to the resulting HTML.
|
21
9
|
def ap_debug(object, options = {})
|
22
|
-
|
23
|
-
|
24
|
-
unless options[:plain]
|
25
|
-
self.class::AP_ANSI_TO_HTML.each do |key, value|
|
26
|
-
formatted.gsub!(key, %Q|<font color="#{value}">|)
|
27
|
-
end
|
28
|
-
formatted.gsub!("\033[0m", "</font>")
|
29
|
-
end
|
30
|
-
|
31
|
-
content_tag(:pre, formatted, :class => "debug_dump")
|
10
|
+
object.ai(options.merge(:html => true)).sub(/^<pre([\s>])/, '<pre class="debug_dump"\\1')
|
32
11
|
end
|
33
12
|
|
34
13
|
alias_method :ap, :ap_debug
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
2
|
+
#
|
3
|
+
# Awesome Print is freely distributable under the terms of MIT license.
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
|
+
#------------------------------------------------------------------------------
|
6
|
+
module AwesomePrintMongoMapper
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.send :alias_method, :printable_without_mongo_mapper, :printable
|
10
|
+
base.send :alias_method, :printable, :printable_with_mongo_mapper
|
11
|
+
end
|
12
|
+
|
13
|
+
# Add MongoMapper class names to the dispatcher pipeline.
|
14
|
+
#------------------------------------------------------------------------------
|
15
|
+
def printable_with_mongo_mapper(object)
|
16
|
+
printable = printable_without_mongo_mapper(object)
|
17
|
+
return printable if !defined?(MongoMapper::Document)
|
18
|
+
|
19
|
+
if printable == :self
|
20
|
+
if object.is_a?(MongoMapper::Document) || object.is_a?(MongoMapper::EmbeddedDocument)
|
21
|
+
printable = :mongo_mapper_instance
|
22
|
+
end
|
23
|
+
elsif printable == :class && (object.ancestors & [MongoMapper::Document, MongoMapper::EmbeddedDocument]).size > 0
|
24
|
+
printable = :mongo_mapper_class
|
25
|
+
end
|
26
|
+
printable
|
27
|
+
end
|
28
|
+
|
29
|
+
# Format MongoMapper instance object.
|
30
|
+
#------------------------------------------------------------------------------
|
31
|
+
def awesome_mongo_mapper_instance(object)
|
32
|
+
return object.inspect if !defined?(ActiveSupport::OrderedHash)
|
33
|
+
|
34
|
+
data = object.keys.keys.sort_by{|k| k}.inject(ActiveSupport::OrderedHash.new) do |hash, name|
|
35
|
+
hash[name] = object[name]
|
36
|
+
hash
|
37
|
+
end
|
38
|
+
"#{object} " + awesome_hash(data)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Format MongoMapper class object.
|
42
|
+
#------------------------------------------------------------------------------
|
43
|
+
def awesome_mongo_mapper_class(object)
|
44
|
+
return object.inspect if !defined?(ActiveSupport::OrderedHash) || !object.respond_to?(:keys)
|
45
|
+
|
46
|
+
data = object.keys.sort_by{|k| k}.inject(ActiveSupport::OrderedHash.new) do |hash, c|
|
47
|
+
hash[c.first] = (c.last.type || "undefined").to_s.underscore.intern
|
48
|
+
hash
|
49
|
+
end
|
50
|
+
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
AwesomePrint.send(:include, AwesomePrintMongoMapper)
|
data/lib/awesome_print.rb
CHANGED
@@ -7,18 +7,20 @@
|
|
7
7
|
# This is the copy of original 'ap.rb' file that matches the gem name. It makes
|
8
8
|
# it possible to omit the :require part in bundler's Gemfile:
|
9
9
|
#
|
10
|
-
# gem 'awesome_print', '>= 0.
|
11
|
-
# gem 'awesome_print', '>= 3.0.0'
|
10
|
+
# gem 'awesome_print', '>= 0.4.0'
|
12
11
|
#
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
unless defined?(AwesomePrint)
|
13
|
+
%w(array string method object class kernel).each do |file|
|
14
|
+
require File.dirname(__FILE__) + "/ap/core_ext/#{file}"
|
15
|
+
end
|
16
16
|
|
17
|
-
require File.dirname(__FILE__) + "/ap/awesome_print"
|
18
|
-
require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(Logger)
|
19
|
-
require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(ActionView)
|
17
|
+
require File.dirname(__FILE__) + "/ap/awesome_print"
|
18
|
+
require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(Logger)
|
19
|
+
require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(ActionView)
|
20
20
|
|
21
|
-
# Load the following under normal circumstatnces as well as in Rails
|
22
|
-
# console when required from ~/.irbrc.
|
23
|
-
require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV'])
|
24
|
-
require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV'])
|
21
|
+
# Load the following under normal circumstatnces as well as in Rails
|
22
|
+
# console when required from ~/.irbrc.
|
23
|
+
require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV'])
|
24
|
+
require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV'])
|
25
|
+
require File.dirname(__FILE__) + "/ap/mixin/mongo_mapper" if defined?(MongoMapper)
|
26
|
+
end
|
data/rails/init.rb
CHANGED
@@ -6,8 +6,4 @@
|
|
6
6
|
#
|
7
7
|
# Load awesome_print when installed as Rails 2.3.x plugin.
|
8
8
|
#
|
9
|
-
# NOTE: After Rails 2.3.x console loads awesome_print/lib/ap.rb it attempts
|
10
|
-
# to load this file as well. Make sure to check whether the awesome_print
|
11
|
-
# is already loaded to avoid Ruby stack overflow when extending core classes.
|
12
|
-
#
|
13
9
|
require File.join(File.dirname(__FILE__), "..", "init") unless defined?(AwesomePrint)
|
data/spec/action_view_spec.rb
CHANGED
@@ -1,35 +1,25 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
|
4
|
-
require '
|
3
|
+
begin
|
4
|
+
require 'action_view'
|
5
|
+
require 'ap/mixin/action_view'
|
5
6
|
|
6
|
-
describe "AwesomePrint ActionView
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
describe "AwesomePrint ActionView extension" do
|
8
|
+
before(:each) do
|
9
|
+
@view = ActionView::Base.new
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should encode HTML entities" do
|
17
|
-
obj = " &<hello>"
|
18
|
-
@view.ap(obj, :plain => true).should == '<pre class="debug_dump">" &<hello>"</pre>'
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should convert primary ANSI colors to HTML" do
|
22
|
-
obj = 42
|
23
|
-
[ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each do |color|
|
24
|
-
@view.ap(obj, :color => { :fixnum => color }).should == %Q|<pre class="debug_dump"><font color="#{color}">42</font></pre>|
|
12
|
+
it "uses HTML and adds 'debug_dump' class to plain <pre> tag" do
|
13
|
+
markup = rand
|
14
|
+
@view.ap(markup, :plain => true).should == %Q|<pre class="debug_dump">#{markup}</pre>|
|
25
15
|
end
|
26
|
-
end
|
27
16
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
[ :black, :darkred, :darkgreen, :brown, :navy, :darkmagenta, :darkcyan, :slategray, :black, :slategray ]) do |ansi, html|
|
32
|
-
@view.ap(obj, :color => { :fixnum => ansi }).should == %Q|<pre class="debug_dump"><font color="#{html}">42</font></pre>|
|
17
|
+
it "uses HTML and adds 'debug_dump' class to colorized <pre> tag" do
|
18
|
+
markup = ' &<hello>'
|
19
|
+
@view.ap(markup).should == '<pre class="debug_dump" style="color:brown">" &<hello>"</pre>'
|
33
20
|
end
|
34
21
|
end
|
22
|
+
|
23
|
+
rescue LoadError
|
24
|
+
puts "Skipping ActionView specs..."
|
35
25
|
end
|
data/spec/active_record_spec.rb
CHANGED
@@ -1,56 +1,56 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
|
4
|
-
require '
|
3
|
+
begin
|
4
|
+
require 'active_record'
|
5
|
+
require 'ap/mixin/active_record'
|
5
6
|
|
7
|
+
if defined?(::ActiveRecord)
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
@columns ||= []
|
14
|
-
end
|
9
|
+
# Create tableless ActiveRecord model.
|
10
|
+
#------------------------------------------------------------------------------
|
11
|
+
class User < ActiveRecord::Base
|
12
|
+
def self.columns()
|
13
|
+
@columns ||= []
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
def self.column(name, sql_type = nil, default = nil, null = true)
|
17
|
+
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
column :id, :integer
|
21
|
+
column :name, :string
|
22
|
+
column :rank, :integer
|
23
|
+
column :admin, :boolean
|
24
|
+
column :created_at, :datetime
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
def self.table_exists?
|
27
|
+
true
|
28
|
+
end
|
28
29
|
end
|
29
|
-
end
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
class SubUser < User
|
32
|
+
def self.columns
|
33
|
+
User.columns
|
34
|
+
end
|
34
35
|
end
|
35
|
-
end
|
36
36
|
|
37
|
-
|
38
|
-
before(:each) do
|
39
|
-
stub_dotfile!
|
40
|
-
end
|
41
|
-
|
42
|
-
#------------------------------------------------------------------------------
|
43
|
-
describe "ActiveRecord instance" do
|
37
|
+
describe "AwesomePrint/ActiveRecord" do
|
44
38
|
before(:each) do
|
45
|
-
|
46
|
-
@diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00")
|
47
|
-
@laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00")
|
48
|
-
@ap = AwesomePrint.new(:plain => true)
|
39
|
+
stub_dotfile!
|
49
40
|
end
|
50
41
|
|
51
|
-
|
52
|
-
|
53
|
-
|
42
|
+
#------------------------------------------------------------------------------
|
43
|
+
describe "ActiveRecord instance" do
|
44
|
+
before(:each) do
|
45
|
+
ActiveRecord::Base.default_timezone = :utc
|
46
|
+
@diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00")
|
47
|
+
@laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00")
|
48
|
+
@ap = AwesomePrint.new(:plain => true)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "display single record" do
|
52
|
+
out = @ap.send(:awesome, @diana)
|
53
|
+
str = <<-EOS.strip
|
54
54
|
#<User:0x01234567> {
|
55
55
|
:id => nil,
|
56
56
|
:name => "Diana",
|
@@ -59,18 +59,18 @@ if defined?(::ActiveRecord)
|
|
59
59
|
:created_at => ?
|
60
60
|
}
|
61
61
|
EOS
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
62
|
+
if RUBY_VERSION.to_f < 1.9
|
63
|
+
str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992')
|
64
|
+
else
|
65
|
+
str.sub!('?', '1992-10-10 12:30:00 UTC')
|
66
|
+
end
|
67
67
|
|
68
|
-
|
69
|
-
|
68
|
+
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
69
|
+
end
|
70
70
|
|
71
|
-
|
72
|
-
|
73
|
-
|
71
|
+
it "display multiple records" do
|
72
|
+
out = @ap.send(:awesome, [ @diana, @laura ])
|
73
|
+
str = <<-EOS.strip
|
74
74
|
[
|
75
75
|
[0] #<User:0x01234567> {
|
76
76
|
:id => nil,
|
@@ -88,23 +88,23 @@ EOS
|
|
88
88
|
}
|
89
89
|
]
|
90
90
|
EOS
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
91
|
+
if RUBY_VERSION.to_f < 1.9
|
92
|
+
str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992')
|
93
|
+
str.sub!('!', 'Mon May 26 14:15:00 UTC 2003')
|
94
|
+
else
|
95
|
+
str.sub!('?', '1992-10-10 12:30:00 UTC')
|
96
|
+
str.sub!('!', '2003-05-26 14:15:00 UTC')
|
97
|
+
end
|
98
98
|
|
99
|
-
|
99
|
+
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
100
|
+
end
|
100
101
|
end
|
101
|
-
end
|
102
102
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
103
|
+
#------------------------------------------------------------------------------
|
104
|
+
describe "ActiveRecord class" do
|
105
|
+
it "should print the class" do
|
106
|
+
@ap = AwesomePrint.new(:plain => true)
|
107
|
+
@ap.send(:awesome, User).should == <<-EOS.strip
|
108
108
|
class User < ActiveRecord::Base {
|
109
109
|
:id => :integer,
|
110
110
|
:name => :string,
|
@@ -112,13 +112,12 @@ class User < ActiveRecord::Base {
|
|
112
112
|
:admin => :boolean,
|
113
113
|
:created_at => :datetime
|
114
114
|
}
|
115
|
-
|
116
|
-
|
117
|
-
end
|
115
|
+
EOS
|
116
|
+
end
|
118
117
|
|
119
|
-
it "should print the class for non-direct subclasses of AR::Base" do
|
120
|
-
|
121
|
-
|
118
|
+
it "should print the class for non-direct subclasses of AR::Base" do
|
119
|
+
@ap = AwesomePrint.new(:plain => true)
|
120
|
+
@ap.send(:awesome, SubUser).should == <<-EOS.strip
|
122
121
|
class SubUser < User {
|
123
122
|
:id => :integer,
|
124
123
|
:name => :string,
|
@@ -126,9 +125,12 @@ class SubUser < User {
|
|
126
125
|
:admin => :boolean,
|
127
126
|
:created_at => :datetime
|
128
127
|
}
|
129
|
-
|
130
|
-
|
128
|
+
EOS
|
129
|
+
end
|
131
130
|
end
|
132
131
|
end
|
133
132
|
end
|
133
|
+
|
134
|
+
rescue LoadError
|
135
|
+
puts "Skipping ActiveRecord specs..."
|
134
136
|
end
|
data/spec/awesome_print_spec.rb
CHANGED
@@ -454,6 +454,47 @@ EOS
|
|
454
454
|
end
|
455
455
|
weird.new.ai(:plain => true).should == ''
|
456
456
|
end
|
457
|
+
|
458
|
+
# See https://github.com/michaeldv/awesome_print/issues/35
|
459
|
+
it "handle array grep when pattern contains / chapacter" do
|
460
|
+
hash = { "1/x" => 1, "2//x" => :"2" }
|
461
|
+
grepped = hash.keys.grep(/^(\d+)\//) { $1 }
|
462
|
+
grepped.ai(:plain => true, :multiline => false).should == '[ "1", "2" ]'
|
463
|
+
end
|
464
|
+
|
465
|
+
it "returns value passed as a parameter" do
|
466
|
+
object = rand
|
467
|
+
self.stub!(:puts)
|
468
|
+
(ap object).should == object
|
469
|
+
end
|
470
|
+
|
471
|
+
# Require different file name this time (lib/ap.rb vs. lib/awesome_print).
|
472
|
+
it "several require 'awesome_print' should do no harm" do
|
473
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/ap')
|
474
|
+
lambda { rand.ai }.should_not raise_error
|
475
|
+
end
|
476
|
+
end
|
477
|
+
|
478
|
+
describe "HTML output" do
|
479
|
+
it "wraps ap output with plain <pre> tag" do
|
480
|
+
markup = rand
|
481
|
+
markup.ai(:html => true, :plain => true).should == "<pre>#{markup}</pre>"
|
482
|
+
end
|
483
|
+
|
484
|
+
it "wraps ap output with colorized <pre> tag" do
|
485
|
+
markup = rand
|
486
|
+
markup.ai(:html => true).should == %Q|<pre style="color:blue">#{markup}</pre>|
|
487
|
+
end
|
488
|
+
|
489
|
+
it "encodes HTML entities (plain)" do
|
490
|
+
markup = ' &<hello>'
|
491
|
+
markup.ai(:html => true, :plain => true).should == '<pre>" &<hello>"</pre>'
|
492
|
+
end
|
493
|
+
|
494
|
+
it "encodes HTML entities (color)" do
|
495
|
+
markup = ' &<hello>'
|
496
|
+
markup.ai(:html => true).should == '<pre style="color:brown">" &<hello>"</pre>'
|
497
|
+
end
|
457
498
|
end
|
458
499
|
|
459
500
|
#------------------------------------------------------------------------------
|
@@ -504,14 +545,15 @@ EOS
|
|
504
545
|
it "inherited from File should be displayed as File" do
|
505
546
|
class My < File; end
|
506
547
|
|
507
|
-
my = File.new('/dev/null')
|
548
|
+
my = File.new('/dev/null') rescue File.new('nul')
|
508
549
|
my.ai(:plain => true).should == "#{my.inspect}\n" << `ls -alF #{my.path}`.chop
|
509
550
|
end
|
510
551
|
|
511
552
|
it "inherited from Dir should be displayed as Dir" do
|
512
553
|
class My < Dir; end
|
513
554
|
|
514
|
-
|
555
|
+
require 'tmpdir'
|
556
|
+
my = My.new(Dir.tmpdir)
|
515
557
|
my.ai(:plain => true).should == "#{my.inspect}\n" << `ls -alF #{my.path}`.chop
|
516
558
|
end
|
517
559
|
|
@@ -523,5 +565,16 @@ EOS
|
|
523
565
|
my = My.new
|
524
566
|
my.methods.ai(:plain => true).should_not raise_error(ArgumentError)
|
525
567
|
end
|
568
|
+
|
569
|
+
it "should handle a class defines its own #method method (ex. request.method)" do
|
570
|
+
class My
|
571
|
+
def method
|
572
|
+
'POST'
|
573
|
+
end
|
574
|
+
end
|
575
|
+
|
576
|
+
my = My.new
|
577
|
+
my.methods.ai(:plain => true).should_not raise_error(ArgumentError)
|
578
|
+
end
|
526
579
|
end
|
527
580
|
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "AwesomePrint" do
|
4
|
+
before(:each) do
|
5
|
+
stub_dotfile!
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "colorization" do
|
9
|
+
PLAIN = '[ 1, :two, "three", [ nil, [ true, false ] ] ]'
|
10
|
+
COLORIZED = "[ \e[1;34m1\e[0m, \e[0;36m:two\e[0m, \e[0;33m\"three\"\e[0m, [ \e[1;31mnil\e[0m, [ \e[1;32mtrue\e[0m, \e[1;31mfalse\e[0m ] ] ]"
|
11
|
+
|
12
|
+
before(:each) do
|
13
|
+
AwesomePrint.force_colors!(false)
|
14
|
+
ENV['TERM'] = "xterm-colors"
|
15
|
+
ENV.delete('ANSICON')
|
16
|
+
@arr = [ 1, :two, "three", [ nil, [ true, false] ] ]
|
17
|
+
end
|
18
|
+
|
19
|
+
it "colorizes tty processes by default" do
|
20
|
+
stub_tty!(STDOUT, true)
|
21
|
+
|
22
|
+
@arr.ai(:multiline => false).should == COLORIZED
|
23
|
+
end
|
24
|
+
|
25
|
+
it "colorizes tty processes by default" do
|
26
|
+
stub_tty!(STDOUT, true)
|
27
|
+
|
28
|
+
@arr.ai(:multiline => false).should == COLORIZED
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
it "colorizes processes with ENV['ANSICON'] by default" do
|
33
|
+
stub_tty!(STDOUT, true)
|
34
|
+
ENV['ANSICON'] = "1"
|
35
|
+
|
36
|
+
@arr.ai(:multiline => false).should == COLORIZED
|
37
|
+
end
|
38
|
+
|
39
|
+
it "does not colorize tty processes running in dumb terminals by default" do
|
40
|
+
stub_tty!(STDOUT, true)
|
41
|
+
ENV['TERM'] = "dumb"
|
42
|
+
|
43
|
+
@arr.ai(:multiline => false).should == PLAIN
|
44
|
+
end
|
45
|
+
|
46
|
+
it "does not colorize subprocesses by default" do
|
47
|
+
stub_tty!(STDOUT, false)
|
48
|
+
|
49
|
+
@arr.ai(:multiline => false).should == PLAIN
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "forced" do
|
53
|
+
before(:each) do
|
54
|
+
AwesomePrint.force_colors!
|
55
|
+
end
|
56
|
+
|
57
|
+
it "still colorizes tty processes" do
|
58
|
+
stub_tty!(STDOUT, true)
|
59
|
+
|
60
|
+
@arr.ai(:multiline => false).should == COLORIZED
|
61
|
+
end
|
62
|
+
|
63
|
+
it "colorizes dumb terminals" do
|
64
|
+
stub_tty!(STDOUT, true)
|
65
|
+
ENV["TERM"] = "dumb"
|
66
|
+
|
67
|
+
@arr.ai(:multiline => false).should == COLORIZED
|
68
|
+
end
|
69
|
+
|
70
|
+
it "colorizes subprocess" do
|
71
|
+
stub_tty!(STDOUT, true)
|
72
|
+
@arr.ai(:multiline => false).should == COLORIZED
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def stub_tty!(stream, value)
|
78
|
+
eval(%{class << stream
|
79
|
+
def tty?
|
80
|
+
#{value}
|
81
|
+
end
|
82
|
+
end})
|
83
|
+
end
|
84
|
+
end
|
data/spec/logger_spec.rb
CHANGED
data/spec/methods_spec.rb
CHANGED
@@ -406,4 +406,23 @@ describe "Methods arrays" do
|
|
406
406
|
out = Hello.methods.grep(/^m(\d)$/) { %w(none one)[$1.to_i] }.ai(:plain => true)
|
407
407
|
out.should == "[\n [0] none() Hello\n [1] one() Hello\n]"
|
408
408
|
end
|
409
|
+
|
410
|
+
# See https://github.com/michaeldv/awesome_print/issues/30 for details.
|
411
|
+
it "grepping methods and converting them to_sym should work as expected" do
|
412
|
+
class Hello
|
413
|
+
private
|
414
|
+
def him; end
|
415
|
+
|
416
|
+
def his
|
417
|
+
private_methods.grep(/^h..$/) { |n| n.to_sym }
|
418
|
+
end
|
419
|
+
|
420
|
+
def her
|
421
|
+
private_methods.grep(/^.e.$/) { |n| n.to_sym }
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
hello = Hello.new
|
426
|
+
(hello.send(:his) - hello.send(:her)).sort_by { |x| x.to_s }.should == [ :him, :his ]
|
427
|
+
end
|
409
428
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
begin
|
4
|
+
require "mongo_mapper"
|
5
|
+
require "ap/mixin/mongo_mapper"
|
6
|
+
|
7
|
+
describe "AwesomePrint/MongoMapper" do
|
8
|
+
before :all do
|
9
|
+
class MongoUser
|
10
|
+
include MongoMapper::Document
|
11
|
+
|
12
|
+
key :first_name, String
|
13
|
+
key :last_name, String
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
before :each do
|
18
|
+
@ap = AwesomePrint.new(:plain => true)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should print for a class instance" do
|
22
|
+
user = MongoUser.new(:first_name => "Al", :last_name => "Capone")
|
23
|
+
out = @ap.send(:awesome, user)
|
24
|
+
str = <<-EOS.strip
|
25
|
+
#<MongoUser:0x01234567> {
|
26
|
+
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
|
27
|
+
"first_name" => "Al",
|
28
|
+
"last_name" => "Capone"
|
29
|
+
}
|
30
|
+
EOS
|
31
|
+
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
|
32
|
+
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
|
33
|
+
out.should == str
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should print for a class" do
|
37
|
+
@ap.send(:awesome, MongoUser).should == <<-EOS.strip
|
38
|
+
class MongoUser < Object {
|
39
|
+
"_id" => :object_id,
|
40
|
+
"first_name" => :string,
|
41
|
+
"last_name" => :string
|
42
|
+
}
|
43
|
+
EOS
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should print for a class when type is undefined" do
|
47
|
+
class Chamelion
|
48
|
+
include MongoMapper::Document
|
49
|
+
key :last_attribute
|
50
|
+
end
|
51
|
+
|
52
|
+
@ap.send(:awesome, Chamelion).should == <<-EOS.strip
|
53
|
+
class Chamelion < Object {
|
54
|
+
"_id" => :object_id,
|
55
|
+
"last_attribute" => :undefined
|
56
|
+
}
|
57
|
+
EOS
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
rescue LoadError
|
62
|
+
puts "Skipping MongoMapper specs..."
|
63
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,25 +4,48 @@
|
|
4
4
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
5
|
#------------------------------------------------------------------------------
|
6
6
|
#
|
7
|
-
# Running specs
|
8
|
-
# $ rake spec
|
9
|
-
# $
|
10
|
-
#
|
11
|
-
# Running specs with Ruby 1.9.2 and RSpec 2.0+:
|
12
|
-
# $ rake spec # Entire spec suite.
|
13
|
-
# $ rspec spec/logger_spec.rb # Individual spec file.
|
7
|
+
# Running specs from the command line:
|
8
|
+
# $ rake spec # Entire spec suite.
|
9
|
+
# $ rspec spec/logger_spec.rb # Individual spec file.
|
14
10
|
#
|
15
11
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
16
12
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
17
|
-
require '
|
18
|
-
|
19
|
-
if RUBY_VERSION.to_f < 1.9
|
20
|
-
require 'spec'
|
21
|
-
require 'spec/autorun'
|
22
|
-
require 'rubygems'
|
23
|
-
end
|
13
|
+
require 'awesome_print'
|
24
14
|
|
25
15
|
def stub_dotfile!
|
26
16
|
dotfile = File.join(ENV["HOME"], ".aprc")
|
27
17
|
File.should_receive(:readable?).at_least(:once).with(dotfile).and_return(false)
|
28
18
|
end
|
19
|
+
|
20
|
+
# The following is needed for the Infinity Test. It runs tests as subprocesses,
|
21
|
+
# which sets STDOUT.tty? to false and would otherwise prematurely disallow colors.
|
22
|
+
AwesomePrint.force_colors!
|
23
|
+
|
24
|
+
# Ruby 1.8.6 only: define missing String methods that are needed for the specs to pass.
|
25
|
+
if RUBY_VERSION < '1.8.7'
|
26
|
+
class String
|
27
|
+
def shellescape # Taken from Ruby 1.9.2 standard library, see lib/shellwords.rb.
|
28
|
+
return "''" if self.empty?
|
29
|
+
str = self.dup
|
30
|
+
str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1")
|
31
|
+
str.gsub!(/\n/, "'\n'")
|
32
|
+
str
|
33
|
+
end
|
34
|
+
|
35
|
+
def start_with?(*prefixes)
|
36
|
+
prefixes.each do |prefix|
|
37
|
+
prefix = prefix.to_s
|
38
|
+
return true if prefix == self[0, prefix.size]
|
39
|
+
end
|
40
|
+
false
|
41
|
+
end
|
42
|
+
|
43
|
+
def end_with?(*suffixes)
|
44
|
+
suffixes.each do |suffix|
|
45
|
+
suffix = suffix.to_s
|
46
|
+
return true if suffix == self[-suffix.size, suffix.size]
|
47
|
+
end
|
48
|
+
false
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awesome_print
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Michael Dvorkin
|
@@ -14,21 +15,23 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2011-
|
18
|
+
date: 2011-05-13 00:00:00 -07:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: rspec
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ">="
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 27
|
27
30
|
segments:
|
28
|
-
-
|
29
|
-
-
|
31
|
+
- 2
|
32
|
+
- 5
|
30
33
|
- 0
|
31
|
-
version:
|
34
|
+
version: 2.5.0
|
32
35
|
type: :development
|
33
36
|
version_requirements: *id001
|
34
37
|
description: "Great Ruby dubugging companion: pretty print Ruby objects to visualize their structure. Supports Rails ActiveRecord objects via included mixin."
|
@@ -59,13 +62,16 @@ files:
|
|
59
62
|
- lib/ap/mixin/action_view.rb
|
60
63
|
- lib/ap/mixin/active_record.rb
|
61
64
|
- lib/ap/mixin/active_support.rb
|
65
|
+
- lib/ap/mixin/mongo_mapper.rb
|
62
66
|
- lib/awesome_print.rb
|
63
67
|
- rails/init.rb
|
64
68
|
- spec/action_view_spec.rb
|
65
69
|
- spec/active_record_spec.rb
|
66
70
|
- spec/awesome_print_spec.rb
|
71
|
+
- spec/colorization_spec.rb
|
67
72
|
- spec/logger_spec.rb
|
68
73
|
- spec/methods_spec.rb
|
74
|
+
- spec/mongo_mapper_spec.rb
|
69
75
|
- spec/spec_helper.rb
|
70
76
|
- spec/string_spec.rb
|
71
77
|
has_rdoc: true
|
@@ -73,36 +79,34 @@ homepage: http://github.com/michaeldv/awesome_print
|
|
73
79
|
licenses: []
|
74
80
|
|
75
81
|
post_install_message:
|
76
|
-
rdoc_options:
|
77
|
-
|
82
|
+
rdoc_options: []
|
83
|
+
|
78
84
|
require_paths:
|
79
85
|
- lib
|
80
86
|
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
81
88
|
requirements:
|
82
89
|
- - ">="
|
83
90
|
- !ruby/object:Gem::Version
|
91
|
+
hash: 3
|
84
92
|
segments:
|
85
93
|
- 0
|
86
94
|
version: "0"
|
87
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
88
97
|
requirements:
|
89
98
|
- - ">="
|
90
99
|
- !ruby/object:Gem::Version
|
100
|
+
hash: 3
|
91
101
|
segments:
|
92
102
|
- 0
|
93
103
|
version: "0"
|
94
104
|
requirements: []
|
95
105
|
|
96
106
|
rubyforge_project: awesome_print
|
97
|
-
rubygems_version: 1.3.
|
107
|
+
rubygems_version: 1.3.7
|
98
108
|
signing_key:
|
99
109
|
specification_version: 3
|
100
110
|
summary: Pretty print Ruby objects with proper indentation and colors.
|
101
|
-
test_files:
|
102
|
-
|
103
|
-
- spec/active_record_spec.rb
|
104
|
-
- spec/awesome_print_spec.rb
|
105
|
-
- spec/logger_spec.rb
|
106
|
-
- spec/methods_spec.rb
|
107
|
-
- spec/spec_helper.rb
|
108
|
-
- spec/string_spec.rb
|
111
|
+
test_files: []
|
112
|
+
|