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
@@ -1,18 +0,0 @@
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 AwesomePrintLogger
7
-
8
- # Add ap method to logger
9
- #------------------------------------------------------------------------------
10
- def ap(object, level = nil)
11
- level ||= AwesomePrint.defaults[:log_level] || :debug
12
- send level, object.ai
13
- end
14
-
15
- end
16
-
17
- Logger.send(:include, AwesomePrintLogger)
18
- ActiveSupport::BufferedLogger.send(:include, AwesomePrintLogger) if defined?(::ActiveSupport::BufferedLogger)
@@ -1,17 +0,0 @@
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 AwesomePrintActionView
7
-
8
- # Use HTML colors and add default "debug_dump" class to the resulting HTML.
9
- def ap_debug(object, options = {})
10
- object.ai(options.merge(:html => true)).sub(/^<pre([\s>])/, '<pre class="debug_dump"\\1')
11
- end
12
-
13
- alias_method :ap, :ap_debug
14
-
15
- end
16
-
17
- ActionView::Base.send(:include, AwesomePrintActionView) if defined?(ActionView)
@@ -1,54 +0,0 @@
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 AwesomePrintActiveRecord
7
-
8
- def self.included(base)
9
- base.send :alias_method, :printable_without_active_record, :printable
10
- base.send :alias_method, :printable, :printable_with_active_record
11
- end
12
-
13
- # Add ActiveRecord class names to the dispatcher pipeline.
14
- #------------------------------------------------------------------------------
15
- def printable_with_active_record(object)
16
- printable = printable_without_active_record(object)
17
- return printable if !defined?(ActiveRecord::Base)
18
-
19
- if printable == :self
20
- if object.is_a?(ActiveRecord::Base)
21
- printable = :active_record_instance
22
- end
23
- elsif printable == :class and object.ancestors.include?(ActiveRecord::Base)
24
- printable = :active_record_class
25
- end
26
- printable
27
- end
28
-
29
- # Format ActiveRecord instance object.
30
- #------------------------------------------------------------------------------
31
- def awesome_active_record_instance(object)
32
- return object.inspect if !defined?(ActiveSupport::OrderedHash)
33
-
34
- data = object.class.column_names.inject(ActiveSupport::OrderedHash.new) do |hash, name|
35
- hash[name.to_sym] = object.send(name) if object.has_attribute?(name) || object.new_record?
36
- hash
37
- end
38
- "#{object} " + awesome_hash(data)
39
- end
40
-
41
- # Format ActiveRecord class object.
42
- #------------------------------------------------------------------------------
43
- def awesome_active_record_class(object)
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
49
- end
50
- "class #{object} < #{object.superclass} " << awesome_hash(data)
51
- end
52
- end
53
-
54
- AwesomePrint.send(:include, AwesomePrintActiveRecord)
@@ -1,46 +0,0 @@
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 AwesomePrintActiveSupport
7
-
8
- def self.included(base)
9
- base.send :alias_method, :printable_without_active_support, :printable
10
- base.send :alias_method, :printable, :printable_with_active_support
11
- end
12
-
13
- # Add ActiveSupport class names to the dispatcher pipeline.
14
- #------------------------------------------------------------------------------
15
- def printable_with_active_support(object)
16
- printable = printable_without_active_support(object)
17
- return printable if !defined?(ActiveSupport::TimeWithZone) || !defined?(HashWithIndifferentAccess)
18
-
19
- if printable == :self
20
- if object.is_a?(ActiveSupport::TimeWithZone)
21
- printable = :active_support_time
22
- elsif object.is_a?(HashWithIndifferentAccess)
23
- printable = :hash_with_indifferent_access
24
- end
25
- end
26
- printable
27
- end
28
-
29
- # Format ActiveSupport::TimeWithZone as standard Time.
30
- #------------------------------------------------------------------------------
31
- def awesome_active_support_time(object)
32
- awesome_self(object, :as => :time)
33
- end
34
-
35
- # Format HashWithIndifferentAccess as standard Hash.
36
- #
37
- # NOTE: can't use awesome_self(object, :as => :hash) since awesome_self uses
38
- # object.inspect internally, i.e. it would convert hash to string.
39
- #------------------------------------------------------------------------------
40
- def awesome_hash_with_indifferent_access(object)
41
- awesome_hash(object)
42
- end
43
-
44
- end
45
-
46
- AwesomePrint.send(:include, AwesomePrintActiveSupport)
@@ -1,54 +0,0 @@
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)
@@ -1,25 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- begin
4
- require 'action_view'
5
- require 'ap/mixin/action_view'
6
-
7
- describe "AwesomePrint ActionView extension" do
8
- before(:each) do
9
- @view = ActionView::Base.new
10
- end
11
-
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>|
15
- end
16
-
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">&quot; &amp;&lt;hello&gt;&quot;</pre>'
20
- end
21
- end
22
-
23
- rescue LoadError
24
- puts "Skipping ActionView specs..."
25
- end
@@ -1,136 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- begin
4
- require 'active_record'
5
- require 'ap/mixin/active_record'
6
-
7
- if defined?(::ActiveRecord)
8
-
9
- # Create tableless ActiveRecord model.
10
- #------------------------------------------------------------------------------
11
- class User < ActiveRecord::Base
12
- def self.columns()
13
- @columns ||= []
14
- end
15
-
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
-
20
- column :id, :integer
21
- column :name, :string
22
- column :rank, :integer
23
- column :admin, :boolean
24
- column :created_at, :datetime
25
-
26
- def self.table_exists?
27
- true
28
- end
29
- end
30
-
31
- class SubUser < User
32
- def self.columns
33
- User.columns
34
- end
35
- end
36
-
37
- describe "AwesomePrint/ActiveRecord" do
38
- before(:each) do
39
- stub_dotfile!
40
- end
41
-
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
- #<User:0x01234567> {
55
- :id => nil,
56
- :name => "Diana",
57
- :rank => 1,
58
- :admin => false,
59
- :created_at => ?
60
- }
61
- EOS
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
-
68
- out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
69
- end
70
-
71
- it "display multiple records" do
72
- out = @ap.send(:awesome, [ @diana, @laura ])
73
- str = <<-EOS.strip
74
- [
75
- [0] #<User:0x01234567> {
76
- :id => nil,
77
- :name => "Diana",
78
- :rank => 1,
79
- :admin => false,
80
- :created_at => ?
81
- },
82
- [1] #<User:0x01234567> {
83
- :id => nil,
84
- :name => "Laura",
85
- :rank => 2,
86
- :admin => true,
87
- :created_at => !
88
- }
89
- ]
90
- EOS
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
-
99
- out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
100
- end
101
- end
102
-
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
- class User < ActiveRecord::Base {
109
- :id => :integer,
110
- :name => :string,
111
- :rank => :integer,
112
- :admin => :boolean,
113
- :created_at => :datetime
114
- }
115
- EOS
116
- end
117
-
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
121
- class SubUser < User {
122
- :id => :integer,
123
- :name => :string,
124
- :rank => :integer,
125
- :admin => :boolean,
126
- :created_at => :datetime
127
- }
128
- EOS
129
- end
130
- end
131
- end
132
- end
133
-
134
- rescue LoadError
135
- puts "Skipping ActiveRecord specs..."
136
- end
@@ -1,84 +0,0 @@
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
@@ -1,43 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
-
4
- require 'logger'
5
- require 'ap/core_ext/logger'
6
-
7
- describe "AwesomePrint logging extensions" do
8
- before(:all) do
9
- @logger = Logger.new('/dev/null') rescue Logger.new('nul')
10
- end
11
-
12
- describe "ap method" do
13
- it "should awesome_inspect the given object" do
14
- object = mock
15
- object.should_receive(:ai)
16
- @logger.ap object
17
- end
18
-
19
- describe "the log level" do
20
- before(:each) do
21
- AwesomePrint.defaults = { }
22
- end
23
-
24
- it "should fallback to the default :debug log level" do
25
- @logger.should_receive(:debug)
26
- @logger.ap(nil)
27
- end
28
-
29
- it "should use the global user default if no level passed" do
30
- AwesomePrint.defaults = { :log_level => :info }
31
- @logger.should_receive(:info)
32
- @logger.ap(nil)
33
- end
34
-
35
- it "should use the passed in level" do
36
- @logger.should_receive(:warn)
37
- @logger.ap(nil, :warn)
38
- end
39
- end
40
- end
41
- end
42
-
43
-