awesome_print 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +6 -0
- data/README.md +33 -1
- data/VERSION +1 -1
- data/lib/ap.rb +5 -1
- data/lib/ap/awesome_print.rb +6 -5
- data/lib/ap/core_ext/logger.rb +18 -0
- data/lib/ap/mixin/{rails.rb → active_record.rb} +8 -15
- data/lib/ap/mixin/active_support.rb +44 -0
- data/spec/{rails_spec.rb → active_record_spec.rb} +38 -8
- data/spec/awesome_print_spec.rb +1 -1
- data/spec/logger_spec.rb +43 -0
- data/spec/spec_helper.rb +1 -0
- metadata +11 -7
data/CHANGELOG
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
0.2.0
|
2
|
+
- Added support for logger.ap (including Rails logger)
|
3
|
+
- Added support for HashWithIndifferentAccess from ActiveSupport
|
4
|
+
- ap now works with scripts that use ActiveRecord/ActiveSupport outside Rails
|
5
|
+
- ap now correctly shows file and directory names with fancy characters (shell escape)
|
6
|
+
|
1
7
|
0.1.4
|
2
8
|
- Format BigDecimal and Rational objects as Float scalars
|
3
9
|
- Explicit options parameter can override custom defaults
|
data/README.md
CHANGED
@@ -144,6 +144,32 @@ Supported color names:
|
|
144
144
|
}
|
145
145
|
rails>
|
146
146
|
|
147
|
+
### IRB integration ###
|
148
|
+
To use awesome_print as default formatter in irb and Rails console add the following
|
149
|
+
lines into your ~/.irbrc file:
|
150
|
+
|
151
|
+
require "rubygems"
|
152
|
+
require "ap"
|
153
|
+
IRB::Irb.class_eval do
|
154
|
+
def output_value
|
155
|
+
ap @context.last_value
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
### Logger Convenience Method ###
|
160
|
+
awesome_print adds an ap method to the Logger and ActiveSupport::BufferedLogger classes,
|
161
|
+
allowing you to call:
|
162
|
+
|
163
|
+
logger.ap object
|
164
|
+
|
165
|
+
By default, this logs at the :debug level. You can override that globally with
|
166
|
+
|
167
|
+
:log_level => :info
|
168
|
+
|
169
|
+
in the custom defaults (see below), or you can override on a per call basis with
|
170
|
+
|
171
|
+
logger.ap object, :warn
|
172
|
+
|
147
173
|
### Setting Custom Defaults ###
|
148
174
|
You can set your own default options by creating ``.aprc`` file in your home
|
149
175
|
directory. Within that file assign your defaults to ``AwesomePrint.defaults``.
|
@@ -165,8 +191,14 @@ For example:
|
|
165
191
|
* Commit, do not mess with rakefile, version, or history.
|
166
192
|
* Send me a pull request.
|
167
193
|
|
194
|
+
### Contributors ###
|
195
|
+
|
196
|
+
* Daniel Bretoi -- http://github.com/danielb2
|
197
|
+
* eregon -- http://github.com/eregon
|
198
|
+
* Tobias Crawley -- http://github.com/tobias
|
199
|
+
|
168
200
|
### License ###
|
169
201
|
Copyright (c) 2010 Michael Dvorkin
|
170
202
|
%w(mike dvorkin.net) * "@" || %w(mike fatfreecrm.com) * "@"
|
171
203
|
|
172
|
-
Released under the MIT license. See LICENSE file for details.
|
204
|
+
Released under the MIT license. See LICENSE file for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/ap.rb
CHANGED
@@ -7,4 +7,8 @@ require File.dirname(__FILE__) + "/ap/core_ext/string"
|
|
7
7
|
require File.dirname(__FILE__) + "/ap/core_ext/kernel"
|
8
8
|
require File.dirname(__FILE__) + "/ap/awesome_print"
|
9
9
|
|
10
|
-
require File.dirname(__FILE__) + "/ap/
|
10
|
+
require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(::Logger) or defined?(::ActiveSupport::BufferedLogger)
|
11
|
+
|
12
|
+
require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(::ActiveRecord)
|
13
|
+
require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(::ActiveSupport)
|
14
|
+
|
data/lib/ap/awesome_print.rb
CHANGED
@@ -3,6 +3,8 @@
|
|
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 "shellwords"
|
7
|
+
|
6
8
|
class AwesomePrint
|
7
9
|
AP = :__awesome_print__
|
8
10
|
CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational ]
|
@@ -20,7 +22,7 @@ class AwesomePrint
|
|
20
22
|
:falseclass => :red,
|
21
23
|
:fixnum => :blue,
|
22
24
|
:float => :blue,
|
23
|
-
:hash => :
|
25
|
+
:hash => :pale,
|
24
26
|
:nilclass => :red,
|
25
27
|
:string => :yellowish,
|
26
28
|
:symbol => :cyanish,
|
@@ -36,8 +38,7 @@ class AwesomePrint
|
|
36
38
|
@indentation = @options[:indent].abs
|
37
39
|
Thread.current[AP] ||= []
|
38
40
|
end
|
39
|
-
|
40
|
-
|
41
|
+
|
41
42
|
private
|
42
43
|
|
43
44
|
# Format an array.
|
@@ -104,14 +105,14 @@ class AwesomePrint
|
|
104
105
|
# Format File object.
|
105
106
|
#------------------------------------------------------------------------------
|
106
107
|
def awesome_file(f)
|
107
|
-
ls = File.directory?(f) ? `ls -adlF #{f.path}` : `ls -alF #{f.path}`
|
108
|
+
ls = File.directory?(f) ? `ls -adlF #{f.path.shellescape}` : `ls -alF #{f.path.shellescape}`
|
108
109
|
awesome_self(f, :with => ls.empty? ? nil : "\n#{ls.chop}")
|
109
110
|
end
|
110
111
|
|
111
112
|
# Format Dir object.
|
112
113
|
#------------------------------------------------------------------------------
|
113
114
|
def awesome_dir(d)
|
114
|
-
ls = `ls -alF #{d.path}`
|
115
|
+
ls = `ls -alF #{d.path.shellescape}`
|
115
116
|
awesome_self(d, :with => ls.empty? ? nil : "\n#{ls.chop}")
|
116
117
|
end
|
117
118
|
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Copyright (c) 2010 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) if defined?(Logger)
|
18
|
+
ActiveSupport::BufferedLogger.send(:include, AwesomePrintLogger) if defined?(::ActiveSupport::BufferedLogger)
|
@@ -3,23 +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
|
-
module
|
6
|
+
module AwesomePrintActiveRecord
|
7
7
|
|
8
8
|
def self.included(base)
|
9
|
-
base.
|
9
|
+
base.send :alias_method, :printable_without_active_record, :printable
|
10
|
+
base.send :alias_method, :printable, :printable_with_active_record
|
10
11
|
end
|
11
12
|
|
12
13
|
# Add ActiveRecord class names to the dispatcher pipeline.
|
13
14
|
#------------------------------------------------------------------------------
|
14
|
-
def
|
15
|
-
printable =
|
15
|
+
def printable_with_active_record(object)
|
16
|
+
printable = printable_without_active_record(object)
|
16
17
|
if printable == :self
|
17
18
|
if object.is_a?(ActiveRecord::Base)
|
18
19
|
printable = :active_record_instance
|
19
|
-
elsif object.is_a?(ActiveSupport::TimeWithZone)
|
20
|
-
printable = :active_support_time
|
21
20
|
end
|
22
|
-
elsif printable == :class
|
21
|
+
elsif printable == :class and object.ancestors.include?(ActiveRecord::Base)
|
23
22
|
printable = :active_record_class
|
24
23
|
end
|
25
24
|
printable
|
@@ -48,13 +47,7 @@ module AwesomePrintRails
|
|
48
47
|
object.inspect
|
49
48
|
end
|
50
49
|
end
|
51
|
-
|
52
|
-
# Format ActiveSupport::TimeWithZone as standard Time.
|
53
|
-
#------------------------------------------------------------------------------
|
54
|
-
def awesome_active_support_time(object)
|
55
|
-
awesome_self(object, :as => :time)
|
56
|
-
end
|
57
|
-
|
50
|
+
|
58
51
|
end
|
59
52
|
|
60
|
-
AwesomePrint.send(:include,
|
53
|
+
AwesomePrint.send(:include, AwesomePrintActiveRecord)
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# Copyright (c) 2010 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
|
+
if printable == :self
|
18
|
+
if object.is_a?(ActiveSupport::TimeWithZone)
|
19
|
+
printable = :active_support_time
|
20
|
+
elsif object.is_a?(HashWithIndifferentAccess)
|
21
|
+
printable = :hash_with_indifferent_access
|
22
|
+
end
|
23
|
+
end
|
24
|
+
printable
|
25
|
+
end
|
26
|
+
|
27
|
+
# Format ActiveSupport::TimeWithZone as standard Time.
|
28
|
+
#------------------------------------------------------------------------------
|
29
|
+
def awesome_active_support_time(object)
|
30
|
+
awesome_self(object, :as => :time)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Format HashWithIndifferentAccess as standard Hash.
|
34
|
+
#
|
35
|
+
# NOTE: can't use awesome_self(object, :as => :hash) since awesome_self uses
|
36
|
+
# object.inspect internally, i.e. it would convert hash to string.
|
37
|
+
#------------------------------------------------------------------------------
|
38
|
+
def awesome_hash_with_indifferent_access(object)
|
39
|
+
awesome_hash(object)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
AwesomePrint.send(:include, AwesomePrintActiveSupport)
|
@@ -1,6 +1,10 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
|
3
|
+
require 'active_record'
|
4
|
+
require 'ap/mixin/active_record'
|
5
|
+
|
6
|
+
|
7
|
+
if defined?(::ActiveRecord)
|
4
8
|
|
5
9
|
# Create tableless ActiveRecord model.
|
6
10
|
#------------------------------------------------------------------------------
|
@@ -18,9 +22,19 @@ if defined?(::Rails)
|
|
18
22
|
column :rank, :integer
|
19
23
|
column :admin, :boolean
|
20
24
|
column :created_at, :datetime
|
21
|
-
end
|
22
25
|
|
23
|
-
|
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
|
24
38
|
before(:each) do
|
25
39
|
stub_dotfile!
|
26
40
|
end
|
@@ -28,6 +42,7 @@ if defined?(::Rails)
|
|
28
42
|
#------------------------------------------------------------------------------
|
29
43
|
describe "ActiveRecord instance" do
|
30
44
|
before(:each) do
|
45
|
+
ActiveRecord::Base.default_timezone = :utc
|
31
46
|
@diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00")
|
32
47
|
@laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00")
|
33
48
|
@ap = AwesomePrint.new(:plain => true)
|
@@ -41,7 +56,7 @@ if defined?(::Rails)
|
|
41
56
|
:name => "Diana",
|
42
57
|
:rank => 1,
|
43
58
|
:admin => false,
|
44
|
-
:created_at => Sat
|
59
|
+
:created_at => Sat Oct 10 12:30:00 UTC 1992
|
45
60
|
}
|
46
61
|
EOS
|
47
62
|
end
|
@@ -55,14 +70,14 @@ EOS
|
|
55
70
|
:name => "Diana",
|
56
71
|
:rank => 1,
|
57
72
|
:admin => false,
|
58
|
-
:created_at => Sat
|
73
|
+
:created_at => Sat Oct 10 12:30:00 UTC 1992
|
59
74
|
},
|
60
75
|
[1] #<User:0x01234567> {
|
61
76
|
:id => nil,
|
62
77
|
:name => "Laura",
|
63
78
|
:rank => 2,
|
64
79
|
:admin => true,
|
65
|
-
:created_at => Mon
|
80
|
+
:created_at => Mon May 26 14:15:00 UTC 2003
|
66
81
|
}
|
67
82
|
]
|
68
83
|
EOS
|
@@ -71,7 +86,7 @@ EOS
|
|
71
86
|
|
72
87
|
#------------------------------------------------------------------------------
|
73
88
|
describe "ActiveRecord class" do
|
74
|
-
it "should" do
|
89
|
+
it "should print the class" do
|
75
90
|
@ap = AwesomePrint.new(:plain => true)
|
76
91
|
@ap.send(:awesome, User).should == <<-EOS.strip
|
77
92
|
class User < ActiveRecord::Base {
|
@@ -81,7 +96,22 @@ class User < ActiveRecord::Base {
|
|
81
96
|
:admin => :boolean,
|
82
97
|
:created_at => :datetime
|
83
98
|
}
|
84
|
-
EOS
|
99
|
+
EOS
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should print the class for non-direct subclasses of AR::Base" do
|
104
|
+
@ap = AwesomePrint.new(:plain => true)
|
105
|
+
@ap.send(:awesome, SubUser).should == <<-EOS.strip
|
106
|
+
class SubUser < User {
|
107
|
+
:id => :integer,
|
108
|
+
:name => :string,
|
109
|
+
:rank => :integer,
|
110
|
+
:admin => :boolean,
|
111
|
+
:created_at => :datetime
|
112
|
+
}
|
113
|
+
EOS
|
114
|
+
|
85
115
|
end
|
86
116
|
end
|
87
117
|
end
|
data/spec/awesome_print_spec.rb
CHANGED
@@ -267,7 +267,7 @@ EOS
|
|
267
267
|
#------------------------------------------------------------------------------
|
268
268
|
describe "BigDecimal and Rational" do
|
269
269
|
it "should present BigDecimal object as Float scalar" do
|
270
|
-
big = BigDecimal("2010.
|
270
|
+
big = BigDecimal("2010.4")
|
271
271
|
big.ai(:plain => true).should == "2010.4"
|
272
272
|
end
|
273
273
|
|
data/spec/logger_spec.rb
ADDED
@@ -0,0 +1,43 @@
|
|
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')
|
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
|
+
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Michael Dvorkin
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-05-05 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -50,11 +50,14 @@ files:
|
|
50
50
|
- lib/ap.rb
|
51
51
|
- lib/ap/awesome_print.rb
|
52
52
|
- lib/ap/core_ext/kernel.rb
|
53
|
+
- lib/ap/core_ext/logger.rb
|
53
54
|
- lib/ap/core_ext/string.rb
|
54
|
-
- lib/ap/mixin/
|
55
|
+
- lib/ap/mixin/active_record.rb
|
56
|
+
- lib/ap/mixin/active_support.rb
|
55
57
|
- rails/init.rb
|
58
|
+
- spec/active_record_spec.rb
|
56
59
|
- spec/awesome_print_spec.rb
|
57
|
-
- spec/
|
60
|
+
- spec/logger_spec.rb
|
58
61
|
- spec/spec.opts
|
59
62
|
- spec/spec_helper.rb
|
60
63
|
- spec/string_spec.rb
|
@@ -89,7 +92,8 @@ signing_key:
|
|
89
92
|
specification_version: 3
|
90
93
|
summary: Pretty print Ruby objects with proper indentation and colors.
|
91
94
|
test_files:
|
95
|
+
- spec/active_record_spec.rb
|
92
96
|
- spec/awesome_print_spec.rb
|
93
|
-
- spec/
|
97
|
+
- spec/logger_spec.rb
|
94
98
|
- spec/spec_helper.rb
|
95
99
|
- spec/string_spec.rb
|