awesome_print 1.1.0 → 1.2.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/.gitignore +1 -0
- data/CHANGELOG +8 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +11 -11
- data/LICENSE +1 -1
- data/README.md +253 -219
- data/lib/ap.rb +1 -1
- data/lib/awesome_print.rb +4 -1
- data/lib/awesome_print/core_ext/array.rb +1 -1
- data/lib/awesome_print/core_ext/class.rb +1 -1
- data/lib/awesome_print/core_ext/kernel.rb +7 -2
- data/lib/awesome_print/core_ext/logger.rb +1 -1
- data/lib/awesome_print/core_ext/method.rb +1 -1
- data/lib/awesome_print/core_ext/object.rb +1 -1
- data/lib/awesome_print/core_ext/string.rb +1 -1
- data/lib/awesome_print/ext/action_view.rb +1 -1
- data/lib/awesome_print/ext/active_record.rb +5 -2
- data/lib/awesome_print/ext/active_support.rb +1 -1
- data/lib/awesome_print/ext/mongo_mapper.rb +1 -1
- data/lib/awesome_print/ext/mongoid.rb +2 -2
- data/lib/awesome_print/ext/no_brainer.rb +58 -0
- data/lib/awesome_print/ext/nokogiri.rb +1 -1
- data/lib/awesome_print/ext/ripple.rb +72 -0
- data/lib/awesome_print/ext/sequel.rb +57 -0
- data/lib/awesome_print/formatter.rb +15 -7
- data/lib/awesome_print/inspector.rb +1 -1
- data/lib/awesome_print/version.rb +2 -2
- data/spec/formats_spec.rb +47 -0
- data/spec/misc_spec.rb +19 -3
- data/spec/spec_helper.rb +1 -1
- metadata +48 -62
data/lib/ap.rb
CHANGED
data/lib/awesome_print.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2013 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
|
@@ -29,4 +29,7 @@ unless defined?(AwesomePrint::Inspector)
|
|
29
29
|
require File.dirname(__FILE__) + "/awesome_print/ext/mongo_mapper" if defined?(MongoMapper)
|
30
30
|
require File.dirname(__FILE__) + "/awesome_print/ext/mongoid" if defined?(Mongoid)
|
31
31
|
require File.dirname(__FILE__) + "/awesome_print/ext/nokogiri" if defined?(Nokogiri)
|
32
|
+
require File.dirname(__FILE__) + "/awesome_print/ext/no_brainer" if defined?(NoBrainer)
|
33
|
+
require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple)
|
34
|
+
require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel)
|
32
35
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2013 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
|
@@ -7,7 +7,12 @@ module Kernel
|
|
7
7
|
|
8
8
|
def ai(options = {})
|
9
9
|
ap = AwesomePrint::Inspector.new(options)
|
10
|
-
ap.awesome self
|
10
|
+
awesome = ap.awesome self
|
11
|
+
if options[:html]
|
12
|
+
awesome = "<pre>#{awesome}</pre>"
|
13
|
+
awesome = awesome.html_safe if defined? ActiveSupport
|
14
|
+
end
|
15
|
+
awesome
|
11
16
|
end
|
12
17
|
alias :awesome_inspect :ai
|
13
18
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2013 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
|
@@ -43,7 +43,10 @@ module AwesomePrint
|
|
43
43
|
return awesome_object(object) if @options[:raw]
|
44
44
|
|
45
45
|
data = object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
|
46
|
-
|
46
|
+
if object.has_attribute?(name) || object.new_record?
|
47
|
+
value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name)
|
48
|
+
hash[name.to_sym] = value
|
49
|
+
end
|
47
50
|
hash
|
48
51
|
end
|
49
52
|
"#{object} " << awesome_hash(data)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2013 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
|
@@ -44,7 +44,7 @@ module AwesomePrint
|
|
44
44
|
def awesome_mongoid_document(object)
|
45
45
|
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
|
46
46
|
|
47
|
-
data = object.attributes.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
|
47
|
+
data = (object.attributes || {}).sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
|
48
48
|
hash[c[0].to_sym] = c[1]
|
49
49
|
hash
|
50
50
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Copyright (c) 2010-2013 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 NoBrainer
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.send :alias_method, :cast_without_no_brainer, :cast
|
11
|
+
base.send :alias_method, :cast, :cast_with_no_brainer
|
12
|
+
end
|
13
|
+
|
14
|
+
# Add NoBrainer class names to the dispatcher pipeline.
|
15
|
+
#------------------------------------------------------------------------------
|
16
|
+
def cast_with_no_brainer(object, type)
|
17
|
+
cast = cast_without_no_brainer(object, type)
|
18
|
+
if defined?(::NoBrainer::Document)
|
19
|
+
if object.is_a?(Class) && object.ancestors.include?(::NoBrainer::Document)
|
20
|
+
cast = :no_brainer_class
|
21
|
+
elsif object.class.ancestors.include?(::NoBrainer::Document)
|
22
|
+
cast = :no_brainer_document
|
23
|
+
end
|
24
|
+
end
|
25
|
+
cast
|
26
|
+
end
|
27
|
+
|
28
|
+
# Format NoBrainer class object.
|
29
|
+
#------------------------------------------------------------------------------
|
30
|
+
def awesome_no_brainer_class(object)
|
31
|
+
return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields)
|
32
|
+
|
33
|
+
# We want id first
|
34
|
+
data = object.fields.sort_by { |key| key[0] == :id ? '_id' : key[0].to_s }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
|
35
|
+
hash[c[0]] = :object
|
36
|
+
hash
|
37
|
+
end
|
38
|
+
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Format NoBrainer Document object.
|
42
|
+
#------------------------------------------------------------------------------
|
43
|
+
def awesome_no_brainer_document(object)
|
44
|
+
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
|
45
|
+
|
46
|
+
data = object.attributes.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
|
47
|
+
hash[c[0].to_sym] = c[1]
|
48
|
+
hash
|
49
|
+
end
|
50
|
+
if !object.errors.empty?
|
51
|
+
data = {:errors => object.errors, :attributes => data}
|
52
|
+
end
|
53
|
+
"#{object} #{awesome_hash(data)}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
AwesomePrint::Formatter.send(:include, AwesomePrint::NoBrainer)
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# Copyright (c) 2010-2013 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 Ripple
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.send :alias_method, :cast_without_ripple, :cast
|
11
|
+
base.send :alias_method, :cast, :cast_with_ripple
|
12
|
+
end
|
13
|
+
|
14
|
+
# Add Ripple class names to the dispatcher pipeline.
|
15
|
+
#------------------------------------------------------------------------------
|
16
|
+
def cast_with_ripple(object, type)
|
17
|
+
cast = cast_without_ripple(object, type)
|
18
|
+
return cast if !defined?(::Ripple)
|
19
|
+
|
20
|
+
if object.is_a?(::Ripple::AttributeMethods) # Module used to access attributes across documents and embedded documents
|
21
|
+
cast = :ripple_document_instance
|
22
|
+
elsif object.is_a?(::Ripple::Properties) # Used to access property metadata on Ripple classes
|
23
|
+
cast = :ripple_document_class
|
24
|
+
end
|
25
|
+
cast
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# Format Ripple instance object.
|
31
|
+
#
|
32
|
+
# NOTE: by default only instance attributes are shown. To format a Ripple document instance
|
33
|
+
# as a regular object showing its instance variables and accessors use :raw => true option:
|
34
|
+
#
|
35
|
+
# ap document, :raw => true
|
36
|
+
#
|
37
|
+
#------------------------------------------------------------------------------
|
38
|
+
def awesome_ripple_document_instance(object)
|
39
|
+
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
|
40
|
+
return awesome_object(object) if @options[:raw]
|
41
|
+
exclude_assoc = @options[:exclude_assoc] or @options[:exclude_associations]
|
42
|
+
|
43
|
+
data = object.attributes.inject(::ActiveSupport::OrderedHash.new) do |hash, (name, value)|
|
44
|
+
hash[name.to_sym] = object.send(name)
|
45
|
+
hash
|
46
|
+
end
|
47
|
+
|
48
|
+
unless exclude_assoc
|
49
|
+
data = object.class.embedded_associations.inject(data) do |hash, assoc|
|
50
|
+
hash[assoc.name] = object.get_proxy(assoc) # Should always be array or Ripple::EmbeddedDocument for embedded associations
|
51
|
+
hash
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
"##{object} " << awesome_hash(data)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Format Ripple class object.
|
59
|
+
#------------------------------------------------------------------------------
|
60
|
+
def awesome_ripple_document_class(object)
|
61
|
+
return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:properties)
|
62
|
+
|
63
|
+
data = object.properties.inject(::ActiveSupport::OrderedHash.new) do |hash, (name, defn)|
|
64
|
+
hash[name.to_sym] = defn.type.to_s.downcase.to_sym
|
65
|
+
hash
|
66
|
+
end
|
67
|
+
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
AwesomePrint::Formatter.send(:include, AwesomePrint::Ripple)
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# Copyright (c) 2010-2013 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 Sequel
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.send :alias_method, :cast_without_sequel, :cast
|
11
|
+
base.send :alias_method, :cast, :cast_with_sequel
|
12
|
+
end
|
13
|
+
|
14
|
+
# Add Sequel class names to the dispatcher pipeline.
|
15
|
+
#------------------------------------------------------------------------------
|
16
|
+
def cast_with_sequel(object, type)
|
17
|
+
cast = cast_without_sequel(object, type)
|
18
|
+
if defined?(::Sequel::Model) && object.is_a?(::Sequel::Model)
|
19
|
+
cast = :sequel_document
|
20
|
+
elsif defined?(::Sequel::Model) && object.is_a?(Class) && object.ancestors.include?(::Sequel::Model)
|
21
|
+
cast = :sequel_model_class
|
22
|
+
elsif defined?(::Sequel::Mysql2::Dataset) && object.class.ancestors.include?(::Sequel::Mysql2::Dataset)
|
23
|
+
cast = :sequel_dataset
|
24
|
+
end
|
25
|
+
cast
|
26
|
+
end
|
27
|
+
|
28
|
+
# Format Sequel Document object.
|
29
|
+
#------------------------------------------------------------------------------
|
30
|
+
def awesome_sequel_document(object)
|
31
|
+
data = object.values.sort_by { |key| key.to_s }.inject({}) do |hash, c|
|
32
|
+
hash[c[0].to_sym] = c[1]
|
33
|
+
hash
|
34
|
+
end
|
35
|
+
if !object.errors.empty?
|
36
|
+
data = {:errors => object.errors, :values => data}
|
37
|
+
end
|
38
|
+
"#{object} #{awesome_hash(data)}"
|
39
|
+
end
|
40
|
+
|
41
|
+
# Format Sequel Dataset object.
|
42
|
+
#------------------------------------------------------------------------------
|
43
|
+
def awesome_sequel_dataset(dataset)
|
44
|
+
[awesome_array(dataset.to_a), awesome_print(dataset.sql)].join("\n")
|
45
|
+
end
|
46
|
+
|
47
|
+
# Format Sequel Model class.
|
48
|
+
#------------------------------------------------------------------------------
|
49
|
+
def awesome_sequel_model_class(object)
|
50
|
+
data = object.db_schema.inject({}) {|h, (name,data)| h.merge(name => data[:db_type])}
|
51
|
+
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
AwesomePrint::Formatter.send(:include, AwesomePrint::Sequel)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2013 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
|
@@ -9,7 +9,7 @@ require "shellwords"
|
|
9
9
|
module AwesomePrint
|
10
10
|
class Formatter
|
11
11
|
|
12
|
-
CORE = [ :array, :
|
12
|
+
CORE = [ :array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod ]
|
13
13
|
DEFAULT_LIMIT_SIZE = 7
|
14
14
|
|
15
15
|
def initialize(inspector)
|
@@ -27,7 +27,7 @@ module AwesomePrint
|
|
27
27
|
else
|
28
28
|
awesome_self(object, type) # Catch all that falls back to object.inspect.
|
29
29
|
end
|
30
|
-
|
30
|
+
awesome
|
31
31
|
end
|
32
32
|
|
33
33
|
# Hook this when adding custom formatters. Check out lib/awesome_print/ext
|
@@ -63,6 +63,8 @@ module AwesomePrint
|
|
63
63
|
def awesome_self(object, type)
|
64
64
|
if @options[:raw] && object.instance_variables.any?
|
65
65
|
awesome_object(object)
|
66
|
+
elsif object == ENV
|
67
|
+
awesome_hash(object.to_hash)
|
66
68
|
else
|
67
69
|
colorize(object.inspect.to_s, type)
|
68
70
|
end
|
@@ -126,7 +128,7 @@ module AwesomePrint
|
|
126
128
|
#------------------------------------------------------------------------------
|
127
129
|
def awesome_object(o)
|
128
130
|
vars = o.instance_variables.map do |var|
|
129
|
-
property = var[1..-1].to_sym
|
131
|
+
property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet.
|
130
132
|
accessor = if o.respond_to?(:"#{property}=")
|
131
133
|
o.respond_to?(property) ? :accessor : :writer
|
132
134
|
else
|
@@ -162,6 +164,12 @@ module AwesomePrint
|
|
162
164
|
end
|
163
165
|
end
|
164
166
|
|
167
|
+
# Format a set.
|
168
|
+
#------------------------------------------------------------------------------
|
169
|
+
def awesome_set(s)
|
170
|
+
awesome_array(s.to_a)
|
171
|
+
end
|
172
|
+
|
165
173
|
# Format a Struct.
|
166
174
|
#------------------------------------------------------------------------------
|
167
175
|
def awesome_struct(s)
|
@@ -237,9 +245,9 @@ module AwesomePrint
|
|
237
245
|
if the_method && the_method.respond_to?(:arity) # Is this original object#method?
|
238
246
|
method_tuple(the_method) # Yes, we are good.
|
239
247
|
end
|
240
|
-
elsif object.respond_to?(:instance_method)
|
241
|
-
method_tuple(object.instance_method(name))
|
242
|
-
end
|
248
|
+
elsif object.respond_to?(:instance_method) # Is this an unbound method?
|
249
|
+
method_tuple(object.instance_method(name)) rescue nil # Rescue to avoid NameError when the method is not
|
250
|
+
end # available (ex. File.lchmod on Ubuntu 12).
|
243
251
|
end
|
244
252
|
tuple || [ name.to_s, '(?)', '?' ] # Return WTF default if all the above fails.
|
245
253
|
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2013 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 AwesomePrint
|
7
7
|
def self.version
|
8
|
-
|
8
|
+
"1.2.0"
|
9
9
|
end
|
10
10
|
end
|
data/spec/formats_spec.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
require "bigdecimal"
|
3
3
|
require "rational"
|
4
|
+
require "set"
|
4
5
|
|
5
6
|
describe "AwesomePrint" do
|
6
7
|
before do
|
@@ -511,6 +512,52 @@ EOS
|
|
511
512
|
end
|
512
513
|
end
|
513
514
|
|
515
|
+
#------------------------------------------------------------------------------
|
516
|
+
describe "Set" do
|
517
|
+
before do
|
518
|
+
@arr = [1, :two, "three" ]
|
519
|
+
@set = Set.new(@arr)
|
520
|
+
end
|
521
|
+
|
522
|
+
it "empty set" do
|
523
|
+
Set.new.ai.should == [].ai
|
524
|
+
end
|
525
|
+
|
526
|
+
if RUBY_VERSION > "1.9"
|
527
|
+
it "plain multiline" do
|
528
|
+
@set.ai(:plain => true).should == @arr.ai(:plain => true)
|
529
|
+
end
|
530
|
+
|
531
|
+
it "plain multiline indented" do
|
532
|
+
@set.ai(:plain => true, :indent => 1).should == @arr.ai(:plain => true, :indent => 1)
|
533
|
+
end
|
534
|
+
|
535
|
+
it "plain single line" do
|
536
|
+
@set.ai(:plain => true, :multiline => false).should == @arr.ai(:plain => true, :multiline => false)
|
537
|
+
end
|
538
|
+
|
539
|
+
it "colored multiline (default)" do
|
540
|
+
@set.ai.should == @arr.ai
|
541
|
+
end
|
542
|
+
else # Prior to Ruby 1.9 the order of set values is unpredicatble.
|
543
|
+
it "plain multiline" do
|
544
|
+
@set.sort_by{ |x| x.to_s }.ai(:plain => true).should == @arr.sort_by{ |x| x.to_s }.ai(:plain => true)
|
545
|
+
end
|
546
|
+
|
547
|
+
it "plain multiline indented" do
|
548
|
+
@set.sort_by{ |x| x.to_s }.ai(:plain => true, :indent => 1).should == @arr.sort_by{ |x| x.to_s }.ai(:plain => true, :indent => 1)
|
549
|
+
end
|
550
|
+
|
551
|
+
it "plain single line" do
|
552
|
+
@set.sort_by{ |x| x.to_s }.ai(:plain => true, :multiline => false).should == @arr.sort_by{ |x| x.to_s }.ai(:plain => true, :multiline => false)
|
553
|
+
end
|
554
|
+
|
555
|
+
it "colored multiline (default)" do
|
556
|
+
@set.sort_by{ |x| x.to_s }.ai.should == @arr.sort_by{ |x| x.to_s }.ai
|
557
|
+
end
|
558
|
+
end
|
559
|
+
end
|
560
|
+
|
514
561
|
#------------------------------------------------------------------------------
|
515
562
|
describe "Struct" do
|
516
563
|
before do
|