awesome_print 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/.gitignore +22 -0
  2. data/CHANGELOG +8 -0
  3. data/Gemfile +3 -0
  4. data/Gemfile.lock +26 -0
  5. data/README.md +52 -14
  6. data/Rakefile +2 -46
  7. data/lib/ap.rb +3 -17
  8. data/lib/awesome_print.rb +16 -12
  9. data/lib/{ap → awesome_print}/core_ext/array.rb +0 -0
  10. data/lib/{ap → awesome_print}/core_ext/class.rb +0 -0
  11. data/lib/{ap → awesome_print}/core_ext/kernel.rb +2 -2
  12. data/lib/awesome_print/core_ext/logger.rb +20 -0
  13. data/lib/{ap → awesome_print}/core_ext/method.rb +0 -0
  14. data/lib/{ap → awesome_print}/core_ext/object.rb +0 -0
  15. data/lib/{ap → awesome_print}/core_ext/string.rb +8 -5
  16. data/lib/awesome_print/ext/action_view.rb +18 -0
  17. data/lib/awesome_print/ext/active_record.rb +40 -0
  18. data/lib/awesome_print/ext/active_support.rb +40 -0
  19. data/lib/awesome_print/ext/mongo_mapper.rb +38 -0
  20. data/lib/awesome_print/ext/mongoid.rb +39 -0
  21. data/lib/awesome_print/ext/nokogiri.rb +45 -0
  22. data/lib/awesome_print/formatter.rb +350 -0
  23. data/lib/awesome_print/inspector.rb +140 -0
  24. data/{rails/init.rb → lib/awesome_print/version.rb} +5 -4
  25. data/spec/colors_spec.rb +106 -0
  26. data/spec/{awesome_print_spec.rb → formats_spec.rb} +187 -37
  27. data/spec/methods_spec.rb +20 -0
  28. data/spec/objects_spec.rb +79 -0
  29. data/spec/spec_helper.rb +1 -1
  30. metadata +49 -53
  31. data/VERSION +0 -1
  32. data/init.rb +0 -1
  33. data/lib/ap/awesome_print.rb +0 -352
  34. data/lib/ap/core_ext/logger.rb +0 -18
  35. data/lib/ap/mixin/action_view.rb +0 -17
  36. data/lib/ap/mixin/active_record.rb +0 -54
  37. data/lib/ap/mixin/active_support.rb +0 -46
  38. data/lib/ap/mixin/mongo_mapper.rb +0 -54
  39. data/spec/action_view_spec.rb +0 -25
  40. data/spec/active_record_spec.rb +0 -136
  41. data/spec/colorization_spec.rb +0 -84
  42. data/spec/logger_spec.rb +0 -43
  43. data/spec/mongo_mapper_spec.rb +0 -63
  44. data/spec/string_spec.rb +0 -20
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ .rvmrc
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ 1.0.0 Thanksgiving edition
2
+ - Added ability to format *arbitrary* Ruby object
3
+ - Added :limit option to limit large output for arrays and hashes (Andrew Horsman)
4
+ - Improved HTML formatting when :html => true (Daniel Johnson)
5
+ - Added Mongoid extension (Adam Doppelt)
6
+ - Added Nokogiri extension (Adam Doppelt)
7
+ - Removed Jeweler gem dependency
8
+
1
9
  0.4.0
2
10
  - 'ap object' now returns the object (Stephan Hagemann)
3
11
  - Added :html => true option to enable HTML colors rather that ANSI (ex. Sinatra templates)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+ gemspec
