awesome_print 0.3.1 → 0.4.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.
- data/CHANGELOG +17 -0
- data/LICENSE +2 -1
- data/README.md +43 -22
- data/Rakefile +9 -29
- data/VERSION +1 -1
- data/init.rb +1 -1
- data/lib/ap/awesome_print.rb +38 -16
- data/lib/ap/core_ext/array.rb +9 -5
- data/lib/ap/core_ext/class.rb +6 -4
- data/lib/ap/core_ext/kernel.rb +2 -1
- data/lib/ap/core_ext/logger.rb +1 -1
- data/lib/ap/core_ext/method.rb +1 -1
- data/lib/ap/core_ext/object.rb +6 -6
- data/lib/ap/core_ext/string.rb +9 -8
- data/lib/ap/mixin/action_view.rb +4 -25
- data/lib/ap/mixin/active_record.rb +11 -10
- data/lib/ap/mixin/active_support.rb +3 -1
- data/lib/ap/mixin/mongo_mapper.rb +54 -0
- data/lib/ap.rb +19 -9
- data/lib/awesome_print.rb +16 -11
- data/rails/init.rb +1 -5
- data/spec/action_view_spec.rb +16 -26
- data/spec/active_record_spec.rb +73 -71
- data/spec/awesome_print_spec.rb +103 -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 +38 -15
- metadata +22 -18
data/CHANGELOG
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
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
|
+
|
|
11
|
+
0.3.2
|
|
12
|
+
- Make sure Rails mixins get loaded in Rails console when required from .irbrc
|
|
13
|
+
- Fixed an issue with classes that define their own #send method (ex: Socket)
|
|
14
|
+
- Fixed compatibility issue with Liquid gem that defines Module#liquid_methods
|
|
15
|
+
- Fixed hash spec for Ruby < 1.9 where order of hash keys is not guaranteed
|
|
16
|
+
- Added :sorted_hash_keys option to sort hash keys (Ed Ruder)
|
|
17
|
+
|
|
1
18
|
0.3.1 RubyConf X edition
|
|
2
19
|
- Fixed Ruby 1.8.6 compatibility issues (thanks, Tim!)
|
|
3
20
|
- Fixed stack overflow issue with Rails 2.3.x console
|
data/LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -15,14 +15,17 @@ 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,
|
|
24
|
-
:plain
|
|
25
|
-
:indent
|
|
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.
|
|
28
|
+
:sorted_hash_keys => false, # Do not sort hash keys.
|
|
26
29
|
:color => {
|
|
27
30
|
:array => :white,
|
|
28
31
|
:bignum => :blue,
|
|
@@ -46,7 +49,7 @@ Supported color names:
|
|
|
46
49
|
|
|
47
50
|
### Examples ###
|
|
48
51
|
$ cat > 1.rb
|
|
49
|
-
require "
|
|
52
|
+
require "awesome_print"
|
|
50
53
|
data = [ false, 42, %w(forty two), { :now => Time.now, :class => Time.now.class, :distance => 42e42 } ]
|
|
51
54
|
ap data
|
|
52
55
|
^D
|
|
@@ -66,7 +69,7 @@ Supported color names:
|
|
|
66
69
|
]
|
|
67
70
|
|
|
68
71
|
$ cat > 2.rb
|
|
69
|
-
require "
|
|
72
|
+
require "awesome_print"
|
|
70
73
|
data = { :now => Time.now, :class => Time.now.class, :distance => 42e42 }
|
|
71
74
|
ap data, :indent => -2 # <-- Left align hash keys.
|
|
72
75
|
^D
|
|
@@ -78,7 +81,7 @@ Supported color names:
|
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
$ cat > 3.rb
|
|
81
|
-
require "
|
|
84
|
+
require "awesome_print"
|
|
82
85
|
data = [ false, 42, %w(forty two) ]
|
|
83
86
|
data << data # <-- Nested array.
|
|
84
87
|
ap data, :multiline => false
|
|
@@ -87,7 +90,7 @@ Supported color names:
|
|
|
87
90
|
[ false, 42, [ "forty", "two" ], [...] ]
|
|
88
91
|
|
|
89
92
|
$ cat > 4.rb
|
|
90
|
-
require "
|
|
93
|
+
require "awesome_print"
|
|
91
94
|
class Hello
|
|
92
95
|
def self.world(x, y, z = nil, &blk)
|
|
93
96
|
end
|
|
@@ -100,7 +103,7 @@ Supported color names:
|
|
|
100
103
|
]
|
|
101
104
|
|
|
102
105
|
$ cat > 5.rb
|
|
103
|
-
require "
|
|
106
|
+
require "awesome_print"
|
|
104
107
|
ap (''.methods - Object.methods).grep(/!/)
|
|
105
108
|
^D
|
|
106
109
|
$ ruby 5.rb
|
|
@@ -127,10 +130,17 @@ Supported color names:
|
|
|
127
130
|
[19] upcase!() String
|
|
128
131
|
]
|
|
129
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
|
+
|
|
130
141
|
### Example (Rails console) ###
|
|
131
|
-
$
|
|
132
|
-
|
|
133
|
-
rails> require "ap"
|
|
142
|
+
$ rails console
|
|
143
|
+
rails> require "awesome_print"
|
|
134
144
|
rails> ap Account.all(:limit => 2)
|
|
135
145
|
[
|
|
136
146
|
[0] #<Account:0x1033220b8> {
|
|
@@ -190,7 +200,7 @@ To use awesome_print as default formatter in irb and Rails console add the follo
|
|
|
190
200
|
lines into your ~/.irbrc file:
|
|
191
201
|
|
|
192
202
|
require "rubygems"
|
|
193
|
-
require "
|
|
203
|
+
require "awesome_print"
|
|
194
204
|
|
|
195
205
|
unless IRB.version.include?('DietRB')
|
|
196
206
|
IRB::Irb.class_eval do
|
|
@@ -207,25 +217,30 @@ lines into your ~/.irbrc file:
|
|
|
207
217
|
end
|
|
208
218
|
|
|
209
219
|
### Logger Convenience Method ###
|
|
210
|
-
awesome_print adds
|
|
211
|
-
|
|
220
|
+
awesome_print adds the 'ap' method to the Logger and ActiveSupport::BufferedLogger classes
|
|
221
|
+
letting you call:
|
|
212
222
|
|
|
213
223
|
logger.ap object
|
|
214
224
|
|
|
215
|
-
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:
|
|
216
226
|
|
|
217
227
|
:log_level => :info
|
|
218
228
|
|
|
219
|
-
in the custom defaults (see below)
|
|
229
|
+
in the custom defaults (see below). You can also override on a per call basis with:
|
|
220
230
|
|
|
221
231
|
logger.ap object, :warn
|
|
222
232
|
|
|
223
233
|
### ActionView Convenience Method ###
|
|
224
|
-
awesome_print adds
|
|
234
|
+
awesome_print adds the 'ap' method to the ActionView::Base class making it available
|
|
225
235
|
within Rails templates. For example:
|
|
226
236
|
|
|
227
237
|
<%= ap @accounts.first %>
|
|
228
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
|
+
|
|
229
244
|
### Setting Custom Defaults ###
|
|
230
245
|
You can set your own default options by creating ``.aprc`` file in your home
|
|
231
246
|
directory. Within that file assign your defaults to ``AwesomePrint.defaults``.
|
|
@@ -242,9 +257,9 @@ For example:
|
|
|
242
257
|
|
|
243
258
|
### Running Specs ###
|
|
244
259
|
|
|
245
|
-
$
|
|
246
|
-
$
|
|
247
|
-
$ 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.
|
|
248
263
|
|
|
249
264
|
### Note on Patches/Pull Requests ###
|
|
250
265
|
* Fork the project on Github.
|
|
@@ -255,16 +270,22 @@ For example:
|
|
|
255
270
|
|
|
256
271
|
### Contributors ###
|
|
257
272
|
|
|
273
|
+
* Andrew O'Brien -- https://github.com/AndrewO
|
|
258
274
|
* Daniel Bretoi -- http://github.com/danielb2
|
|
259
275
|
* Eloy Duran -- http://github.com/alloy
|
|
276
|
+
* Elpizo Choi -- https://github.com/fuJiin
|
|
260
277
|
* Benoit Daloze -- http://github.com/eregon
|
|
261
278
|
* Sean Gallagher -- http://github.com/torandu
|
|
279
|
+
* Stephan Hagemann -- https://github.com/shageman
|
|
262
280
|
* Tim Harper -- http://github.com/timcharper
|
|
263
281
|
* Tobias Crawley -- http://github.com/tobias
|
|
282
|
+
* Viktar Basharymau -- https://github.com/DNNX
|
|
264
283
|
|
|
265
284
|
### License ###
|
|
266
|
-
Copyright (c) 2010 Michael Dvorkin
|
|
285
|
+
Copyright (c) 2010-2011 Michael Dvorkin
|
|
286
|
+
|
|
267
287
|
twitter.com/mid
|
|
288
|
+
|
|
268
289
|
%w(mike dvorkin.net) * "@" || %w(mike fatfreecrm.com) * "@"
|
|
269
290
|
|
|
270
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/awesome_print.rb
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
# Copyright (c) 2010 Michael Dvorkin
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
|
2
2
|
#
|
|
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,
|
|
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.
|
|
18
22
|
:color => {
|
|
19
23
|
:array => :white,
|
|
20
24
|
:bigdecimal => :blue,
|
|
@@ -42,7 +46,8 @@ class AwesomePrint
|
|
|
42
46
|
@indentation = @options[:indent].abs
|
|
43
47
|
Thread.current[AP] ||= []
|
|
44
48
|
end
|
|
45
|
-
|
|
49
|
+
|
|
50
|
+
|
|
46
51
|
private
|
|
47
52
|
|
|
48
53
|
# Format an array.
|
|
@@ -75,7 +80,8 @@ class AwesomePrint
|
|
|
75
80
|
def awesome_hash(h)
|
|
76
81
|
return "{}" if h == {}
|
|
77
82
|
|
|
78
|
-
|
|
83
|
+
keys = @options[:sorted_hash_keys] ? h.keys.sort { |a, b| a.to_s <=> b.to_s } : h.keys
|
|
84
|
+
data = keys.map do |key|
|
|
79
85
|
plain_single_line do
|
|
80
86
|
[ awesome(key), h[key] ]
|
|
81
87
|
end
|
|
@@ -101,7 +107,7 @@ class AwesomePrint
|
|
|
101
107
|
end
|
|
102
108
|
end
|
|
103
109
|
|
|
104
|
-
# Format a Struct. If @options[:indent]
|
|
110
|
+
# Format a Struct. If @options[:indent] is negative left align hash keys.
|
|
105
111
|
#------------------------------------------------------------------------------
|
|
106
112
|
def awesome_struct(s)
|
|
107
113
|
h = {}
|
|
@@ -174,13 +180,15 @@ class AwesomePrint
|
|
|
174
180
|
def methods_array(a)
|
|
175
181
|
object = a.instance_variable_get('@__awesome_methods__')
|
|
176
182
|
tuples = a.map do |name|
|
|
177
|
-
if object.respond_to?(name, true)
|
|
178
|
-
|
|
179
|
-
|
|
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?
|
|
180
189
|
method_tuple(object.instance_method(name))
|
|
181
|
-
else # WTF method.
|
|
182
|
-
[ name.to_s, '(?)', '' ]
|
|
183
190
|
end
|
|
191
|
+
tuple || [ name.to_s, '(?)', '' ] # Return WTF default if all the above fails.
|
|
184
192
|
end
|
|
185
193
|
|
|
186
194
|
width = (tuples.size - 1).to_s.size
|
|
@@ -239,10 +247,11 @@ class AwesomePrint
|
|
|
239
247
|
# Pick the color and apply it to the given string as necessary.
|
|
240
248
|
#------------------------------------------------------------------------------
|
|
241
249
|
def colorize(s, type)
|
|
242
|
-
|
|
243
|
-
|
|
250
|
+
s = CGI.escapeHTML(s) if @options[:html]
|
|
251
|
+
if @options[:plain] || !@options[:color][type] || !colorize?
|
|
252
|
+
@options[:html] ? "<pre>#{s}</pre>" : s
|
|
244
253
|
else
|
|
245
|
-
s.send(@options[:color][type])
|
|
254
|
+
s.send(@options[:color][type], @options[:html])
|
|
246
255
|
end
|
|
247
256
|
end
|
|
248
257
|
|
|
@@ -317,6 +326,19 @@ class AwesomePrint
|
|
|
317
326
|
$stderr.puts "Could not load #{dotfile}: #{e}"
|
|
318
327
|
end
|
|
319
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
|
+
|
|
320
342
|
# Class accessors for custom defaults.
|
|
321
343
|
#------------------------------------------------------------------------------
|
|
322
344
|
def self.defaults
|
data/lib/ap/core_ext/array.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010 Michael Dvorkin
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
|
2
2
|
#
|
|
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
|
|
@@ -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
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
# Copyright (c) 2010 Michael Dvorkin
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
|
2
2
|
#
|
|
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
6
|
class Class #:nodoc:
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
# Remaining public/private etc. '_methods' are handled in core_ext/object.rb.
|
|
8
|
+
%w(instance_methods private_instance_methods protected_instance_methods public_instance_methods).each do |name|
|
|
9
|
+
original_method = instance_method(name)
|
|
10
|
+
|
|
9
11
|
define_method name do |*args|
|
|
10
|
-
methods = self.
|
|
12
|
+
methods = original_method.bind(self).call(*args)
|
|
11
13
|
methods.instance_variable_set('@__awesome_methods__', self) # Evil?!
|
|
12
14
|
methods.sort!
|
|
13
15
|
end
|
data/lib/ap/core_ext/kernel.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010 Michael Dvorkin
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
|
2
2
|
#
|
|
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
|
|
@@ -13,6 +13,7 @@ module Kernel
|
|
|
13
13
|
|
|
14
14
|
def ap(object, options = {})
|
|
15
15
|
puts object.ai(options)
|
|
16
|
+
object
|
|
16
17
|
end
|
|
17
18
|
alias :awesome_print :ap
|
|
18
19
|
|
data/lib/ap/core_ext/logger.rb
CHANGED
data/lib/ap/core_ext/method.rb
CHANGED
data/lib/ap/core_ext/object.rb
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
# Copyright (c) 2010 Michael Dvorkin
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
|
2
2
|
#
|
|
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
6
|
class Object #:nodoc:
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
# Remaining instance '_methods' are handled in core_ext/class.rb.
|
|
8
|
+
%w(methods private_methods protected_methods public_methods singleton_methods).each do |name|
|
|
9
|
+
original_method = instance_method(name)
|
|
9
10
|
|
|
10
|
-
alias :"original_#{name}" :"#{name}"
|
|
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
|
|
16
16
|
end
|
|
17
|
-
end
|
|
17
|
+
end
|
data/lib/ap/core_ext/string.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010 Michael Dvorkin
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
|
2
2
|
#
|
|
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
|
|
@@ -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
|
@@ -1,38 +1,17 @@
|
|
|
1
|
-
# Copyright (c) 2010 Michael Dvorkin
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
|
2
2
|
#
|
|
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
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
|
|
35
14
|
|
|
36
15
|
end
|
|
37
16
|
|
|
38
|
-
ActionView::Base.send(:include, AwesomePrintActionView) if defined?(
|
|
17
|
+
ActionView::Base.send(:include, AwesomePrintActionView) if defined?(ActionView)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010 Michael Dvorkin
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
|
2
2
|
#
|
|
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
|
|
@@ -14,6 +14,8 @@ module AwesomePrintActiveRecord
|
|
|
14
14
|
#------------------------------------------------------------------------------
|
|
15
15
|
def printable_with_active_record(object)
|
|
16
16
|
printable = printable_without_active_record(object)
|
|
17
|
+
return printable if !defined?(ActiveRecord::Base)
|
|
18
|
+
|
|
17
19
|
if printable == :self
|
|
18
20
|
if object.is_a?(ActiveRecord::Base)
|
|
19
21
|
printable = :active_record_instance
|
|
@@ -27,6 +29,8 @@ module AwesomePrintActiveRecord
|
|
|
27
29
|
# Format ActiveRecord instance object.
|
|
28
30
|
#------------------------------------------------------------------------------
|
|
29
31
|
def awesome_active_record_instance(object)
|
|
32
|
+
return object.inspect if !defined?(ActiveSupport::OrderedHash)
|
|
33
|
+
|
|
30
34
|
data = object.class.column_names.inject(ActiveSupport::OrderedHash.new) do |hash, name|
|
|
31
35
|
hash[name.to_sym] = object.send(name) if object.has_attribute?(name) || object.new_record?
|
|
32
36
|
hash
|
|
@@ -37,17 +41,14 @@ module AwesomePrintActiveRecord
|
|
|
37
41
|
# Format ActiveRecord class object.
|
|
38
42
|
#------------------------------------------------------------------------------
|
|
39
43
|
def awesome_active_record_class(object)
|
|
40
|
-
if object.respond_to?(:columns)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
|
46
|
-
else
|
|
47
|
-
object.inspect
|
|
44
|
+
return object.inspect if !defined?(ActiveSupport::OrderedHash) || !object.respond_to?(:columns)
|
|
45
|
+
|
|
46
|
+
data = object.columns.inject(ActiveSupport::OrderedHash.new) do |hash, c|
|
|
47
|
+
hash[c.name.to_sym] = c.type
|
|
48
|
+
hash
|
|
48
49
|
end
|
|
50
|
+
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
|
49
51
|
end
|
|
50
|
-
|
|
51
52
|
end
|
|
52
53
|
|
|
53
54
|
AwesomePrint.send(:include, AwesomePrintActiveRecord)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010 Michael Dvorkin
|
|
1
|
+
# Copyright (c) 2010-2011 Michael Dvorkin
|
|
2
2
|
#
|
|
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
|
|
@@ -14,6 +14,8 @@ module AwesomePrintActiveSupport
|
|
|
14
14
|
#------------------------------------------------------------------------------
|
|
15
15
|
def printable_with_active_support(object)
|
|
16
16
|
printable = printable_without_active_support(object)
|
|
17
|
+
return printable if !defined?(ActiveSupport::TimeWithZone) || !defined?(HashWithIndifferentAccess)
|
|
18
|
+
|
|
17
19
|
if printable == :self
|
|
18
20
|
if object.is_a?(ActiveSupport::TimeWithZone)
|
|
19
21
|
printable = :active_support_time
|