awesome_print 1.8.0 → 1.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/Appraisals +43 -44
  4. data/CHANGELOG.md +20 -2
  5. data/CONTRIBUTING.md +1 -0
  6. data/LICENSE +16 -19
  7. data/README.md +43 -8
  8. data/awesome_print.gemspec +33 -0
  9. data/init.rb +1 -0
  10. data/lib/awesome_print/custom_defaults.rb +1 -1
  11. data/lib/awesome_print/ext/active_record.rb +26 -1
  12. data/lib/awesome_print/formatter.rb +20 -12
  13. data/lib/awesome_print/formatters/array_formatter.rb +2 -2
  14. data/lib/awesome_print/formatters/base_formatter.rb +3 -3
  15. data/lib/awesome_print/formatters/hash_formatter.rb +2 -2
  16. data/lib/awesome_print/formatters/object_formatter.rb +5 -2
  17. data/lib/awesome_print/inspector.rb +6 -3
  18. data/lib/awesome_print/version.rb +1 -1
  19. data/spec/ext/action_view_spec.rb +5 -2
  20. data/spec/ext/active_record_spec.rb +57 -49
  21. data/spec/ext/active_support_spec.rb +5 -1
  22. data/spec/ext/mongoid_spec.rb +2 -39
  23. data/spec/formats_spec.rb +4 -4
  24. data/spec/methods_spec.rb +10 -2
  25. data/spec/misc_spec.rb +25 -18
  26. data/spec/objects_spec.rb +49 -0
  27. data/spec/spec_helper.rb +4 -2
  28. data/spec/support/active_record_data/5_1_diana.txt +104 -0
  29. data/spec/support/active_record_data/5_1_multi.txt +210 -0
  30. data/spec/support/active_record_data/5_2_diana.txt +104 -0
  31. data/spec/support/active_record_data/5_2_multi.txt +210 -0
  32. data/spec/support/active_record_data/6_0_diana.txt +104 -0
  33. data/spec/support/active_record_data/6_0_multi.txt +210 -0
  34. data/spec/support/active_record_data/6_1_diana.txt +109 -0
  35. data/spec/support/active_record_data/6_1_multi.txt +220 -0
  36. data/spec/support/mongoid_versions.rb +10 -6
  37. data/spec/support/rails_versions.rb +20 -0
  38. metadata +45 -56
@@ -11,7 +11,7 @@ module AwesomePrint
11
11
 
12
12
  attr_reader :inspector, :options
13
13
 
14
- CORE = [:array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod]
14
+ CORE_FORMATTERS = [:array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod]
15
15
 
16
16
  def initialize(inspector)
17
17
  @inspector = inspector
@@ -34,7 +34,7 @@ module AwesomePrint
34
34
  # directory for custom formatters that ship with awesome_print.
35
35
  #------------------------------------------------------------------------------
36
36
  def cast(object, type)
37
- CORE.grep(type)[0] || :self
37
+ CORE_FORMATTERS.include?(type) ? type : :self
38
38
  end
39
39
 
40
40
  private
@@ -106,21 +106,29 @@ module AwesomePrint
106
106
 
107
107
  # Utility methods.
108
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
+
109
119
  def convert_to_hash(object)
110
- if !object.respond_to?(:to_hash)
111
- return nil
112
- end
120
+ return nil if has_method_accessor?(object)
121
+ return nil if !object.respond_to?(:to_hash) || object.method(:to_hash).arity != 0
113
122
 
114
- if object.method(:to_hash).arity != 0
115
- return nil
116
- end
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?
117
127
 
118
128
  hash = object.to_hash
119
- if !hash.respond_to?(:keys) || !hash.respond_to?('[]')
120
- return nil
121
- end
129
+ return nil if !hash.respond_to?(:keys) || !hash.respond_to?(:[])
122
130
 
123
- return hash
131
+ hash
124
132
  end
125
133
  end
126
134
  end
@@ -12,7 +12,7 @@ module AwesomePrint
12
12
  end
13
13
 
14
14
  def format
15
- if array.empty?
15
+ if array.length.zero?
16
16
  '[]'
17
17
  elsif methods_array?