3
+
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ awesome_print (1.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ fakefs (0.3.2)
11
+ rspec (2.6.0)
12
+ rspec-core (~> 2.6.0)
13
+ rspec-expectations (~> 2.6.0)
14
+ rspec-mocks (~> 2.6.0)
15
+ rspec-core (2.6.4)
16
+ rspec-expectations (2.6.0)
17
+ diff-lcs (~> 1.1.2)
18
+ rspec-mocks (2.6.0)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ awesome_print!
25
+ fakefs (>= 0.2.1)
26
+ rspec (>= 2.6.0)
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  ## Awesome Print ##
2
- Awesome Print is Ruby library that pretty prints Ruby objects in full color
2
+ Awesome Print is a Ruby library that pretty prints Ruby objects in full color
3
3
  exposing their internal structure with proper indentation. Rails ActiveRecord
4
4
  objects and usage within Rails templates are supported via included mixins.
5
5
 
@@ -7,9 +7,6 @@ objects and usage within Rails templates are supported via included mixins.
7
7
  # Installing as Ruby gem
8
8
  $ gem install awesome_print
9
9
 
10
- # Installing as Rails plugin
11
- $ ruby script/plugin install http://github.com/michaeldv/awesome_print.git
12
-
13
10
  # Cloning the repository
14
11
  $ git clone git://github.com/michaeldv/awesome_print.git
15
12
 
@@ -20,26 +17,32 @@ objects and usage within Rails templates are supported via included mixins.
20
17
 
21
18
  Default options:
22
19
 
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.
20
+ :indent => 4, # Indent using 4 spaces.
21
+ :index => true, # Display array indices.
22
+ :html => false, # Use ANSI color codes rather than HTML.
23
+ :multiline => true, # Display in multiple lines.
24
+ :plain => false, # Use colors.
25
+ :sort_keys => false, # Do not sort hash keys.
26
+ :limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer.
29
27
  :color => {
28
+ :args => :pale,
30
29
  :array => :white,
31
- :bignum => :blue,
30
+ :bigdecimal => :blue,
32
31
  :class => :yellow,
33
32
  :date => :greenish,
34
33
  :falseclass => :red,
35
34
  :fixnum => :blue,
36
35
  :float => :blue,
37
- :hash => :gray,
36
+ :hash => :pale,
37
+ :keyword => :cyan,
38
+ :method => :purpleish,
38
39
  :nilclass => :red,
39
40
  :string => :yellowish,
41
+ :struct => :pale,
40
42
  :symbol => :cyanish,
41
43
  :time => :greenish,
42
- :trueclass => :green
44
+ :trueclass => :green,
45
+ :variable => :cyanish
43
46
  }
44
47
 
45
48
  Supported color names:
@@ -137,6 +140,35 @@ Supported color names:
137
140
  $ ruby 6.rb
138
141
  42
139
142
  true
143
+ $ cat 7.rb
144
+ require "awesome_print"
145
+ some_array = (1..1000).to_a
146
+ ap some_array, :limit => true
147
+ ^D
148
+ $ ruby 7.rb
149
+ [
150
+ [ 0] 1,
151
+ [ 1] 2,
152
+ [ 2] 3,
153
+ [ 3] .. [996],
154
+ [997] 998,
155
+ [998] 999,
156
+ [999] 1000
157
+ ]
158
+
159
+ $ cat 8.rb
160
+ require "awesome_print"
161
+ some_array = (1..1000).to_a
162
+ ap some_array, :limit => 5
163
+ ^D
164
+ $ ruby 8.rb
165
+ [
166
+ [ 0] 1,
167
+ [ 1] 2,
168
+ [ 2] .. [997],
169
+ [998] 999,
170
+ [999] 1000
171
+ ]
140
172
 
141
173
  ### Example (Rails console) ###
142
174
  $ rails console
@@ -270,11 +302,17 @@ For example:
270
302
 
271
303
  ### Contributors ###
272
304
 
305
+ * Adam Doppelt -- https://github.com/gurgeous
273
306
  * Andrew O'Brien -- https://github.com/AndrewO
307
+ * Andrew Horsman -- https://github.com/basicxman
308
+ * Benoit Daloze -- http://github.com/eregon
309
+ * Brandon Zylstra -- https://github.com/brandondrew
310
+ * Daniel Johnson -- https://github.com/adhd360
274
311
  * Daniel Bretoi -- http://github.com/danielb2
275
312
  * Eloy Duran -- http://github.com/alloy
276
313
  * Elpizo Choi -- https://github.com/fuJiin
277
- * Benoit Daloze -- http://github.com/eregon
314
+ * Greg Weber -- https://github.com/gregwebs
315
+ * Jeff Felchner -- https://github.com/jfelchner
278
316
  * Sean Gallagher -- http://github.com/torandu
279
317
  * Stephan Hagemann -- https://github.com/shageman
280
318
  * Tim Harper -- http://github.com/timcharper
data/Rakefile CHANGED
@@ -1,46 +1,2 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "awesome_print"
8
- gem.rubyforge_project = "awesome_print"
9
- gem.summary = %Q{Pretty print Ruby objects with proper indentation and colors.}
10
- gem.description = %Q{Great Ruby dubugging companion: pretty print Ruby objects to visualize their structure. Supports Rails ActiveRecord objects via included mixin.}
11
- gem.email = "mike@dvorkin.net"
12
- gem.homepage = "http://github.com/michaeldv/awesome_print"
13
- gem.authors = ["Michael Dvorkin"]
14
- gem.add_development_dependency "rspec", ">= 2.5.0"
15
- gem.files = FileList["[A-Z]*", "lib/**/*.rb", "rails/*.rb", "spec/*", "init.rb"]
16
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
- end
18
- Jeweler::GemcutterTasks.new
19
- rescue LoadError
20
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
- end
22
-
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
28
-
29
- RSpec::Core::RakeTask.new(:rcov) do |spec|
30
- spec.rcov = true
31
- spec.rcov_opts = %q[--exclude "spec"]
32
- end
33
-
34
- task :spec => :check_dependencies
35
-
36
- task :default => :spec
37
-
38
- require 'rake/rdoctask'
39
- Rake::RDocTask.new do |rdoc|
40
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
-
42
- rdoc.rdoc_dir = 'rdoc'
43
- rdoc.title = "ap #{version}"
44
- rdoc.rdoc_files.include('README*')
45
- rdoc.rdoc_files.include('lib/**/*.rb')
46
- end
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/lib/ap.rb CHANGED
@@ -4,21 +4,7 @@
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
5
  #------------------------------------------------------------------------------
6
6
  #
7
- # AwesomePrint might be loaded implicitly through ~/.irbrc so do nothing
8
- # for subsequent requires.
7
+ # Keeping this for backwards compatibility to allow
8
+ # require "ap"
9
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
14
-
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)
18
-
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
10
+ require File.dirname(__FILE__) + "/awesome_print"
@@ -4,23 +4,27 @@
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
5
  #------------------------------------------------------------------------------
