awesome_print 0.3.0 → 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 CHANGED
@@ -1,3 +1,24 @@
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
+
18
+ 0.3.1 RubyConf X edition
19
+ - Fixed Ruby 1.8.6 compatibility issues (thanks, Tim!)
20
+ - Fixed stack overflow issue with Rails 2.3.x console
21
+
1
22
  0.3.0
2
23
  - Display object.methods and family in human readable format
3
24
  - Objects inherited from Array, Hash, File, Dir, and Struct are shown as their base class
data/LICENSE CHANGED
@@ -1,4 +1,5 @@
1
- Copyright (c) 2010 Michael Dvorkin
1
+ Copyright (c) 2010-2011 Michael Dvorkin
2
+ twitter.com/mid
2
3
  %w(mike dvorkin.net) * "@" || %w(mike fatfreecrm.com) * "@"
3
4
 
4
5
  Permission is hereby granted, free of charge, to any person obtaining
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 "ap"
18
+ require "awesome_print"
19
19
  ap object, options = {}
20
20
 
21
21
  Default options:
22
22
 
23
- :multiline => true,
24
- :plain => false,
25
- :indent => 4,
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 "ap"
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 "ap"
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 "ap"
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 "ap"
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 "ap"
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
- $ ruby script/console
132
- Loading development environment (Rails 2.3.5)
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 "ap"
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 an ap method to the Logger and ActiveSupport::BufferedLogger classes,
211
- allowing you to call:
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), or you can override on a per call basis with
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 an ap method to the ActionView::Base class making it available
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
- $ rake spec # Entire spec suite.
246
- $ ruby -rubygems spec/logger_spec.rb # Individual spec file (Ruby 1.8.7 and RSpec 1.3+)
247
- $ rspec spec/logger_spec.rb # Individual spec file (Ruby 1.9.2 and RSpec 2.0+)
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,14 +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
280
+ * Tim Harper -- http://github.com/timcharper
262
281
  * Tobias Crawley -- http://github.com/tobias
282
+ * Viktar Basharymau -- https://github.com/DNNX
263
283
 
264
284
  ### License ###
265
- Copyright (c) 2010 Michael Dvorkin
285
+ Copyright (c) 2010-2011 Michael Dvorkin
286
+
287
+ twitter.com/mid
288
+
266
289
  %w(mike dvorkin.net) * "@" || %w(mike fatfreecrm.com) * "@"
267
290
 
268
- Released under the MIT license. See LICENSE file for details.
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
- if RUBY_VERSION.to_f >= 1.9
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
- if RUBY_VERSION.to_f >= 1.9
29
- require "rspec/core/rake_task" # RSpec 2.0
30
- RSpec::Core::RakeTask.new(:spec) do |spec|
31
- spec.pattern = 'spec/**/*_spec.rb'
32
- spec.rspec_opts = ['--color']
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
- Spec::Rake::SpecTask.new(:rcov) do |spec|
48
- spec.libs << 'lib' << 'spec'
49
- spec.pattern = 'spec/**/*_spec.rb'
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.3.0
1
+ 0.4.0
data/init.rb CHANGED
@@ -1 +1 @@
1
- require File.join(File.dirname(__FILE__), "lib", "ap")
1
+ require File.join(File.dirname(__FILE__), "lib", "awesome_print")
@@ -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
- data = h.keys.map do |key|
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] if negative left align hash keys.
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) # Regular method?
178
- method_tuple(object.method(name))
179
- elsif object.respond_to?(:instance_method) # Unbound method?
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
- if @options[:plain] || @options[:color][type].nil?
243
- s
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
 
@@ -261,7 +270,7 @@ class AwesomePrint
261
270
  end
262
271
  end
263
272
  else # See http://ruby-doc.org/core/classes/Method.html#M001902