18
18
  methods_array
@@ -31,7 +31,7 @@ module AwesomePrint
31
31
  if options[:multiline]
32
32
  multiline_array
33
33
  else
34
- '[ ' << array.map { |item| inspector.awesome(item) }.join(', ') << ' ]'
34
+ "[ #{array.map { |item| inspector.awesome(item) }.join(', ')} ]"
35
35
  end
36
36
  end
37
37
 
@@ -90,7 +90,7 @@ module AwesomePrint
90
90
  # #<Method: User(id: integer, username: string)(ActiveRecord::Base).current>
91
91
  # #<UnboundMethod: Hello#world>
92
92
  #
93
- if method.to_s =~ /(Unbound)*Method: (.*)[#\.]/
93
+ if method.to_s =~ /(Unbound)*Method: (.*?)[#\.]/
94
94
  unbound = $1 && '(unbound)'
95
95
  klass = $2
96
96
  if klass && klass =~ /(\(\w+:\s.*?\))/ # Is this ActiveRecord-style class?
@@ -109,8 +109,8 @@ module AwesomePrint
109
109
  inspector.current_indentation
110
110
  end
111
111
 
112
- def indented
113
- inspector.increase_indentation(&Proc.new)
112
+ def indented(&block)
113
+ inspector.increase_indentation(&block)
114
114
  end
115
115
 
116
116
  def indent
@@ -32,7 +32,7 @@ module AwesomePrint
32
32
  end
33
33
 
34
34
  def multiline_hash
35
- "{\n" << printable_hash.join(",\n") << "\n#{outdent}}"
35
+ ["{\n", printable_hash.join(",\n"), "\n#{outdent}}"].join
36
36
  end
37
37
 
38
38
  def simple_hash
@@ -73,7 +73,7 @@ module AwesomePrint
73
73
 
74
74
  keys.map! do |key|
75
75
  plain_single_line do
76
- [inspector.awesome(key), hash[key]]
76
+ [String.new(inspector.awesome(key)), hash[key]]
77
77
  end
78
78
  end
79
79
  end
@@ -22,7 +22,7 @@ module AwesomePrint
22
22
  object.respond_to?(property) ? :reader : nil
23
23
  end
24
24
  if accessor
25
- ["attr_#{accessor} :#{property}", var]
25
+ [String.new("attr_#{accessor} :#{property}"), var]
26
26
  else
27
27
  [var.to_s, var]
28
28
  end
@@ -60,7 +60,10 @@ module AwesomePrint
60
60
  end
61
61
 
62
62
  def awesome_instance
63
- "#{object.class}:0x%08x" % (object.__id__ * 2)
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
64
67
  end
65
68
 
66
69
  def left_aligned
@@ -23,6 +23,8 @@ module AwesomePrint
23
23
  sort_vars: true, # Sort instance variables.
24
24
  limit: false, # Limit arrays & hashes. Accepts bool or int.
25
25
  ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output.
26
+ class_name: :class, # Method used to get Instance class name.
27
+ object_id: true, # Show object_id.
26
28
  color: {
27
29
  args: :pale,
28
30
  array: :white,
@@ -31,6 +33,7 @@ module AwesomePrint
31
33
  date: :greenish,
32
34
  falseclass: :red,
33
35
  fixnum: :blue,
36
+ integer: :blue,
34
37
  float: :blue,
35
38
  hash: :pale,
36
39
  keyword: :cyan,
@@ -59,8 +62,8 @@ module AwesomePrint
59
62
  indentator.indentation
60
63
  end
61
64
 
62
- def increase_indentation
63
- indentator.indent(&Proc.new)
65
+ def increase_indentation(&block)
66
+ indentator.indent(&block)
64
67
  end
65
68
 
66
69
  # Dispatcher that detects data nesting and invokes object-aware formatter.
@@ -160,7 +163,7 @@ module AwesomePrint
160
163
  load_dotfile
161
164
  merge_options!(AwesomePrint.defaults) if AwesomePrint.defaults.is_a?(Hash)
162
165
  rescue => e
163
- $stderr.puts "Could not load #{dotfile}: #{e}"
166
+ $stderr.puts "Could not load '.aprc' from ENV['HOME']: #{e}"
164
167
  end
165
168
  end
166
169
  end
@@ -5,6 +5,6 @@
5
5
  #------------------------------------------------------------------------------
6
6
  module AwesomePrint
7
7
  def self.version
8
- '1.8.0'
8
+ '1.9.2'
9
9
  end
10
10
  end
@@ -1,9 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe 'AwesomePrint ActionView extensions', skip: -> { !ExtVerifier.has_rails? }.call do
4
-
5
4
  before do
6
- @view = ActionView::Base.new
5
+ @view = if rails_6_1?
6
+ ActionView::Base.new(ActionView::LookupContext.new([]), {}, {})
7
+ else
8
+ ActionView::Base.new
9
+ end
7
10
  end
8
11
 
9
12
  it "uses HTML and adds 'debug_dump' class to plain <pre> tag" do
@@ -21,11 +21,11 @@ RSpec.describe 'AwesomePrint/ActiveRecord', skip: -> { !ExtVerifier.has_rails? }
21
21
  :rank => 1
22
22
  }
23
23
  EOS
24
- if RUBY_VERSION < '1.9'
25
- str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992')
26
- else
27
- str.sub!('?', '1992-10-10 12:30:00 UTC')
28
- end
24
+
25
+ expect(RUBY_VERSION).to be >= '2.5'
26
+
27
+ str.sub!('?', '1992-10-10 12:30:00 UTC')
28
+
29
29
  expect(out).to be_similar_to(str)
30
30
  end
31
31
 
@@ -49,13 +49,10 @@ RSpec.describe 'AwesomePrint/ActiveRecord', skip: -> { !ExtVerifier.has_rails? }
49
49
  }
50
50
  ]