6
6
  #
7
- # This is the copy of original 'ap.rb' file that matches the gem name. It makes
8
- # it possible to omit the :require part in bundler's Gemfile:
9
- #
10
- # gem 'awesome_print', '>= 0.4.0'
7
+ # AwesomePrint might be loaded implicitly through ~/.irbrc so do nothing
8
+ # for subsequent requires.
11
9
  #
12
10
  unless defined?(AwesomePrint)
13
11
  %w(array string method object class kernel).each do |file|
14
- require File.dirname(__FILE__) + "/ap/core_ext/#{file}"
12
+ require File.dirname(__FILE__) + "/awesome_print/core_ext/#{file}"
15
13
  end
16
14
 
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)
15
+ require File.dirname(__FILE__) + "/awesome_print/inspector"
16
+ require File.dirname(__FILE__) + "/awesome_print/formatter"
17
+ require File.dirname(__FILE__) + "/awesome_print/version"
18
+ require File.dirname(__FILE__) + "/awesome_print/core_ext/logger" if defined?(Logger)
20
19
 
21
- # Load the following under normal circumstatnces as well as in Rails
20
+ # Load the following under normal circumstances as well as in Rails
22
21
  # 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)
22
+ require File.dirname(__FILE__) + "/awesome_print/ext/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV'])
23
+ require File.dirname(__FILE__) + "/awesome_print/ext/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV'])
24
+
25
+ # Load remaining extensions.
26
+ require File.dirname(__FILE__) + "/awesome_print/ext/action_view" if defined?(ActionView)
27
+ require File.dirname(__FILE__) + "/awesome_print/ext/mongo_mapper" if defined?(MongoMapper)
28
+ require File.dirname(__FILE__) + "/awesome_print/ext/mongoid" if defined?(Mongoid)
29
+ require File.dirname(__FILE__) + "/awesome_print/ext/nokogiri" if defined?(Nokogiri)
26
30
  end
@@ -6,8 +6,8 @@
6
6
  module Kernel
7
7
 
8
8
  def ai(options = {})
9
- ap = AwesomePrint.new(options)
10
- ap.send(:awesome, self)
9
+ ap = AwesomePrint::Inspector.new(options)
10
+ ap.awesome self
11
11
  end
12
12
  alias :awesome_inspect :ai
13
13
 
@@ -0,0 +1,20 @@
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 AwesomePrint
7
+ module Logger
8
+
9
+ # Add ap method to logger
10
+ #------------------------------------------------------------------------------
11
+ def ap(object, level = nil)
12
+ level ||= AwesomePrint.defaults[:log_level] if AwesomePrint.defaults
13
+ level ||= :debug
14
+ send level, object.ai
15
+ end
16
+ end
17
+ end
18
+
19
+ Logger.send(:include, AwesomePrint::Logger)
20
+ ActiveSupport::BufferedLogger.send(:include, AwesomePrint::Logger) if defined?(::ActiveSupport::BufferedLogger)
@@ -4,21 +4,24 @@
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
5
  #------------------------------------------------------------------------------
6
6
  class String
7
-
7
+ #
8
8
  # ANSI color codes:
