awesome_print 1.7.0 → 1.8.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.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/Appraisals +11 -0
- data/CHANGELOG.md +10 -0
- data/CONTRIBUTING.md +1 -1
- data/README.md +47 -38
- data/Rakefile +5 -5
- data/lib/ap.rb +2 -2
- data/lib/awesome_print.rb +18 -17
- data/lib/awesome_print/colorize.rb +1 -1
- data/lib/awesome_print/core_ext/{array.rb → awesome_method_array.rb} +16 -18
- data/lib/awesome_print/core_ext/class.rb +3 -2
- data/lib/awesome_print/core_ext/kernel.rb +1 -1
- data/lib/awesome_print/core_ext/logger.rb +1 -1
- data/lib/awesome_print/core_ext/method.rb +2 -2
- data/lib/awesome_print/core_ext/object.rb +3 -2
- data/lib/awesome_print/core_ext/string.rb +3 -3
- data/lib/awesome_print/custom_defaults.rb +57 -0
- data/lib/awesome_print/ext/action_view.rb +8 -4
- data/lib/awesome_print/ext/active_record.rb +19 -11
- data/lib/awesome_print/ext/active_support.rb +1 -1
- data/lib/awesome_print/ext/mongo_mapper.rb +16 -13
- data/lib/awesome_print/ext/mongoid.rb +8 -6
- data/lib/awesome_print/ext/nobrainer.rb +8 -5
- data/lib/awesome_print/ext/nokogiri.rb +4 -4
- data/lib/awesome_print/ext/ostruct.rb +1 -1
- data/lib/awesome_print/ext/ripple.rb +5 -6
- data/lib/awesome_print/ext/sequel.rb +7 -6
- data/lib/awesome_print/formatter.rb +11 -19
- data/lib/awesome_print/formatters.rb +15 -0
- data/lib/awesome_print/formatters/array_formatter.rb +108 -42
- data/lib/awesome_print/formatters/base_formatter.rb +13 -11
- data/lib/awesome_print/formatters/class_formatter.rb +2 -1
- data/lib/awesome_print/formatters/dir_formatter.rb +1 -1
- data/lib/awesome_print/formatters/file_formatter.rb +1 -1
- data/lib/awesome_print/formatters/hash_formatter.rb +74 -22
- data/lib/awesome_print/formatters/object_formatter.rb +9 -14
- data/lib/awesome_print/formatters/struct_formatter.rb +71 -0
- data/lib/awesome_print/inspector.rb +77 -93
- data/lib/awesome_print/version.rb +2 -2
- data/spec/active_record_helper.rb +8 -2
- data/spec/colors_spec.rb +30 -30
- data/spec/core_ext/logger_spec.rb +43 -0
- data/spec/core_ext/string_spec.rb +20 -0
- data/spec/ext/action_view_spec.rb +18 -0
- data/spec/ext/active_record_spec.rb +252 -0
- data/spec/ext/active_support_spec.rb +26 -0
- data/spec/ext/mongo_mapper_spec.rb +261 -0
- data/spec/ext/mongoid_spec.rb +104 -0
- data/spec/ext/nobrainer_spec.rb +59 -0
- data/spec/ext/nokogiri_spec.rb +46 -0
- data/spec/ext/ostruct_spec.rb +22 -0
- data/spec/ext/ripple_spec.rb +48 -0
- data/spec/formats_spec.rb +193 -165
- data/spec/methods_spec.rb +116 -128
- data/spec/misc_spec.rb +104 -108
- data/spec/objects_spec.rb +70 -28
- data/spec/spec_helper.rb +27 -10
- data/spec/support/active_record_data.rb +20 -0
- data/spec/support/active_record_data/3_2_diana.txt +24 -0
- data/spec/support/active_record_data/3_2_diana_legacy.txt +24 -0
- data/spec/support/active_record_data/3_2_multi.txt +50 -0
- data/spec/support/active_record_data/3_2_multi_legacy.txt +50 -0
- data/spec/support/active_record_data/4_0_diana.txt +98 -0
- data/spec/support/active_record_data/4_0_multi.txt +198 -0
- data/spec/support/active_record_data/4_1_diana.txt +97 -0
- data/spec/support/active_record_data/4_1_multi.txt +196 -0
- data/spec/support/active_record_data/4_2_diana.txt +109 -0
- data/spec/support/active_record_data/4_2_diana_legacy.txt +109 -0
- data/spec/support/active_record_data/4_2_multi.txt +220 -0
- data/spec/support/active_record_data/4_2_multi_legacy.txt +220 -0
- data/spec/support/active_record_data/5_0_diana.txt +105 -0
- data/spec/support/active_record_data/5_0_multi.txt +212 -0
- data/spec/support/ext_verifier.rb +42 -0
- data/spec/support/mongoid_versions.rb +22 -0
- data/spec/support/rails_versions.rb +35 -0
- metadata +79 -4
@@ -0,0 +1,57 @@
|
|
1
|
+
module AwesomePrint
|
2
|
+
class << self
|
3
|
+
attr_accessor :defaults, :force_colors
|
4
|
+
|
5
|
+
# Class accessor to force colorized output (ex. forked subprocess where TERM
|
6
|
+
# might be dumb).
|
7
|
+
#---------------------------------------------------------------------------
|
8
|
+
def force_colors!(value = true)
|
9
|
+
@force_colors = value
|
10
|
+
end
|
11
|
+
|
12
|
+
def console?
|
13
|
+
boolean(defined?(IRB) || defined?(Pry))
|
14
|
+
end
|
15
|
+
|
16
|
+
def rails_console?
|
17
|
+
console? && boolean(defined?(Rails::Console) || ENV['RAILS_ENV'])
|
18
|
+
end
|
19
|
+
|
20
|
+
def diet_rb
|
21
|
+
IRB.formatter = Class.new(IRB::Formatter) do
|
22
|
+
def inspect_object(object)
|
23
|
+
object.ai
|
24
|
+
end
|
25
|
+
end.new
|
26
|
+
end
|
27
|
+
|
28
|
+
def usual_rb
|
29
|
+
IRB::Irb.class_eval do
|
30
|
+
def output_value
|
31
|
+
ap @context.last_value
|
32
|
+
rescue NoMethodError
|
33
|
+
puts "(Object doesn't support #ai)"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def irb!
|
39
|
+
return unless defined?(IRB)
|
40
|
+
|
41
|
+
IRB.version.include?('DietRB') ? diet_rb : usual_rb
|
42
|
+
end
|
43
|
+
|
44
|
+
def pry!
|
45
|
+
Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
# Takes a value and returns true unless it is false or nil
|
51
|
+
# This is an alternative to the less readable !!(value)
|
52
|
+
# https://github.com/bbatsov/ruby-style-guide#no-bang-bang
|
53
|
+
def boolean(value)
|
54
|
+
value ? true : false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -1,17 +1,21 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
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
|
module ActionView
|
8
|
-
|
9
8
|
# Use HTML colors and add default "debug_dump" class to the resulting HTML.
|
10
9
|
def ap_debug(object, options = {})
|
11
|
-
object.ai(
|
10
|
+
object.ai(
|
11
|
+
options.merge(html: true)
|
12
|
+
).sub(
|
13
|
+
/^<pre([\s>])/,
|
14
|
+
'<pre class="debug_dump"\\1'
|
15
|
+
)
|
12
16
|
end
|
13
17
|
|
14
|
-
|
18
|
+
alias ap ap_debug
|
15
19
|
end
|
16
20
|
end
|
17
21
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
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
|
@@ -15,7 +15,7 @@ module AwesomePrint
|
|
15
15
|
#------------------------------------------------------------------------------
|
16
16
|
def cast_with_active_record(object, type)
|
17
17
|
cast = cast_without_active_record(object, type)
|
18
|
-
return cast if !defined?(::ActiveRecord)
|
18
|
+
return cast if !defined?(::ActiveRecord::Base)
|
19
19
|
|
20
20
|
if object.is_a?(::ActiveRecord::Base)
|
21
21
|
cast = :active_record_instance
|
@@ -42,27 +42,35 @@ module AwesomePrint
|
|
42
42
|
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
|
43
43
|
return awesome_object(object) if @options[:raw]
|
44
44
|
|
45
|
-
data = object.class.column_names.
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
45
|
+
data = if object.class.column_names != object.attributes.keys
|
46
|
+
object.attributes
|
47
|
+
else
|
48
|
+
object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
|
49
|
+
if object.has_attribute?(name) || object.new_record?
|
50
|
+
value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name)
|
51
|
+
hash[name.to_sym] = value
|
52
|
+
end
|
53
|
+
hash
|
54
|
+
end
|
55
|
+
end
|
52
56
|
"#{object} " << awesome_hash(data)
|
53
57
|
end
|
54
58
|
|
55
59
|
# Format ActiveRecord class object.
|
56
60
|
#------------------------------------------------------------------------------
|
57
61
|
def awesome_active_record_class(object)
|
58
|
-
return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:columns) || object.to_s ==
|
62
|
+
return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:columns) || object.to_s == 'ActiveRecord::Base'
|
59
63
|
return awesome_class(object) if object.respond_to?(:abstract_class?) && object.abstract_class?
|
60
64
|
|
61
65
|
data = object.columns.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
|
62
66
|
hash[c.name.to_sym] = c.type
|
63
67
|
hash
|
64
68
|
end
|
65
|
-
|
69
|
+
|
70
|
+
name = "class #{awesome_simple(object.to_s, :class)}"
|
71
|
+
base = "< #{awesome_simple(object.superclass.to_s, :class)}"
|
72
|
+
|
73
|
+
[name, base, awesome_hash(data)].join(' ')
|
66
74
|
end
|
67
75
|
end
|
68
76
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
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
|
@@ -18,7 +18,7 @@ module AwesomePrint
|
|
18
18
|
cast = cast_without_mongo_mapper(object, type)
|
19
19
|
|
20
20
|
if defined?(::MongoMapper::Document)
|
21
|
-
if object.is_a?(Class) && (object.ancestors & [
|
21
|
+
if object.is_a?(Class) && (object.ancestors & [::MongoMapper::Document, ::MongoMapper::EmbeddedDocument]).size > 0
|
22
22
|
cast = :mongo_mapper_class
|
23
23
|
elsif object.is_a?(::MongoMapper::Document) || object.is_a?(::MongoMapper::EmbeddedDocument)
|
24
24
|
cast = :mongo_mapper_instance
|
@@ -38,7 +38,7 @@ module AwesomePrint
|
|
38
38
|
return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:keys)
|
39
39
|
|
40
40
|
data = object.keys.sort.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
|
41
|
-
hash[c.first] = (c.last.type ||
|
41
|
+
hash[c.first] = (c.last.type || 'undefined').to_s.underscore.intern
|
42
42
|
hash
|
43
43
|
end
|
44
44
|
|
@@ -49,7 +49,10 @@ module AwesomePrint
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
"class #{object
|
52
|
+
name = "class #{awesome_simple(object.to_s, :class)}"
|
53
|
+
base = "< #{awesome_simple(object.superclass.to_s, :class)}"
|
54
|
+
|
55
|
+
[name, base, awesome_hash(data)].join(' ')
|
53
56
|
end
|
54
57
|
|
55
58
|
# Format MongoMapper instance object.
|
@@ -65,7 +68,7 @@ module AwesomePrint
|
|
65
68
|
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
|
66
69
|
return awesome_object(object) if @options[:raw]
|
67
70
|
|
68
|
-
data = object.keys.keys.sort_by{|k| k}.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
|
71
|
+
data = object.keys.keys.sort_by { |k| k }.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
|
69
72
|
hash[name] = object[name]
|
70
73
|
hash
|
71
74
|
end
|
@@ -73,11 +76,11 @@ module AwesomePrint
|
|
73
76
|
# Add in associations
|
74
77
|
if @options[:mongo_mapper][:show_associations]
|
75
78
|
object.associations.each do |name, assoc|
|
76
|
-
if @options[:mongo_mapper][:inline_embedded] and assoc.embeddable?
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
79
|
+
data[name.to_s] = if @options[:mongo_mapper][:inline_embedded] and assoc.embeddable?
|
80
|
+
object.send(name)
|
81
|
+
else
|
82
|
+
assoc
|
83
|
+
end
|
81
84
|
end
|
82
85
|
end
|
83
86
|
|
@@ -93,7 +96,7 @@ module AwesomePrint
|
|
93
96
|
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
|
94
97
|
return awesome_object(object) if @options[:raw]
|
95
98
|
|
96
|
-
association = object.class.name.split('::').last.titleize.downcase.sub(/ association$/,'')
|
99
|
+
association = object.class.name.split('::').last.titleize.downcase.sub(/ association$/, '')
|
97
100
|
association = "embeds #{association}" if object.embeddable?
|
98
101
|
class_name = object.class_name
|
99
102
|
|
@@ -111,8 +114,8 @@ module AwesomePrint
|
|
111
114
|
def apply_default_mongo_mapper_options
|
112
115
|
@options[:color][:assoc] ||= :greenish
|
113
116
|
@options[:mongo_mapper] ||= {
|
114
|
-
:
|
115
|
-
:
|
117
|
+
show_associations: false, # Display association data for MongoMapper documents and classes.
|
118
|
+
inline_embedded: false # Display embedded associations inline with MongoMapper documents.
|
116
119
|
}
|
117
120
|
end
|
118
121
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
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
|
@@ -33,10 +33,14 @@ module AwesomePrint
|
|
33
33
|
return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields)
|
34
34
|
|
35
35
|
data = object.fields.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
|
36
|
-
hash[c[1].name.to_sym] = (c[1].type ||
|
36
|
+
hash[c[1].name.to_sym] = (c[1].type || 'undefined').to_s.underscore.intern
|
37
37
|
hash
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
|
+
name = "class #{awesome_simple(object.to_s, :class)}"
|
41
|
+
base = "< #{awesome_simple(object.superclass.to_s, :class)}"
|
42
|
+
|
43
|
+
[name, base, awesome_hash(data)].join(' ')
|
40
44
|
end
|
41
45
|
|
42
46
|
# Format Mongoid Document object.
|
@@ -48,9 +52,7 @@ module AwesomePrint
|
|
48
52
|
hash[c[0].to_sym] = c[1]
|
49
53
|
hash
|
50
54
|
end
|
51
|
-
if !object.errors.empty?
|
52
|
-
data = {:errors => object.errors, :attributes => data}
|
53
|
-
end
|
55
|
+
data = { errors: object.errors, attributes: data } if !object.errors.empty?
|
54
56
|
"#{object} #{awesome_hash(data)}"
|
55
57
|
end
|
56
58
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
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
|
@@ -28,19 +28,22 @@ module AwesomePrint
|
|
28
28
|
# Format NoBrainer class object.
|
29
29
|
#------------------------------------------------------------------------------
|
30
30
|
def awesome_nobrainer_class(object)
|
31
|
+
name = "#{awesome_simple(object, :class)} < #{awesome_simple(object.superclass, :class)}"
|
31
32
|
data = Hash[object.fields.map do |field, options|
|
32
33
|
[field, (options[:type] || Object).to_s.underscore.to_sym]
|
33
34
|
end]
|
34
|
-
|
35
|
+
|
36
|
+
name = "class #{awesome_simple(object.to_s, :class)}"
|
37
|
+
base = "< #{awesome_simple(object.superclass.to_s, :class)}"
|
38
|
+
|
39
|
+
[name, base, awesome_hash(data)].join(' ')
|
35
40
|
end
|
36
41
|
|
37
42
|
# Format NoBrainer Document object.
|
38
43
|
#------------------------------------------------------------------------------
|
39
44
|
def awesome_nobrainer_document(object)
|
40
45
|
data = object.inspectable_attributes.symbolize_keys
|
41
|
-
if object.errors.present?
|
42
|
-
data = {:errors => object.errors, :attributes => data}
|
43
|
-
end
|
46
|
+
data = { errors: object.errors, attributes: data } if object.errors.present?
|
44
47
|
"#{object} #{awesome_hash(data)}"
|
45
48
|
end
|
46
49
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
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
|
@@ -15,7 +15,7 @@ module AwesomePrint
|
|
15
15
|
#------------------------------------------------------------------------------
|
16
16
|
def cast_with_nokogiri(object, type)
|
17
17
|
cast = cast_without_nokogiri(object, type)
|
18
|
-
if (defined?(::Nokogiri::XML::Node) && object.is_a?(::Nokogiri::XML::Node)) ||
|
18
|
+
if (defined?(::Nokogiri::XML::Node) && object.is_a?(::Nokogiri::XML::Node)) ||
|
19
19
|
(defined?(::Nokogiri::XML::NodeSet) && object.is_a?(::Nokogiri::XML::NodeSet))
|
20
20
|
cast = :nokogiri_xml_node
|
21
21
|
end
|
@@ -25,9 +25,9 @@ module AwesomePrint
|
|
25
25
|
#------------------------------------------------------------------------------
|
26
26
|
def awesome_nokogiri_xml_node(object)
|
27
27
|
if object.is_a?(::Nokogiri::XML::NodeSet) && object.empty?
|
28
|
-
return
|
28
|
+
return '[]'
|
29
29
|
end
|
30
|
-
xml = object.to_xml(:
|
30
|
+
xml = object.to_xml(indent: 2)
|
31
31
|
#
|
32
32
|
# Colorize tag, id/class name, and contents.
|
33
33
|
#
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
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
|
@@ -60,11 +60,10 @@ module AwesomePrint
|
|
60
60
|
def awesome_ripple_document_class(object)
|
61
61
|
return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:properties)
|
62
62
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
63
|
+
name = "class #{awesome_simple(object.to_s, :class)}"
|
64
|
+
base = "< #{awesome_simple(object.superclass.to_s, :class)}"
|
65
|
+
|
66
|
+
[name, base, awesome_hash(data)].join(' ')
|
68
67
|
end
|
69
68
|
end
|
70
69
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
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
|
@@ -32,9 +32,7 @@ module AwesomePrint
|
|
32
32
|
hash[c[0].to_sym] = c[1]
|
33
33
|
hash
|
34
34
|
end
|
35
|
-
if !object.errors.empty?
|
36
|
-
data = {:errors => object.errors, :values => data}
|
37
|
-
end
|
35
|
+
data = { errors: object.errors, values: data } if !object.errors.empty?
|
38
36
|
"#{object} #{awesome_hash(data)}"
|
39
37
|
end
|
40
38
|
|
@@ -47,8 +45,11 @@ module AwesomePrint
|
|
47
45
|
# Format Sequel Model class.
|
48
46
|
#------------------------------------------------------------------------------
|
49
47
|
def awesome_sequel_model_class(object)
|
50
|
-
data = object.db_schema.inject({}) {|h, (
|
51
|
-
"class #{object
|
48
|
+
data = object.db_schema.inject({}) { |h, (prop, defn)| h.merge(prop => defn[:db_type]) }
|
49
|
+
name = "class #{awesome_simple(object.to_s, :class)}"
|
50
|
+
base = "< #{awesome_simple(object.superclass.to_s, :class)}"
|
51
|
+
|
52
|
+
[name, base, awesome_hash(data)].join(' ')
|
52
53
|
end
|
53
54
|
end
|
54
55
|
|
@@ -1,17 +1,9 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
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
|
-
|
7
|
-
require_relative "formatters/hash_formatter"
|
8
|
-
require_relative "formatters/array_formatter"
|
9
|
-
require_relative "formatters/simple_formatter"
|
10
|
-
require_relative "formatters/method_formatter"
|
11
|
-
require_relative "formatters/class_formatter"
|
12
|
-
require_relative "formatters/dir_formatter"
|
13
|
-
require_relative "formatters/file_formatter"
|
14
|
-
require_relative "colorize"
|
6
|
+
require 'awesome_print/formatters'
|
15
7
|
|
16
8
|
module AwesomePrint
|
17
9
|
class Formatter
|
@@ -19,7 +11,7 @@ module AwesomePrint
|
|
19
11
|
|
20
12
|
attr_reader :inspector, :options
|
21
13
|
|
22
|
-
CORE = [
|
14
|
+
CORE = [:array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod]
|
23
15
|
|
24
16
|
def initialize(inspector)
|
25
17
|
@inspector = inspector
|
@@ -52,7 +44,7 @@ module AwesomePrint
|
|
52
44
|
def awesome_self(object, type)
|
53
45
|
if @options[:raw] && object.instance_variables.any?
|
54
46
|
awesome_object(object)
|
55
|
-
elsif hash = convert_to_hash(object)
|
47
|
+
elsif (hash = convert_to_hash(object))
|
56
48
|
awesome_hash(hash)
|
57
49
|
else
|
58
50
|
awesome_simple(object.inspect.to_s, type, @inspector)
|
@@ -60,7 +52,7 @@ module AwesomePrint
|
|
60
52
|
end
|
61
53
|
|
62
54
|
def awesome_bigdecimal(n)
|
63
|
-
o = n.to_s(
|
55
|
+
o = n.to_s('F')
|
64
56
|
type = :bigdecimal
|
65
57
|
awesome_simple(o, type, @inspector)
|
66
58
|
end
|
@@ -71,7 +63,7 @@ module AwesomePrint
|
|
71
63
|
awesome_simple(o, type, @inspector)
|
72
64
|
end
|
73
65
|
|
74
|
-
def awesome_simple(o, type, inspector)
|
66
|
+
def awesome_simple(o, type, inspector = @inspector)
|
75
67
|
AwesomePrint::Formatters::SimpleFormatter.new(o, type, inspector).format
|
76
68
|
end
|
77
69
|
|
@@ -88,15 +80,15 @@ module AwesomePrint
|
|
88
80
|
end
|
89
81
|
|
90
82
|
def awesome_object(o)
|
91
|
-
Formatters::ObjectFormatter.new(o,
|
83
|
+
Formatters::ObjectFormatter.new(o, @inspector).format
|
92
84
|
end
|
93
85
|
|
94
86
|
def awesome_struct(s)
|
95
|
-
Formatters::
|
87
|
+
Formatters::StructFormatter.new(s, @inspector).format
|
96
88
|
end
|
97
89
|
|
98
90
|
def awesome_method(m)
|
99
|
-
Formatters::MethodFormatter.new(m, @inspector).format
|
91
|
+
Formatters::MethodFormatter.new(m, @inspector).format
|
100
92
|
end
|
101
93
|
alias :awesome_unboundmethod :awesome_method
|
102
94
|
|
@@ -115,7 +107,7 @@ module AwesomePrint
|
|
115
107
|
# Utility methods.
|
116
108
|
#------------------------------------------------------------------------------
|
117
109
|
def convert_to_hash(object)
|
118
|
-
if !
|
110
|
+
if !object.respond_to?(:to_hash)
|
119
111
|
return nil
|
120
112
|
end
|
121
113
|
|
@@ -124,7 +116,7 @@ module AwesomePrint
|
|
124
116
|
end
|
125
117
|
|
126
118
|
hash = object.to_hash
|
127
|
-
if !
|
119
|
+
if !hash.respond_to?(:keys) || !hash.respond_to?('[]')
|
128
120
|
return nil
|
129
121
|
end
|
130
122
|
|