51
51
  EOS
52
- if RUBY_VERSION < '1.9'
53
- str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992')
54
- str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003')
55
- else
56
- str.sub!('??', '1992-10-10 12:30:00 UTC')
57
- str.sub!('?!', '2003-05-26 14:15:00 UTC')
58
- end
52
+
53
+ str.sub!('??', '1992-10-10 12:30:00 UTC')
54
+ str.sub!('?!', '2003-05-26 14:15:00 UTC')
55
+
59
56
  expect(out).to be_similar_to(str)
60
57
  end
61
58
 
@@ -81,13 +78,10 @@ RSpec.describe 'AwesomePrint/ActiveRecord', skip: -> { !ExtVerifier.has_rails? }
81
78
  }
82
79
  ]
83
80
  EOS
84
- if RUBY_VERSION < '1.9'
85
- str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992')
86
- str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003')
87
- else
88
- str.sub!('??', '1992-10-10 12:30:00 UTC')
89
- str.sub!('?!', '2003-05-26 14:15:00 UTC')
90
- end
81
+
82
+ str.sub!('??', '1992-10-10 12:30:00 UTC')
83
+ str.sub!('?!', '2003-05-26 14:15:00 UTC')
84
+
91
85
  expect(out).to be_similar_to(str)
92
86
  end
93
87
  end
@@ -126,24 +120,24 @@ EOS
126
120
  out = @ap.awesome(@diana)
127
121
 
128
122
  raw_object_string =
129
- if activerecord_5_0?
123
+ if activerecord_6_1?
124
+ ActiveRecordData.raw_6_1_diana
125
+ elsif activerecord_6_0?
126
+ ActiveRecordData.raw_6_0_diana
127
+ elsif activerecord_5_2?
128
+ ActiveRecordData.raw_5_2_diana
129
+ elsif activerecord_5_1?
130
+ ActiveRecordData.raw_5_1_diana
131
+ elsif activerecord_5_0?
130
132
  ActiveRecordData.raw_5_0_diana
131
133
  elsif activerecord_4_2?
132
- if RUBY_VERSION > '1.9.3'
133
- ActiveRecordData.raw_4_2_diana
134
- else
135
- ActiveRecordData.raw_4_2_diana_legacy
136
- end
134
+ ActiveRecordData.raw_4_2_diana
137
135
  elsif activerecord_4_1?
138
136
  ActiveRecordData.raw_4_1_diana
