awesome_print 1.7.0 → 1.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +4 -0
- data/Appraisals +44 -34
- data/CHANGELOG.md +28 -0
- data/CONTRIBUTING.md +2 -1
- data/Gemfile.lock +25 -33
- data/LICENSE +16 -19
- data/README.md +88 -44
- data/Rakefile +5 -5
- data/awesome_print.gemspec +33 -0
- data/init.rb +1 -0
- data/lib/ap.rb +2 -2
- 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 +45 -12
- 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 +28 -28
- data/lib/awesome_print/formatters/array_formatter.rb +108 -42
- data/lib/awesome_print/formatters/base_formatter.rb +16 -14
- 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 +13 -15
- data/lib/awesome_print/formatters/struct_formatter.rb +71 -0
- data/lib/awesome_print/formatters.rb +15 -0
- data/lib/awesome_print/inspector.rb +83 -96
- data/lib/awesome_print/version.rb +2 -2
- data/lib/awesome_print.rb +18 -17
- 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 +21 -0
- data/spec/ext/active_record_spec.rb +260 -0
- data/spec/ext/active_support_spec.rb +30 -0
- data/spec/ext/mongo_mapper_spec.rb +261 -0
- data/spec/ext/mongoid_spec.rb +67 -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 +125 -129
- data/spec/misc_spec.rb +95 -92
- data/spec/objects_spec.rb +119 -28
- data/spec/spec_helper.rb +29 -10
- 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/active_record_data/5_1_diana.txt +104 -0
- data/spec/support/active_record_data/5_1_multi.txt +210 -0
- data/spec/support/active_record_data/5_2_diana.txt +104 -0
- data/spec/support/active_record_data/5_2_multi.txt +210 -0
- data/spec/support/active_record_data/6_0_diana.txt +104 -0
- data/spec/support/active_record_data/6_0_multi.txt +210 -0
- data/spec/support/active_record_data/6_1_diana.txt +109 -0
- data/spec/support/active_record_data/6_1_multi.txt +220 -0
- data/spec/support/active_record_data.rb +20 -0
- data/spec/support/ext_verifier.rb +42 -0
- data/spec/support/mongoid_versions.rb +26 -0
- data/spec/support/rails_versions.rb +55 -0
- metadata +90 -26
@@ -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
|
-
|
14
|
+
CORE_FORMATTERS = [:array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod]
|
23
15
|
|
24
16
|
def initialize(inspector)
|
25
17
|
@inspector = inspector
|
@@ -42,7 +34,7 @@ module AwesomePrint
|
|
42
34
|
# directory for custom formatters that ship with awesome_print.
|
43
35
|
#------------------------------------------------------------------------------
|
44
36
|
def cast(object, type)
|
45
|
-
|
37
|
+
CORE_FORMATTERS.include?(type) ? type : :self
|
46
38
|
end
|
47
39
|
|
48
40
|
private
|
@@ -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
|
|
@@ -114,21 +106,29 @@ module AwesomePrint
|
|
114
106
|
|
115
107
|
# Utility methods.
|
116
108
|
#------------------------------------------------------------------------------
|
109
|
+
|
110
|
+
# A class (ex. `Net::HTTP.Get`) might have `attr_reader :method` accessor
|
111
|
+
# which causes `object.method(:to_hash)` throw `ArgumentError (wrong number
|
112
|
+
# of arguments (given 1, expected 0))`. The following tries to avoid that.
|
113
|
+
def has_method_accessor?(object)
|
114
|
+
!object.method(:method)
|
115
|
+
rescue ArgumentError
|
116
|
+
true
|
117
|
+
end
|
118
|
+
|
117
119
|
def convert_to_hash(object)
|
118
|
-
if
|
119
|
-
|
120
|
-
end
|
120
|
+
return nil if has_method_accessor?(object)
|
121
|
+
return nil if !object.respond_to?(:to_hash) || object.method(:to_hash).arity != 0
|
121
122
|
|
122
|
-
if
|
123
|
-
|
124
|
-
|
123
|
+
# ActionController::Parameters will raise if they are not yet permitted
|
124
|
+
# and we try to convert to hash.
|
125
|
+
# https://api.rubyonrails.org/classes/ActionController/Parameters.html
|
126
|
+
return nil if object.respond_to?(:permitted?) && !object.permitted?
|
125
127
|
|
126
128
|
hash = object.to_hash
|
127
|
-
if !
|
128
|
-
return nil
|
129
|
-
end
|
129
|
+
return nil if !hash.respond_to?(:keys) || !hash.respond_to?(:[])
|
130
130
|
|
131
|
-
|
131
|
+
hash
|
132
132
|
end
|
133
133
|
end
|
134
134
|
end
|
@@ -3,7 +3,6 @@ require_relative 'base_formatter'
|
|
3
3
|
module AwesomePrint
|
4
4
|
module Formatters
|
5
5
|
class ArrayFormatter < BaseFormatter
|
6
|
-
|
7
6
|
attr_reader :array, :inspector, :options
|
8
7
|
|
9
8
|
def initialize(array, inspector)
|
@@ -13,60 +12,127 @@ module AwesomePrint
|
|
13
12
|
end
|
14
13
|
|
15
14
|
def format
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
methods_array
|
20
|
-
elsif options[:multiline]
|
21
|
-
width = (array.size - 1).to_s.size
|
22
|
-
|
23
|
-
data = array.inject([]) do |arr, item|
|
24
|
-
index = indent
|
25
|
-
index << colorize("[#{arr.size.to_s.rjust(width)}] ", :array) if options[:index]
|
26
|
-
indented do
|
27
|
-
arr << (index << inspector.awesome(item))
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
data = limited(data, width) if should_be_limited?
|
32
|
-
"[\n" << data.join(",\n") << "\n#{outdent}]"
|
15
|
+
if array.length.zero?
|
16
|
+
'[]'
|
17
|
+
elsif methods_array?
|
18
|
+
methods_array
|
33
19
|
else
|
34
|
-
|
20
|
+
simple_array
|
35
21
|
end
|
36
22
|
end
|
37
23
|
|
38
24
|
private
|
39
25
|
|
40
|
-
def methods_array
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
26
|
+
def methods_array?
|
27
|
+
array.instance_variable_defined?('@__awesome_methods__')
|
28
|
+
end
|
29
|
+
|
30
|
+
def simple_array
|
31
|
+
if options[:multiline]
|
32
|
+
multiline_array
|
33
|
+
else
|
34
|
+
"[ #{array.map { |item| inspector.awesome(item) }.join(', ')} ]"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def multiline_array
|
39
|
+
data = unless should_be_limited?
|
40
|
+
generate_printable_array
|
41
|
+
else
|
42
|
+
limited(generate_printable_array, width(array))
|
43
|
+
end
|
44
|
+
|
45
|
+
%Q([\n#{data.join(",\n")}\n#{outdent}])
|
46
|
+
end
|
47
|
+
|
48
|
+
def generate_printable_array
|
49
|
+
array.map.with_index do |item, index|
|
50
|
+
array_prefix(index, width(array)).tap do |temp|
|
51
|
+
indented { temp << inspector.awesome(item) }
|
53
52
|
end
|
54
|
-
tuple || [ name.to_s, '(?)', '?' ] # Return WTF default if all the above fails.
|
55
53
|
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def array_prefix(iteration, width)
|
57
|
+
generic_prefix(iteration, width)
|
58
|
+
end
|
59
|
+
|
60
|
+
def methods_array
|
61
|
+
array.map!(&:to_s).sort!
|
56
62
|
|
57
|
-
|
58
|
-
name_width = tuples.map { |item| item[0].size }.max || 0
|
59
|
-
args_width = tuples.map { |item| item[1].size }.max || 0
|
63
|
+
data = generate_printable_tuples.join("\n")
|
60
64
|
|
61
|
-
data
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
65
|
+
"[\n#{data}\n#{outdent}]"
|
66
|
+
end
|
67
|
+
|
68
|
+
def generate_printable_tuples
|
69
|
+
tuples.map.with_index do |item, index|
|
70
|
+
tuple_prefix(index, width(tuples)).tap do |temp|
|
71
|
+
indented { temp << tuple_template(item) }
|
66
72
|
end
|
67
73
|
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def tuple_template(item)
|
77
|
+
name_width, args_width = name_and_args_width
|
78
|
+
|
79
|
+
[
|
80
|
+
colorize(item[0].rjust(name_width), :method),
|
81
|
+
colorize(item[1].ljust(args_width), :args),
|
82
|
+
' ',
|
83
|
+
colorize(item[2], :class)
|
84
|
+
].join
|
85
|
+
end
|
86
|
+
|
87
|
+
def tuples
|
88
|
+
@tuples ||= array.map { |name| generate_tuple(name) }
|
89
|
+
end
|
90
|
+
|
91
|
+
def name_and_args_width
|
92
|
+
name_and_args = tuples.transpose
|
93
|
+
|
94
|
+
return name_and_args[0].map(&:size).max, name_and_args[1].map(&:size).max
|
95
|
+
end
|
96
|
+
|
97
|
+
def tuple_prefix(iteration, width)
|
98
|
+
generic_prefix(iteration, width, ' ')
|
99
|
+
end
|
100
|
+
|
101
|
+
def generate_tuple(name)
|
102
|
+
meth = case name
|
103
|
+
when Symbol, String
|
104
|
+
find_method(name)
|
105
|
+
end
|
106
|
+
|
107
|
+
meth ? method_tuple(meth) : [name.to_s, '(?)', '?']
|
108
|
+
end
|
109
|
+
|
110
|
+
def find_method(name)
|
111
|
+
object = array.instance_variable_get('@__awesome_methods__')
|
112
|
+
|
113
|
+
meth = begin
|
114
|
+
object.method(name)
|
115
|
+
rescue NameError, ArgumentError
|
116
|
+
nil
|
117
|
+
end
|
118
|
+
|
119
|
+
meth || begin
|
120
|
+
object.instance_method(name)
|
121
|
+
rescue NameError
|
122
|
+
nil
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def generic_prefix(iteration, width, padding='')
|
127
|
+
if options[:index]
|
128
|
+
indent + colorize("[#{iteration.to_s.rjust(width)}] ", :array)
|
129
|
+
else
|
130
|
+
indent + padding
|
131
|
+
end
|
132
|
+
end
|
68
133
|
|
69
|
-
|
134
|
+
def width(items)
|
135
|
+
(items.size - 1).to_s.size
|
70
136
|
end
|
71
137
|
end
|
72
138
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative '../colorize'
|
2
2
|
|
3
3
|
module AwesomePrint
|
4
4
|
module Formatters
|
@@ -28,7 +28,7 @@ module AwesomePrint
|
|
28
28
|
# ]
|
29
29
|
#------------------------------------------------------------------------------
|
30
30
|
def should_be_limited?
|
31
|
-
options[:limit] or (options[:limit].is_a?(
|
31
|
+
options[:limit] or (options[:limit].is_a?(Integer) and options[:limit] > 0)
|
32
32
|
end
|
33
33
|
|
34
34
|
def get_limit_size
|
@@ -50,13 +50,13 @@ module AwesomePrint
|
|
50
50
|
tail = head - (limit - 1) % 2
|
51
51
|
|
52
52
|
# Add the proper elements to the temp array and format the separator.
|
53
|
-
temp = data[0, head] + [
|
53
|
+
temp = data[0, head] + [nil] + data[-tail, tail]
|
54
54
|
|
55
|
-
if is_hash
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
temp[head] = if is_hash
|
56
|
+
"#{indent}#{data[head].strip} .. #{data[data.length - tail - 1].strip}"
|
57
|
+
else
|
58
|
+
"#{indent}[#{head.to_s.rjust(width)}] .. [#{data.length - tail - 1}]"
|
59
|
+
end
|
60
60
|
|
61
61
|
temp
|
62
62
|
end
|
@@ -65,7 +65,8 @@ module AwesomePrint
|
|
65
65
|
|
66
66
|
def method_tuple(method)
|
67
67
|
if method.respond_to?(:parameters) # Ruby 1.9.2+
|
68
|
-
# See http://
|
68
|
+
# See http://readruby.chengguangnan.com/methods#method-objects-parameters
|
69
|
+
# (mirror: http://archive.is/XguCA#selection-3381.1-3381.11)
|
69
70
|
args = method.parameters.inject([]) do |arr, (type, name)|
|
70
71
|
name ||= (type == :block ? 'block' : "arg#{arr.size + 1}")
|
71
72
|
arr << case type
|
@@ -89,15 +90,16 @@ module AwesomePrint
|
|
89
90
|
# #<Method: User(id: integer, username: string)(ActiveRecord::Base).current>
|
90
91
|
# #<UnboundMethod: Hello#world>
|
91
92
|
#
|
92
|
-
if method.to_s =~ /(Unbound)*Method: (
|
93
|
-
unbound
|
93
|
+
if method.to_s =~ /(Unbound)*Method: (.*?)[#\.]/
|
94
|
+
unbound = $1 && '(unbound)'
|
95
|
+
klass = $2
|
94
96
|
if klass && klass =~ /(\(\w+:\s.*?\))/ # Is this ActiveRecord-style class?
|
95
97
|
klass.sub!($1, '') # Yes, strip the fields leaving class name only.
|
96
98
|
end
|
97
99
|
owner = "#{klass}#{unbound}".gsub('(', ' (')
|
98
100
|
end
|
99
101
|
|
100
|
-
[
|
102
|
+
[method.name.to_s, "(#{args.join(', ')})", owner.to_s]
|
101
103
|
end
|
102
104
|
|
103
105
|
#
|
@@ -107,8 +109,8 @@ module AwesomePrint
|
|
107
109
|
inspector.current_indentation
|
108
110
|
end
|
109
111
|
|
110
|
-
def indented
|
111
|
-
inspector.increase_indentation(&
|
112
|
+
def indented(&block)
|
113
|
+
inspector.increase_indentation(&block)
|
112
114
|
end
|
113
115
|
|
114
116
|
def indent
|
@@ -13,7 +13,8 @@ module AwesomePrint
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def format
|
16
|
-
|
16
|
+
superclass = klass.superclass
|
17
|
+
if superclass
|
17
18
|
colorize("#{klass.inspect} < #{superclass}", :class)
|
18
19
|
else
|
19
20
|
colorize(klass.inspect, :class)
|
@@ -3,7 +3,6 @@ require_relative 'base_formatter'
|
|
3
3
|
module AwesomePrint
|
4
4
|
module Formatters
|
5
5
|
class HashFormatter < BaseFormatter
|
6
|
-
|
7
6
|
attr_reader :hash, :inspector, :options
|
8
7
|
|
9
8
|
def initialize(hash, inspector)
|
@@ -13,41 +12,94 @@ module AwesomePrint
|
|
13
12
|
end
|
14
13
|
|
15
14
|
def format
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
[ inspector.awesome(key), hash[key] ]
|
23
|
-
end
|
15
|
+
if hash.empty?
|
16
|
+
empty_hash
|
17
|
+
elsif multiline_hash?
|
18
|
+
multiline_hash
|
19
|
+
else
|
20
|
+
simple_hash
|
24
21
|
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
def empty_hash
|
27
|
+
'{}'
|
28
|
+
end
|
29
|
+
|
30
|
+
def multiline_hash?
|
31
|
+
options[:multiline]
|
32
|
+
end
|
33
|
+
|
34
|
+
def multiline_hash
|
35
|
+
["{\n", printable_hash.join(",\n"), "\n#{outdent}}"].join
|
36
|
+
end
|
28
37
|
|
29
|
-
|
38
|
+
def simple_hash
|
39
|
+
"{ #{printable_hash.join(', ')} }"
|
40
|
+
end
|
41
|
+
|
42
|
+
def printable_hash
|
43
|
+
data = printable_keys
|
44
|
+
width = left_width(data)
|
45
|
+
|
46
|
+
data.map! do |key, value|
|
30
47
|
indented do
|
31
|
-
|
48
|
+
if options[:ruby19_syntax] && symbol?(key)
|
49
|
+
ruby19_syntax(key, value, width)
|
50
|
+
else
|
51
|
+
pre_ruby19_syntax(key, value, width)
|
52
|
+
end
|
32
53
|
end
|
33
54
|
end
|
34
55
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
56
|
+
should_be_limited? ? limited(data, width, hash: true) : data
|
57
|
+
end
|
58
|
+
|
59
|
+
def left_width(keys)
|
60
|
+
result = max_key_width(keys)
|
61
|
+
result += indentation if options[:indent] > 0
|
62
|
+
result
|
63
|
+
end
|
64
|
+
|
65
|
+
def max_key_width(keys)
|
66
|
+
keys.map { |key, _value| key.size }.max || 0
|
67
|
+
end
|
68
|
+
|
69
|
+
def printable_keys
|
70
|
+
keys = hash.keys
|
71
|
+
|
72
|
+
keys.sort! { |a, b| a.to_s <=> b.to_s } if options[:sort_keys]
|
73
|
+
|
74
|
+
keys.map! do |key|
|
75
|
+
plain_single_line do
|
76
|
+
[String.new(inspector.awesome(key)), hash[key]]
|
77
|
+
end
|
40
78
|
end
|
41
79
|
end
|
42
80
|
|
43
|
-
|
81
|
+
def symbol?(key)
|
82
|
+
key[0] == ':'
|
83
|
+
end
|
84
|
+
|
85
|
+
def ruby19_syntax(key, value, width)
|
86
|
+
key[0] = ''
|
87
|
+
align(key, width - 1) << colorize(': ', :hash) << inspector.awesome(value)
|
88
|
+
end
|
89
|
+
|
90
|
+
def pre_ruby19_syntax(key, value, width)
|
91
|
+
align(key, width) << colorize(' => ', :hash) << inspector.awesome(value)
|
92
|
+
end
|
44
93
|
|
45
94
|
def plain_single_line
|
46
|
-
plain
|
47
|
-
|
95
|
+
plain = options[:plain]
|
96
|
+
multiline = options[:multiline]
|
97
|
+
options[:plain] = true
|
98
|
+
options[:multiline] = false
|
48
99
|
yield
|
49
100
|
ensure
|
50
|
-
options[:plain]
|
101
|
+
options[:plain] = plain
|
102
|
+
options[:multiline] = multiline
|
51
103
|
end
|
52
104
|
end
|
53
105
|
end
|
@@ -6,9 +6,9 @@ module AwesomePrint
|
|
6
6
|
|
7
7
|
attr_reader :object, :variables, :inspector, :options
|
8
8
|
|
9
|
-
def initialize(object,
|
9
|
+
def initialize(object, inspector)
|
10
10
|
@object = object
|
11
|
-
@variables =
|
11
|
+
@variables = object.instance_variables
|
12
12
|
@inspector = inspector
|
13
13
|
@options = inspector.options
|
14
14
|
end
|
@@ -22,13 +22,13 @@ module AwesomePrint
|
|
22
22
|
object.respond_to?(property) ? :reader : nil
|
23
23
|
end
|
24
24
|
if accessor
|
25
|
-
[
|
25
|
+
[String.new("attr_#{accessor} :#{property}"), var]
|
26
26
|
else
|
27
|
-
[
|
27
|
+
[var.to_s, var]
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
data = vars.sort.map do |declaration, var|
|
31
|
+
data = (options[:sort_vars] ? vars.sort : vars).map do |declaration, var|
|
32
32
|
key = left_aligned do
|
33
33
|
align(declaration, declaration.size)
|
34
34
|
end
|
@@ -42,13 +42,7 @@ module AwesomePrint
|
|
42
42
|
end
|
43
43
|
|
44
44
|
indented do
|
45
|
-
|
46
|
-
object.instance_variable_get(var)
|
47
|
-
else
|
48
|
-
object.send(var) # Enables handling of Struct attributes
|
49
|
-
end
|
50
|
-
|
51
|
-
key << colorize(" = ", :hash) + inspector.awesome(var_contents)
|
45
|
+
key << colorize(' = ', :hash) + inspector.awesome(object.instance_variable_get(var))
|
52
46
|
end
|
53
47
|
end
|
54
48
|
|
@@ -59,18 +53,22 @@ module AwesomePrint
|
|
59
53
|
end
|
60
54
|
end
|
61
55
|
|
62
|
-
private
|
56
|
+
private
|
63
57
|
|
64
58
|
def valid_instance_var?(variable_name)
|
65
59
|
variable_name.to_s.start_with?('@')
|
66
60
|
end
|
67
61
|
|
68
62
|
def awesome_instance
|
69
|
-
|
63
|
+
str = String.new
|
64
|
+
str << object.send(options[:class_name]).to_s
|
65
|
+
str << ":0x%08x" % (object.__id__ * 2) if options[:object_id]
|
66
|
+
str
|
70
67
|
end
|
71
68
|
|
72
69
|
def left_aligned
|
73
|
-
current
|
70
|
+
current = options[:indent]
|
71
|
+
options[:indent] = 0
|
74
72
|
yield
|
75
73
|
ensure
|
76
74
|
options[:indent] = current
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require_relative 'base_formatter'
|
2
|
+
|
3
|
+
module AwesomePrint
|
4
|
+
module Formatters
|
5
|
+
class StructFormatter < BaseFormatter
|
6
|
+
|
7
|
+
attr_reader :struct, :variables, :inspector, :options
|
8
|
+
|
9
|
+
def initialize(struct, inspector)
|
10
|
+
@struct = struct
|
11
|
+
@variables = struct.members
|
12
|
+
@inspector = inspector
|
13
|
+
@options = inspector.options
|
14
|
+
end
|
15
|
+
|
16
|
+
def format
|
17
|
+
vars = variables.map do |var|
|
18
|
+
property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet.
|
19
|
+
accessor = if struct.respond_to?(:"#{property}=")
|
20
|
+
struct.respond_to?(property) ? :accessor : :writer
|
21
|
+
else
|
22
|
+
struct.respond_to?(property) ? :reader : nil
|
23
|
+
end
|
24
|
+
if accessor
|
25
|
+
["attr_#{accessor} :#{property}", var]
|
26
|
+
else
|
27
|
+
[var.to_s, var]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
data = vars.sort.map do |declaration, var|
|
32
|
+
key = left_aligned do
|
33
|
+
align(declaration, declaration.size)
|
34
|
+
end
|
35
|
+
|
36
|
+
unless options[:plain]
|
37
|
+
if key =~ /(@\w+)/
|
38
|
+
key.sub!($1, colorize($1, :variable))
|
39
|
+
else
|
40
|
+
key.sub!(/(attr_\w+)\s(\:\w+)/, "#{colorize('\\1', :keyword)} #{colorize('\\2', :method)}")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
indented do
|
45
|
+
key << colorize(' = ', :hash) + inspector.awesome(struct.send(var))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
if options[:multiline]
|
50
|
+
"#<#{awesome_instance}\n#{data.join(%Q/,\n/)}\n#{outdent}>"
|
51
|
+
else
|
52
|
+
"#<#{awesome_instance} #{data.join(', ')}>"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def awesome_instance
|
59
|
+
"#{struct.class.superclass}:#{struct.class}:0x%08x" % (struct.__id__ * 2)
|
60
|
+
end
|
61
|
+
|
62
|
+
def left_aligned
|
63
|
+
current = options[:indent]
|
64
|
+
options[:indent] = 0
|
65
|
+
yield
|
66
|
+
ensure
|
67
|
+
options[:indent] = current
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module AwesomePrint
|
2
|
+
module Formatters
|
3
|
+
require 'awesome_print/formatters/object_formatter'
|
4
|
+
require 'awesome_print/formatters/struct_formatter'
|
5
|
+
require 'awesome_print/formatters/hash_formatter'
|
6
|
+
require 'awesome_print/formatters/array_formatter'
|
7
|
+
require 'awesome_print/formatters/simple_formatter'
|
8
|
+
require 'awesome_print/formatters/method_formatter'
|
9
|
+
require 'awesome_print/formatters/class_formatter'
|
10
|
+
require 'awesome_print/formatters/dir_formatter'
|
11
|
+
require 'awesome_print/formatters/file_formatter'
|
12
|
+
require 'awesome_print/colorize'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|