9
- # \033 => escape
9
+ # \e => escape
10
10
  # 30 => color base
11
11
  # 1 => bright
12
12
  # 0 => normal
13
-
13
+ #
14
+ # For HTML coloring we use <kbd> tag instead of <span> to require monospace
15
+ # font. Note that beloved <tt> has been removed from HTML5.
16
+ #
14
17
  %w(gray red green yellow blue purple cyan white).zip(
15
18
  %w(black darkred darkgreen brown navy darkmagenta darkcyan slategray)).each_with_index do |(color, shade), i|
16
19
  define_method color do |*html|
17
- html[0] ? %Q|<pre style="color:#{color}">#{self}</pre>| : "\033[1;#{30+i}m#{self}\033[0m"
20
+ html[0] ? %Q|<kbd style="color:#{color}">#{self}</kbd>| : "\e[1;#{30+i}m#{self}\e[0m"
18
21
  end
19
22
 
20
23
  define_method "#{color}ish" do |*html|
21
- html[0] ? %Q|<pre style="color:#{shade}">#{self}</pre>| : "\033[0;#{30+i}m#{self}\033[0m"
24
+ html[0] ? %Q|<kbd style="color:#{shade}">#{self}</kbd>| : "\e[0;#{30+i}m#{self}\e[0m"
22
25
  end
23
26
  end
24
27
 
@@ -0,0 +1,18 @@
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 AwesomePrint
7
+ module ActionView
8
+
9
+ # Use HTML colors and add default "debug_dump" class to the resulting HTML.
10
+ def ap_debug(object, options = {})
11
+ object.ai(options.merge(:html => true)).sub(/^<pre([\s>])/, '<pre class="debug_dump"\\1')
12
+ end
13
+
14
+ alias_method :ap, :ap_debug
15
+ end
16
+ end
17
+
18
+ ActionView::Base.send(:include, AwesomePrint::ActionView) if defined?(ActionView)
@@ -0,0 +1,40 @@
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 AwesomePrint
7
+ module ActiveRecord
8
+
9
+ def self.included(base)
10
+ base.send :alias_method, :cast_without_active_record, :cast
11
+ base.send :alias_method, :cast, :cast_with_active_record
12
+ end
13
+
14
+ # Add ActiveRecord class names to the dispatcher pipeline.
15
+ #------------------------------------------------------------------------------
16
+ def cast_with_active_record(object, type)
17
+ cast = cast_without_active_record(object, type)
18
+ if defined?(::ActiveRecord) && object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base)
19
+ cast = :active_record_class
20
+ end
21
+ cast
22
+ end
23
+
24
+ private
25
+
26
+ # Format ActiveRecord class object.
27
+ #------------------------------------------------------------------------------
28
+ def awesome_active_record_class(object)
29
+ return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:columns) || object.to_s == "ActiveRecord::Base"
30
+
31
+ data = object.columns.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
32
+ hash[c.name.to_sym] = c.type
33
+ hash
34
+ end
35
+ "class #{object} < #{object.superclass} " << awesome_hash(data)
36
+ end
37
+ end
38
+ end
39
+
40
+ AwesomePrint::Formatter.send(:include, AwesomePrint::ActiveRecord)
@@ -0,0 +1,40 @@
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 AwesomePrint
7
+ module ActiveSupport
8
+
9
+ def self.included(base)
10
+ base.send :alias_method, :cast_without_active_support, :cast
11
+ base.send :alias_method, :cast, :cast_with_active_support
12
+ end
13
+
14
+ def cast_with_active_support(object, type)
15
+ cast = cast_without_active_support(object, type)
16
+ if defined?(::ActiveSupport) && defined?(::HashWithIndifferentAccess)
17
+ if object.is_a?(::ActiveSupport::TimeWithZone)
18
+ cast = :active_support_time
19
+ elsif object.is_a?(::HashWithIndifferentAccess)
20
+ cast = :hash_with_indifferent_access
21
+ end
22
+ end
23
+ cast
24
+ end
25
+
26
+ # Format ActiveSupport::TimeWithZone as standard Time.
27
+ #------------------------------------------------------------------------------
28
+ def awesome_active_support_time(object)
29
+ colorize(object.inspect, :time)
30
+ end
31
+
32
+ # Format HashWithIndifferentAccess as standard Hash.
33
+ #------------------------------------------------------------------------------
34
+ def awesome_hash_with_indifferent_access(object)
35
+ awesome_hash(object)
36
+ end
37
+ end
38
+ end
39
+
40
+ AwesomePrint::Formatter.send(:include, AwesomePrint::ActiveSupport)