139
137
  elsif activerecord_4_0?
140
138
  ActiveRecordData.raw_4_0_diana
141
139
  elsif activerecord_3_2?
142
- if RUBY_VERSION > '1.9.3'
143
- ActiveRecordData.raw_3_2_diana
144
- else
145
- ActiveRecordData.raw_3_2_diana_legacy
146
- end
140
+ ActiveRecordData.raw_3_2_diana
147
141
  end
148
142
  raw_object_string.sub!('?', '1992-10-10 12:30:00')
149
143
  expect(out).to be_similar_to(raw_object_string)
@@ -153,24 +147,24 @@ EOS
153
147
  out = @ap.awesome([@diana, @laura])
154
148
 
155
149
  raw_object_string =
156
- if activerecord_5_0?
150
+ if activerecord_6_1?
151
+ ActiveRecordData.raw_6_1_multi
152
+ elsif activerecord_6_0?
153
+ ActiveRecordData.raw_6_0_multi
154
+ elsif activerecord_5_2?
155
+ ActiveRecordData.raw_5_2_multi
156
+ elsif activerecord_5_1?
157
+ ActiveRecordData.raw_5_1_multi
158
+ elsif activerecord_5_0?
157
159
  ActiveRecordData.raw_5_0_multi
158
160
  elsif activerecord_4_2?
159
- if RUBY_VERSION > '1.9.3'
160
- ActiveRecordData.raw_4_2_multi
161
- else
162
- ActiveRecordData.raw_4_2_multi_legacy
163
- end
161
+ ActiveRecordData.raw_4_2_multi
164
162
  elsif activerecord_4_1?
165
163
  ActiveRecordData.raw_4_1_multi
166
164
  elsif activerecord_4_0?
167
165
  ActiveRecordData.raw_4_0_multi
168
166
  elsif activerecord_3_2?
169
- if RUBY_VERSION > '1.9.3'
170
- ActiveRecordData.raw_3_2_multi
171
- else
172
- ActiveRecordData.raw_3_2_multi_legacy
173
- end
167
+ ActiveRecordData.raw_3_2_multi
174
168
  end
175
169
  raw_object_string.sub!('?', '1992-10-10 12:30:00')
176
170
  raw_object_string.sub!('?', '2003-05-26 14:15:00')