264
- args = method.arity.abs.times.map { |i| "arg#{i+1}" }
273
+ args = (1..method.arity.abs).map { |i| "arg#{i}" }
265
274
  args[-1] = "*#{args[-1]}" if method.arity < 0
266
275
  end
267
276
 
@@ -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
@@ -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
@@ -10,16 +10,17 @@
10
10
  # ap [].methods - Object.methods
11
11
  # ap ''.methods.grep(/!|\?/)
12
12
  #
13
- # If you could think of a better way please let me know: twitter.com/mid :-)
13
+ # If you could think of a better way please let me know :-)
14
14
  #
15
15
  class Array #:nodoc:
16
16
  [ :-, :& ].each do |operator|
17
- alias :"original_#{operator.object_id}" :"#{operator}"
17
+ original_operator = instance_method(operator)
18
+
18
19
  define_method operator do |*args|
19
- arr = self.send(:"original_#{operator.object_id}", *args)
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
@@ -44,7 +45,7 @@ class Array #:nodoc:
44
45
  # doesn't set $1 within the grep block which causes nil.to_sym failure.
45
46
  # The workaround below has been tested with Ruby 1.8.7/Rails 2.3.8 and
46
47
  # Ruby 1.9.2/Rails 3.0.0. For more info see the following thread dating
47
- # back to 1993 when Ruby 1.8.0 was as fresh off the grill as Ruby 1.9.2
48
+ # back to 2003 when Ruby 1.8.0 was as fresh off the grill as Ruby 1.9.2
48
49
  # is in 2010 :-)
49
50
  #
50
51
  # http://www.justskins.com/forums/bug-when-rerouting-string-52852.html
@@ -56,7 +57,10 @@ class Array #:nodoc:
56
57
  arr = unless blk
57
58
  original_grep(pattern)
58
59
  else
59
- original_grep(pattern) { |match| eval("%Q/#{match}/ =~ #{pattern.inspect}", blk.binding); yield match }
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__'))
@@ -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
- methods.grep(/instance_methods$/) do |name|
8
- alias :"original_#{name}" :"#{name}"
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.send(:"original_#{name}", *args)
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
@@ -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
 
@@ -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,5 +14,5 @@ module AwesomePrintLogger
14
14
 
15
15
  end
16
16
 
17
- Logger.send(:include, AwesomePrintLogger) if defined?(Logger)
17
+ Logger.send(:include, AwesomePrintLogger)
18
18
  ActiveSupport::BufferedLogger.send(:include, AwesomePrintLogger) if defined?(::ActiveSupport::BufferedLogger)
@@ -0,0 +1,21 @@
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
+ #
7
+ # Method#name was intorduced in Ruby 1.8.7 so we define it here as necessary.
8
+ #
9
+ unless nil.method(:class).respond_to?(:name)
10
+ class Method
11
+ def name
12
+ inspect.split(/[#.>]/)[-1]
13
+ end
14
+ end
15
+
16
+ class UnboundMethod
17
+ def name
18
+ inspect.split(/[#.>]/)[-1]
19
+ end
20
+ end
21
+ end
@@ -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
- methods.grep(/methods$/) do |name|
8
- next if name.to_s.include? 'instance' # Instance methods are trapped in Class.
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.send(:"original_#{name}", *args)
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
@@ -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
- [ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i|
15
- if STDOUT.tty? && ENV['TERM'] && ENV['TERM'] != 'dumb'
16
- define_method color do "\033[1;#{30+i}m#{self}\033[0m" end
17
- define_method :"#{color}ish" do "\033[0;#{30+i}m#{self}\033[0m" end
18
- else
19
- define_method color do self end
20
- alias_method :"#{color}ish", color
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
 
@@ -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
- def self.included(base)
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
- formatted = h(object.ai(options))
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?(::ActionView)
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
- data = object.columns.inject(ActiveSupport::OrderedHash.new) do |hash, c|
42
- hash[c.name.to_sym] = c.type
43
- hash
44
- end
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)