@@ -224,29 +218,43 @@ class SubUser < User {
224
218
  # spec 1
225
219
  out = @ap.awesome(User.methods.grep(/first/))
226
220
 
227
- if ActiveRecord::VERSION::STRING >= '3.2'
228
- if RUBY_VERSION >= '1.9'
229
- expect(out).to match(/\sfirst\(\*args,\s&block\)\s+Class \(ActiveRecord::Querying\)/)
221
+ if RUBY_VERSION >= '3.0.0'
222
+ expect(out).to match(/\sfirst\(\*\*,\s&&\)/)
223
+ elsif RUBY_VERSION >= '2.7.0'
224
+ if ActiveRecord::VERSION::STRING >= '3.2'
225
+ expect(out).to match(/\sfirst\(\*\*,\s&&\)\s+User/)
230
226
  else
231
- expect(out).to match(/\sfirst\(\*arg1\)\s+Class \(ActiveRecord::Querying\)/)
227
+ expect(out).to match(/\sfirst\(\*\*,\s&&\)\s+User \(ActiveRecord::Base\)/)
232
228
  end
233
229
  else
234
- expect(out).to match(/\sfirst\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/)
230
+ if ActiveRecord::VERSION::STRING >= '3.2'
231
+ expect(out).to match(/\sfirst\(\*arg.*?\)\s+User/)
232
+ else
233
+ expect(out).to match(/\sfirst\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/)
234
+ end
235
235
  end
236
236
 
237
237
  # spec 2
238
238
  out = @ap.awesome(User.methods.grep(/primary_key/))
239
- expect(out).to match(/\sprimary_key\(.*?\)\s+Class \(ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods\)/)
239
+ if RUBY_VERSION >= '3.0.0'
240
+ expect(out).to match(/\sprimary_key\(.*?\)/)
241
+ else
242
+ expect(out).to match(/\sprimary_key\(.*?\)\s+User/)
243
+ end
240
244
 
241
245
  # spec 3
242
246
  out = @ap.awesome(User.methods.grep(/validate/))
243
-
244
247
  if ActiveRecord::VERSION::MAJOR < 3
245
248
  expect(out).to match(/\svalidate\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/)
246
249
  else
247
- expect(out).to match(/\svalidate\(\*arg.*?\)\s+Class \(ActiveModel::Validations::ClassMethods\)/)
250
+ if RUBY_VERSION >= '3.0.0'
251
+ expect(out).to match(/\svalidate\(\*arg.*?\)/)
252
+ else
253
+ expect(out).to match(/\svalidate\(\*arg.*?\)\s+User/)
254
+ end
248
255
  end
249
256
 
250
257
  end
251
258
  end
252
259
  end
260
+
@@ -8,7 +8,11 @@ RSpec.describe 'AwesomePrint::ActiveSupport', skip: -> { !ExtVerifier.has_rails?
8
8
  it 'should format ActiveSupport::TimeWithZone as regular Time' do
9
9
  Time.zone = 'Eastern Time (US & Canada)'
10
10
  time = Time.utc(2007, 2, 10, 20, 30, 45).in_time_zone
11
- expect(@ap.send(:awesome, time)).to eq("\e[0;32mSat, 10 Feb 2007 15:30:45 EST -05:00\e[0m")
11
+ if activerecord_6_1?
12
+ expect(@ap.send(:awesome, time)).to eq("\e[0;32mSat, 10 Feb 2007 15:30:45.000000000 EST -05:00\e[0m")
13
+ else
14
+ expect(@ap.send(:awesome, time)).to eq("\e[0;32mSat, 10 Feb 2007 15:30:45 EST -05:00\e[0m")
15
+ end
12
16
  end
13
17
 
14
18
  it 'should format HashWithIndifferentAccess as regular Hash' do
@@ -38,32 +38,13 @@ RSpec.describe 'AwesomePrint/Mongoid', skip: -> { !ExtVerifier.has_mongoid? }.ca
38
38
  end
39
39
 
40
40
  it 'should print the class' do
41
- class_spec = if mongoid_3_0?
42
- <<-EOS.strip
43
- class MongoUser < Object {
44
- :_id => :"moped/bson/object_id",
45
- :_type => :string,
46
- :first_name => :string,
47
- :last_name => :string
48
- }
49
- EOS
50
- elsif mongoid_3_1?
51
- <<-EOS.strip
52
- class MongoUser < Object {
53
- :_id => :"moped/bson/object_id",
54
- :first_name => :string,
55
- :last_name => :string
56
- }
57
- EOS
58
- elsif mongoid_4_0?
59
- <<-EOS.strip
41
+ class_spec = <<-EOS.strip
60
42
  class MongoUser < Object {
61
43
  :_id => :"bson/object_id",
62
44
  :first_name => :string,
63
45
  :last_name => :string
64
46
  }
65
47
  EOS
66
- end
67
48
 
68
49
  expect(@ap.send(:awesome, MongoUser)).to eq class_spec
69
50
  end
@@ -74,30 +55,12 @@ class MongoUser < Object {
74
55
  field :last_attribute
75
56
  end
76
57
 
77
- class_spec = if mongoid_3_0?
78
- <<-EOS.strip
79
- class Chamelion < Object {
80
- :_id => :"moped/bson/object_id",
81
- :_type => :string,
82
- :last_attribute => :object
83
- }
84
- EOS
85
- elsif mongoid_3_1?
86
- <<-EOS.strip
87
- class Chamelion < Object {
88
- :_id => :"moped/bson/object_id",
89
- :last_attribute => :object
90
- }
91
- EOS
92
- elsif mongoid_4_0?
93
- <<-EOS.strip
58
+ class_spec = <<-EOS.strip
94
59
  class Chamelion < Object {
95
60
  :_id => :"bson/object_id",
96
61
  :last_attribute => :object
97
62
  }
98
63
  EOS
99
- end
100
-
101
64
 
102
65
  expect(@ap.send(:awesome, Chamelion)).to eq class_spec
103
66
  end