amazing_print 1.5.0 → 1.8.1

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.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +79 -37
  3. data/README.md +11 -11
  4. data/lib/amazing_print/core_ext/awesome_method_array.rb +3 -3
  5. data/lib/amazing_print/custom_defaults.rb +7 -1
  6. data/lib/amazing_print/ext/active_record.rb +32 -2
  7. data/lib/amazing_print/ext/mongo_mapper.rb +2 -2
  8. data/lib/amazing_print/ext/mongoid.rb +14 -2
  9. data/lib/amazing_print/ext/nobrainer.rb +2 -2
  10. data/lib/amazing_print/ext_loader.rb +27 -0
  11. data/lib/amazing_print/formatter.rb +1 -1
  12. data/lib/amazing_print/formatters/array_formatter.rb +1 -1
  13. data/lib/amazing_print/formatters/base_formatter.rb +14 -5
  14. data/lib/amazing_print/formatters/hash_formatter.rb +28 -11
  15. data/lib/amazing_print/formatters/mswin_helper.rb +4 -4
  16. data/lib/amazing_print/formatters/object_formatter.rb +2 -2
  17. data/lib/amazing_print/formatters/struct_formatter.rb +2 -2
  18. data/lib/amazing_print/inspector.rb +8 -5
  19. data/lib/amazing_print/version.rb +1 -1
  20. data/lib/amazing_print.rb +2 -8
  21. metadata +9 -62
  22. data/.gitignore +0 -35
  23. data/Appraisals +0 -50
  24. data/Gemfile +0 -15
  25. data/Gemfile.lock +0 -83
  26. data/LICENSE +0 -22
  27. data/Rakefile +0 -25
  28. data/spec/active_record_helper.rb +0 -43
  29. data/spec/colors_spec.rb +0 -120
  30. data/spec/core_ext/logger_spec.rb +0 -70
  31. data/spec/ext/action_controller_spec.rb +0 -40
  32. data/spec/ext/action_view_spec.rb +0 -24
  33. data/spec/ext/active_model_spec.rb +0 -37
  34. data/spec/ext/active_record_spec.rb +0 -298
  35. data/spec/ext/active_support_spec.rb +0 -37
  36. data/spec/ext/mongo_mapper_spec.rb +0 -265
  37. data/spec/ext/mongoid_spec.rb +0 -72
  38. data/spec/ext/nobrainer_spec.rb +0 -64
  39. data/spec/ext/nokogiri_spec.rb +0 -52
  40. data/spec/ext/ostruct_spec.rb +0 -24
  41. data/spec/ext/ripple_spec.rb +0 -53
  42. data/spec/ext/sequel_spec.rb +0 -45
  43. data/spec/formats_spec.rb +0 -792
  44. data/spec/methods_spec.rb +0 -520
  45. data/spec/misc_spec.rb +0 -206
  46. data/spec/objects_spec.rb +0 -225
  47. data/spec/sequel_helper.rb +0 -18
  48. data/spec/spec_helper.rb +0 -112
  49. data/spec/support/active_record_data/3_2_diana.txt +0 -24
  50. data/spec/support/active_record_data/3_2_diana_legacy.txt +0 -24
  51. data/spec/support/active_record_data/3_2_multi.txt +0 -50
  52. data/spec/support/active_record_data/3_2_multi_legacy.txt +0 -50
  53. data/spec/support/active_record_data/4_0_diana.txt +0 -98
  54. data/spec/support/active_record_data/4_0_multi.txt +0 -198
  55. data/spec/support/active_record_data/4_1_diana.txt +0 -97
  56. data/spec/support/active_record_data/4_1_multi.txt +0 -196
  57. data/spec/support/active_record_data/4_2_diana.txt +0 -109
  58. data/spec/support/active_record_data/4_2_diana_legacy.txt +0 -109
  59. data/spec/support/active_record_data/4_2_multi.txt +0 -220
  60. data/spec/support/active_record_data/4_2_multi_legacy.txt +0 -220
  61. data/spec/support/active_record_data/5_0_diana.txt +0 -105
  62. data/spec/support/active_record_data/5_0_multi.txt +0 -212
  63. data/spec/support/active_record_data/5_1_diana.txt +0 -104
  64. data/spec/support/active_record_data/5_1_multi.txt +0 -210
  65. data/spec/support/active_record_data/5_2_diana.txt +0 -104
  66. data/spec/support/active_record_data/5_2_multi.txt +0 -210
  67. data/spec/support/active_record_data/6_0_diana.txt +0 -104
  68. data/spec/support/active_record_data/6_0_multi.txt +0 -210
  69. data/spec/support/active_record_data/6_1_diana.txt +0 -109
  70. data/spec/support/active_record_data/6_1_multi.txt +0 -220
  71. data/spec/support/active_record_data.rb +0 -22
  72. data/spec/support/ext_verifier.rb +0 -46
  73. data/spec/support/mongoid_versions.rb +0 -24
  74. data/spec/support/rails_versions.rb +0 -57
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d771d603c88c997bca4379eba3dc4cd794994e5810edb0859ad5c47c3b2590c
4
- data.tar.gz: 87ea8f90780f57731ad4cff0bfbce9eb2d837681829979bd23e839f62415a1b0
3
+ metadata.gz: 4c54bf5089bc3eb9ecb72f953d2b7972ec91f512d17d85f64e0e68bfffe963eb
4
+ data.tar.gz: e25109a0515688c4a3212e2dfa1a9a65a615959f67372a88f77011d8e0a531e0
5
5
  SHA512:
6
- metadata.gz: d1d1d0eb8ca306adbf3af3eec93d9cbb3fa6a47f1ad0e3b836d473bc4a0595be02dcd27af60de86a3f524bb373e5e0d2b9053f6b40dfac437add803dc075a315
7
- data.tar.gz: 3c74f2a0333fad6e5c34b5209124767b931d56c88423960f55945b694e27dadd41636259f7a1a395e64d4971887c2d1f095b64714acd525748dae5423d670872
6
+ metadata.gz: 53eb2f3a3bba9510f098ea1d550a2c4ce2080f21c4b19daea1e40e7b4fdc8df4336ef494bdb2626892e73f17ab6dc4bc2001dea1f64168d8d331baeb998ee273
7
+ data.tar.gz: 5cf3937b23f27e89a75c2a0adbf0353f8a2fb5f936f836b44f7e152559bdb0c7f8b1e30c95f9bca506342bf08b2da3e29b377bf1757ccf031de7e24cf7ccdc99
data/CHANGELOG.md CHANGED
@@ -1,58 +1,100 @@
1
+ # CHANGELOG
2
+
1
3
  ## Unreleased
2
4
 
5
+ ## v1.8.1
6
+
7
+ - Fix authors list in gemspec #128
8
+ - Use array.length.zero? to avoid issues with ActiveRecord collection proxy #129
9
+ - grammar glowup in README.md #130
10
+
11
+ ## v1.8.0
12
+
13
+ - Drop Ruby 3.0 and add Ruby 3.4 #125
14
+
15
+ ## v1.7.2
16
+
17
+ - update the version string in `AmazingPrint.version`
18
+
19
+ ## v1.7.1
20
+
21
+ - no changes, only linting
22
+
23
+ ## v1.7.0
24
+
25
+ - Require Ruby >= 3.0
26
+ - Add support for `::ActiveModel::Attributes`
27
+ - Preserve user defined color defaults
28
+ - Micro-optimization to `AmazingPrint::ActiveRecord#cast` (avoid calls to `#ancestors`)
29
+
30
+ ## v1.6.0
31
+
32
+ - Use pager with Pry #96
33
+ - Add Rails 7 appraisal #98
34
+ - Allow Hash keys to be colorized #99
35
+ - Use CI merge queue #104
36
+ - Add support for Ruby 3.3 #105
37
+ - Add Mongoid field aliases #106
38
+ - Add bigdecimal gem #109
39
+ - Add ExtLoader to help with require order issues #110
40
+
3
41
  ## v1.5.0
4
- - Drop support for Ruby 2.3 and 2.4 as well as JRuby 9.1
5
- - Add File/Dir formatters for mswin platform #48
6
- - Don't monkey patch String class #91
7
- - Fix ruby19 hash syntax so it can be copy-pasted #94
42
+
43
+ - Drop support for Ruby 2.3 and 2.4 as well as JRuby 9.1
44
+ - Add File/Dir formatters for mswin platform #48
45
+ - Don't monkey patch String class #91
46
+ - Fix ruby19 hash syntax so it can be copy-pasted #94
8
47
 
9
48
  ## v1.4.0
10
- - Support loading config from `$XDG_CONFIG_HOME/aprc` - #63
11
- - Remove support for Rails 5.1 #75
12
- - Update AR specs for Ruby 2.6.7 #76
13
- - Load .aprc configs only once. #74
14
- - Add XDG config support #77
15
- - Rubocop updates #79
16
- - Update Irb integration for v1.2.6+ #81
49
+
50
+ - Support loading config from `$XDG_CONFIG_HOME/aprc` - #63
51
+ - Remove support for Rails 5.1 #75
52
+ - Update AR specs for Ruby 2.6.7 #76
53
+ - Load .aprc configs only once. #74
54
+ - Add XDG config support #77
55
+ - Rubocop updates #79
56
+ - Update Irb integration for v1.2.6+ #81
17
57
 
18
58
  ## v1.3.0
19
- - Fix HTML escaping problems #53
20
- - Update test suite for Ruby 2.7.2 and JRuby #61
21
- - Add ActionView spec for html_safe #65
22
- - Add support for Rails 6.1 #68
23
- - Update specs for Ruby 3.0 #69
59
+
60
+ - Fix HTML escaping problems #53
61
+ - Update test suite for Ruby 2.7.2 and JRuby #61
62
+ - Add ActionView spec for html_safe #65
63
+ - Add support for Rails 6.1 #68
64
+ - Update specs for Ruby 3.0 #69
24
65
 
25
66
  ## v1.2.2
26
- - Support Ruby 3.0 / IRB 1.2.6 - #57
27
- - Fix FrozenError - #51
28
- - Drop support for Ruby 2.3 and 2.4 as well as JRuby 9.1 - #46
29
- - Add passing of `options` to `Logger#ap` - #55
67
+
68
+ - Support Ruby 3.0 / IRB 1.2.6 - #57
69
+ - Fix FrozenError - #51
70
+ - Drop support for Ruby 2.3 and 2.4 as well as JRuby 9.1 - #46
71
+ - Add passing of `options` to `Logger#ap` - #55
30
72
 
31
73
  ## v1.2.1
32
- - Correctly print active_model_errors for models that don't have tables - #42 by sahglie
33
- - Update AmazingPrint::MongoMapper for frozen strings - #44
34
74
 
35
- ## v1.2.0
75
+ - Correctly print active_model_errors for models that don't have tables - #42 by sahglie
76
+ - Update AmazingPrint::MongoMapper for frozen strings - #44
36
77
 
37
- - Fix frozen string literal issue with ActiveRecord
38
- - Add uncolor String method to remove ANSI color codes - #30 by duffyjp
39
- - Restore original copyright - #33 by amarshall
40
- - Remove method core extension since it is not needed since ruby 1.9 - #37 by grosser
41
- - Remove pale and black string color aliases - #38
42
- - Fix formatting ActionController::Parameters - #29
78
+ ## v1.2.0
43
79
 
80
+ - Fix frozen string literal issue with ActiveRecord
81
+ - Add uncolor String method to remove ANSI color codes - #30 by duffyjp
82
+ - Restore original copyright - #33 by amarshall
83
+ - Remove method core extension since it is not needed since ruby 1.9 - #37 by grosser
84
+ - Remove pale and black string color aliases - #38
85
+ - Fix formatting ActionController::Parameters - #29
44
86
 
45
87
  ## v1.1.0
46
- - Print out method keyword arguments
47
- - Fix NoMethodError with Sequel
48
- - Code cleanups
49
-
50
- Thanks for the great contributions from:
51
88
 
52
- - andydna
53
- - beanieboi
89
+ - Print out method keyword arguments
90
+ - Fix NoMethodError with Sequel
91
+ - Code cleanups
54
92
 
93
+ Thanks for the great contributions from:
55
94
 
95
+ - andydna
96
+ - beanieboi
56
97
 
57
98
  ## v1.0.0
58
- - Initial Release.
99
+
100
+ - Initial Release.
data/README.md CHANGED
@@ -8,14 +8,14 @@
8
8
  |
9
9
  [![Gitter](https://badges.gitter.im/amazing-print/community.svg)](https://gitter.im/amazing-print/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
10
10
 
11
- AmazingPrint is a fork of [AwesomePrint](https://github.com/awesome-print/awesome_print) which became stale and should be used in its place to avoid conflicts. It is a Ruby library that pretty prints Ruby objects in full color exposing their internal structure with proper indentation. Rails ActiveRecord objects and usage within Rails templates are supported via included mixins.
11
+ AmazingPrint is a fork of [AwesomePrint](https://github.com/awesome-print/awesome_print), which became stale, and should be used in its place to avoid conflicts. It is a Ruby library that pretty prints Ruby objects in full color, exposing their internal structure with proper indentation. Rails ActiveRecord objects and usage within Rails templates are supported via included mixins.
12
12
 
13
13
  ![GitHub API demo](github-api-demo.gif)
14
14
 
15
15
  ### Supported Versions ###
16
16
 
17
- - Ruby >= 2.5
18
- - Rails >= 5.2
17
+ - Ruby >= 3.1
18
+ - Rails >= 6.1
19
19
 
20
20
  ### Installation ###
21
21
  # Installing as Ruby gem
@@ -266,7 +266,7 @@ rails>
266
266
  ```
267
267
 
268
268
  ### IRB integration ###
269
- To use amazing_print as default formatter in irb and Rails console add the following
269
+ To use amazing_print as the default formatter in irb and the Rails console, add the following
270
270
  code to your ~/.irbrc file:
271
271
 
272
272
  ```ruby
@@ -293,7 +293,7 @@ By default, this logs at the :debug level. You can override that globally with:
293
293
 
294
294
  :log_level => :info
295
295
 
296
- in the custom defaults (see below). You can also override on a per call basis with:
296
+ in the custom defaults (see below). You can also override it on a per call basis with:
297
297
 
298
298
  logger.ap object, :warn
299
299
  # or
@@ -310,22 +310,22 @@ within Rails templates. For example:
310
310
  <%= ap @accounts.first %> # ERB
311
311
  != ap @accounts.first # HAML
312
312
 
313
- With other web frameworks (ex: in Sinatra templates) you can explicitly request HTML
313
+ With other web frameworks (for example in Sinatra templates) you can explicitly request HTML
314
314
  formatting:
315
315
 
316
316
  <%= ap @accounts.first, :html => true %>
317
317
 
318
- ### String Convenience Methods ###
318
+ ### Colorizing Strings ###
319
319
  Use methods such as `.red` to set string color:
320
320
 
321
321
  ```ruby
322
- irb> puts "red text".red
322
+ irb> puts AmazingPrint::Colors.red("red text")
323
323
  red text # (it's red)
324
324
  ```
325
325
 
326
326
  ### Setting Custom Defaults ###
327
- You can set your own default options by creating ``aprc`` file in your `$XDG_CONFIG_HOME`
328
- directory (defaults to `~/.config` if undefined). Within that file assign your defaults
327
+ You can set your own default options by creating an ``aprc`` file in your `$XDG_CONFIG_HOME`
328
+ directory (which defaults to `~/.config` if undefined). Within that file assign your defaults
329
329
  to ``AmazingPrint.defaults``.
330
330
  For example:
331
331
 
@@ -340,7 +340,7 @@ AmazingPrint.defaults = {
340
340
  }
341
341
  ```
342
342
 
343
- The previous `~/.aprc` location is still supported as fallback.
343
+ The previous `~/.aprc` location is still supported as a fallback.
344
344
 
345
345
  ## Versioning
346
346
 
@@ -56,7 +56,7 @@ module AwesomeMethodArray # :nodoc:
56
56
  # the comment :-)
57
57
  #
58
58
  arr = if blk
59
- super(pattern) do |match|
59
+ super do |match|
60
60
  #
61
61
  # The binding can only be used with Ruby-defined methods, therefore
62
62
  # we must rescue potential "ArgumentError: Can't create Binding from
@@ -75,10 +75,10 @@ module AwesomeMethodArray # :nodoc:
75
75
  yield match
76
76
  end
77
77
  else
78
- super(pattern)
78
+ super
79
79
  end
80
80
  arr.instance_variable_set(:@__awesome_methods__, instance_variable_get(:@__awesome_methods__))
81
- arr.select! { |item| (item.is_a?(Symbol) || item.is_a?(String)) } # grep block might return crap.
81
+ arr.select! { |item| item.is_a?(Symbol) || item.is_a?(String) } # grep block might return crap.
82
82
  arr
83
83
  end
84
84
  end
@@ -44,7 +44,13 @@ module AmazingPrint
44
44
  end
45
45
 
46
46
  def pry!
47
- Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry)
47
+ return unless defined?(Pry)
48
+
49
+ Pry.print = proc do |_output, value, pry_instance|
50
+ pry_instance.pager.open do |pager|
51
+ pager.print "#{value.ai}\n"
52
+ end
53
+ end
48
54
  end
49
55
 
50
56
  ##
@@ -20,11 +20,15 @@ module AmazingPrint
20
20
 
21
21
  if object.is_a?(::ActiveRecord::Base)
22
22
  cast = :active_record_instance
23
+ elsif object.is_a?(Class) && object.include?(::ActiveModel::Attributes)
24
+ cast = :active_model_class
25
+ elsif object.is_a?(::ActiveModel::Attributes)
26
+ cast = :active_model_instance
23
27
  elsif object.is_a?(::ActiveModel::Errors)
24
28
  cast = :active_model_error
25
- elsif object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base)
29
+ elsif object.is_a?(Class) && object <= ::ActiveRecord::Base
26
30
  cast = :active_record_class
27
- elsif type == :activerecord_relation || object.class.ancestors.include?(::ActiveRecord::Relation)
31
+ elsif type == :activerecord_relation || object.is_a?(::ActiveRecord::Relation)
28
32
  cast = :array
29
33
  end
30
34
  cast
@@ -71,6 +75,32 @@ module AmazingPrint
71
75
  [awesome_simple("class #{object} < #{object.superclass}", :class), awesome_hash(data)].join(' ')
72
76
  end
73
77
 
78
+ # Format ActiveModel instance object.
79
+ #------------------------------------------------------------------------------
80
+ def awesome_active_model_instance(object)
81
+ return object.inspect unless defined?(::ActiveSupport::OrderedHash)
82
+ return awesome_object(object) if @options[:raw]
83
+
84
+ data = object.attributes.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
85
+ hash[c.first.to_sym] = c.last
86
+ end
87
+
88
+ [awesome_simple(object.to_s, :active_model_instance), awesome_hash(data)].join(' ')
89
+ end
90
+
91
+ # Format ActiveModel class object.
92
+ #------------------------------------------------------------------------------
93
+ def awesome_active_model_class(object)
94
+ return object.inspect unless defined?(::ActiveSupport::OrderedHash)
95
+ return awesome_class(object) if @options[:raw]
96
+
97
+ data = object.attribute_types.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
98
+ hash[c.first.to_sym] = c.last.type
99
+ end
100
+
101
+ [awesome_simple("class #{object} < #{object.superclass}", :class), awesome_hash(data)].join(' ')
102
+ end
103
+
74
104
  # Format ActiveModel error object.
75
105
  #------------------------------------------------------------------------------
76
106
  def awesome_active_model_error(object)
@@ -19,8 +19,8 @@ module AmazingPrint
19
19
  cast = cast_without_mongo_mapper(object, type)
20
20
 
21
21
  if defined?(::MongoMapper::Document)
22
- if object.is_a?(Class) && !(object.ancestors & [::MongoMapper::Document,
23
- ::MongoMapper::EmbeddedDocument]).empty?
22
+ if object.is_a?(Class) && !object.ancestors.intersect?([::MongoMapper::Document,
23
+ ::MongoMapper::EmbeddedDocument]).nil?
24
24
  cast = :mongo_mapper_class
25
25
  elsif object.is_a?(::MongoMapper::Document) || object.is_a?(::MongoMapper::EmbeddedDocument)
26
26
  cast = :mongo_mapper_instance
@@ -33,8 +33,14 @@ module AmazingPrint
33
33
  def awesome_mongoid_class(object)
34
34
  return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields)
35
35
 
36
+ aliases = object.aliased_fields.invert
36
37
  data = object.fields.sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
37
- hash[c[1].name.to_sym] = (c[1].type || 'undefined').to_s.underscore.intern
38
+ name = c[1].name
39
+ alias_name = aliases[name] unless name == '_id'
40
+ printed_name = alias_name ? "#{alias_name}(#{name})" : name
41
+
42
+ hash[printed_name.to_sym] = (c[1].type || 'undefined').to_s.underscore.intern
43
+ hash
38
44
  end
39
45
 
40
46
  name = "class #{awesome_simple(object.to_s, :class)}"
@@ -48,8 +54,14 @@ module AmazingPrint
48
54
  def awesome_mongoid_document(object)
49
55
  return object.inspect unless defined?(::ActiveSupport::OrderedHash)
50
56
 
57
+ aliases = object.aliased_fields.invert
51
58
  data = (object.attributes || {}).sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
52
- hash[c[0].to_sym] = c[1]
59
+ name = c[0]
60
+ alias_name = aliases[name] unless name == '_id'
61
+ printed_name = alias_name ? "#{alias_name}(#{name})" : name
62
+
63
+ hash[printed_name.to_sym] = c[1]
64
+ hash
53
65
  end
54
66
  data = { errors: object.errors, attributes: data } unless object.errors.empty?
55
67
  "#{object} #{awesome_hash(data)}"
@@ -32,9 +32,9 @@ module AmazingPrint
32
32
  #------------------------------------------------------------------------------
33
33
  def awesome_nobrainer_class(object)
34
34
  name = "#{awesome_simple(object, :class)} < #{awesome_simple(object.superclass, :class)}"
35
- data = object.fields.map do |field, options|
35
+ data = object.fields.to_h do |field, options|
36
36
  [field, (options[:type] || Object).to_s.underscore.to_sym]
37
- end.to_h
37
+ end
38
38
 
39
39
  name = "class #{awesome_simple(object.to_s, :class)}"
40
40
  base = "< #{awesome_simple(object.superclass.to_s, :class)}"
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AmazingPrint
4
+ ##
5
+ # Attempt to load extensions up to 3 times since this library may be required
6
+ # before dependencies that we have extensions for.
7
+ #
8
+ class ExtLoader
9
+ EXT_LOAD_ATTEMPT_LIMIT = 3
10
+
11
+ @load_attemps = 0
12
+
13
+ def self.call
14
+ return if @load_attemps >= EXT_LOAD_ATTEMPT_LIMIT
15
+
16
+ require_relative 'ext/mongo_mapper' if defined?(MongoMapper)
17
+ require_relative 'ext/mongoid' if defined?(Mongoid)
18
+ require_relative 'ext/nobrainer' if defined?(NoBrainer)
19
+ require_relative 'ext/nokogiri' if defined?(Nokogiri)
20
+ require_relative 'ext/ostruct' if defined?(OpenStruct) # rubocop:disable Style/OpenStructUse
21
+ require_relative 'ext/ripple' if defined?(Ripple)
22
+ require_relative 'ext/sequel' if defined?(Sequel)
23
+
24
+ @load_attemps += 1
25
+ end
26
+ end
27
+ end
@@ -48,7 +48,7 @@ module AmazingPrint
48
48
  elsif (hash = convert_to_hash(object))
49
49
  awesome_hash(hash)
50
50
  else
51
- awesome_simple(object.inspect.to_s, type, @inspector)
51
+ awesome_simple(object.inspect.to_s, type, @inspector) # rubocop:disable Lint/RedundantTypeConversion
52
52
  end
53
53
  end
54
54
 
@@ -15,7 +15,7 @@ module AmazingPrint
15
15
  end
16
16
 
17
17
  def format
18
- if array.empty?
18
+ if array.length.zero? # rubocop:disable Style/ZeroLengthPredicate
19
19
  '[]'
20
20
  elsif methods_array?
21
21
  methods_array
@@ -110,8 +110,8 @@ module AmazingPrint
110
110
  inspector.current_indentation
111
111
  end
112
112
 
113
- def indented(&blk)
114
- inspector.increase_indentation(&blk)
113
+ def indented(&)
114
+ inspector.increase_indentation(&)
115
115
  end
116
116
 
117
117
  # precompute common indentations
@@ -130,17 +130,26 @@ module AmazingPrint
130
130
  def align(value, width)
131
131
  if options[:multiline]
132
132
  indent_option = options[:indent]
133
+ effective_width = width + value.size - colorless_size(value)
133
134
  if indent_option.positive?
134
- value.rjust(width)
135
+ value.rjust(effective_width)
135
136
  elsif indent_option.zero?
136
- "#{indent}#{value.ljust(width)}"
137
+ "#{indent}#{value.ljust(effective_width)}"
137
138
  else
138
- "#{indent(indentation + indent_option)}#{value.ljust(width)}"
139
+ "#{indent(indentation + indent_option)}#{value.ljust(effective_width)}"
139
140
  end
140
141
  else
141
142
  value
142
143
  end
143
144
  end
145
+
146
+ def colorless(string)
147
+ string.gsub(/\e\[[\d;]+m/, '')
148
+ end
149
+
150
+ def colorless_size(string)
151
+ colorless(string).size
152
+ end
144
153
  end
145
154
  end
146
155
  end
@@ -65,8 +65,18 @@ module AmazingPrint
65
65
  result
66
66
  end
67
67
 
68
+ def key_size(key)
69
+ return key.inspect.size if symbol?(key)
70
+
71
+ if options[:html]
72
+ single_line { inspector.awesome(key) }.size
73
+ else
74
+ plain_single_line { inspector.awesome(key) }.size
75
+ end
76
+ end
77
+
68
78
  def max_key_width(keys)
69
- keys.map { |key, _value| key.size }.max || 0
79
+ keys.map { |key, _value| key_size(key) }.max || 0
70
80
  end
71
81
 
72
82
  def printable_keys
@@ -75,34 +85,41 @@ module AmazingPrint
75
85
  keys.sort! { |a, b| a.to_s <=> b.to_s } if options[:sort_keys]
76
86
 
77
87
  keys.map! do |key|
78
- plain_single_line do
79
- [inspector.awesome(key), hash[key]]
88
+ single_line do
89
+ [key, hash[key]]
80
90
  end
81
91
  end
82
92
  end
83
93
 
84
94
  def symbol?(key)
85
- key[0] == ':'
95
+ key.is_a?(Symbol)
86
96
  end
87
97
 
88
98
  def ruby19_syntax(key, value, width)
89
- key[0] = ''
90
- key << ':'
91
- "#{align(key, width)} #{inspector.awesome(value)}"
99
+ # Move the colon to the right side of the symbol
100
+ awesome_key = inspector.awesome(key).sub(/#{key.inspect}/, "#{key}:")
101
+
102
+ "#{align(awesome_key, width)} #{inspector.awesome(value)}"
92
103
  end
93
104
 
94
105
  def pre_ruby19_syntax(key, value, width)
95
- "#{align(key, width)}#{colorize(' => ', :hash)}#{inspector.awesome(value)}"
106
+ awesome_key = single_line { inspector.awesome(key) }
107
+ "#{align(awesome_key, width)}#{colorize(' => ', :hash)}#{inspector.awesome(value)}"
96
108
  end
97
109
 
98
- def plain_single_line
110
+ def plain_single_line(&)
99
111
  plain = options[:plain]
100
- multiline = options[:multiline]
101
112
  options[:plain] = true
113
+ single_line(&)
114
+ ensure
115
+ options[:plain] = plain
116
+ end
117
+
118
+ def single_line
119
+ multiline = options[:multiline]
102
120
  options[:multiline] = false
103
121
  yield
104
122
  ensure
105
- options[:plain] = plain
106
123
  options[:multiline] = multiline
107
124
  end
108
125
  end
@@ -27,10 +27,10 @@ module AmazingPrint
27
27
  def mode
28
28
  r = ['-'] * 6
29
29
  r[0] = 'd' if @stat.directory?
30
- r[1] = 'a' unless (@attrs & FILE_ATTRIBUTE_ARCHIVE).zero?
31
- r[2] = 'r' unless (@attrs & FILE_ATTRIBUTE_READONLY).zero?
32
- r[3] = 'h' unless (@attrs & FILE_ATTRIBUTE_HIDDEN).zero?
33
- r[4] = 's' unless (@attrs & FILE_ATTRIBUTE_SYSTEM).zero?
30
+ r[1] = 'a' unless @attrs.nobits?(FILE_ATTRIBUTE_ARCHIVE)
31
+ r[2] = 'r' unless @attrs.nobits?(FILE_ATTRIBUTE_READONLY)
32
+ r[3] = 'h' unless @attrs.nobits?(FILE_ATTRIBUTE_HIDDEN)
33
+ r[4] = 's' unless @attrs.nobits?(FILE_ATTRIBUTE_SYSTEM)
34
34
  r[5] = 'l' if File.symlink? @fname
35
35
  r.join
36
36
  end
@@ -17,7 +17,7 @@ module AmazingPrint
17
17
 
18
18
  def format
19
19
  vars = variables.map do |var|
20
- property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet.
20
+ property = var.to_s[1..].to_sym # to_s because of some monkey patching done by Puppet.
21
21
  accessor = if object.respond_to?(:"#{property}=")
22
22
  object.respond_to?(property) ? :accessor : :writer
23
23
  else
@@ -68,7 +68,7 @@ module AmazingPrint
68
68
  # We need to ensure that the original Kernel#format is used here instead of the one
69
69
  # defined above.
70
70
  # rubocop:disable Style/ColonMethodCall
71
- str + Kernel::format(':0x%08x', (object.__id__ * 2))
71
+ str + Kernel::format(':0x%08x', object.__id__ * 2)
72
72
  # rubocop:enable Style/ColonMethodCall
73
73
  end
74
74
 
@@ -17,7 +17,7 @@ module AmazingPrint
17
17
 
18
18
  def format
19
19
  vars = variables.map do |var|
20
- property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet.
20
+ property = var.to_s[1..].to_sym # to_s because of some monkey patching done by Puppet.
21
21
  accessor = if struct.respond_to?(:"#{property}=")
22
22
  struct.respond_to?(property) ? :accessor : :writer
23
23
  else
@@ -61,7 +61,7 @@ module AmazingPrint
61
61
  # We need to ensure that the original Kernel#format is used here instead of the one defined
62
62
  # above.
63
63
  # rubocop:disable Style/ColonMethodCall
64
- Kernel::format("#{struct.class.superclass}:#{struct.class}:0x%08x", (struct.__id__ * 2))
64
+ Kernel::format("#{struct.class.superclass}:#{struct.class}:0x%08x", struct.__id__ * 2)
65
65
  # rubocop:enable Style/ColonMethodCall
66
66
  end
67
67
 
@@ -6,9 +6,10 @@
6
6
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
7
7
  #------------------------------------------------------------------------------
8
8
 
9
- # rubocop:disable Metrics/ClassLength
9
+ # rubocop:disable Metrics/ClassLength, Metrics/MethodLength
10
10
 
11
11
  require_relative 'indentator'
12
+ require_relative 'ext_loader'
12
13
 
13
14
  module AmazingPrint
14
15
  class Inspector
@@ -70,14 +71,16 @@ module AmazingPrint
70
71
  @formatter = AmazingPrint::Formatter.new(self)
71
72
  @indentator = AmazingPrint::Indentator.new(@options[:indent].abs)
72
73
  Thread.current[AP] ||= []
74
+
75
+ ExtLoader.call
73
76
  end
74
77
 
75
78
  def current_indentation
76
79
  indentator.indentation
77
80
  end
78
81
 
79
- def increase_indentation(&blk)
80
- indentator.indent(&blk)
82
+ def increase_indentation(&)
83
+ indentator.indent(&)
81
84
  end
82
85
 
83
86
  # Dispatcher that detects data nesting and invokes object-aware formatter.
@@ -189,11 +192,11 @@ module AmazingPrint
189
192
  #---------------------------------------------------------------------------
190
193
  def merge_custom_defaults!
191
194
  load_dotfile
192
- merge_options!(AmazingPrint.defaults) if AmazingPrint.defaults.is_a?(Hash)
195
+ merge_options!(AmazingPrint.defaults.dup) if AmazingPrint.defaults.is_a?(Hash)
193
196
  rescue StandardError => e
194
197
  warn "Could not load '.aprc' from ENV['HOME']: #{e}"
195
198
  end
196
199
  end
197
200
  end
198
201
 
199
- # rubocop:enable Metrics/ClassLength
202
+ # rubocop:enable Metrics/ClassLength, Metrics/MethodLength
@@ -7,6 +7,6 @@
7
7
  #------------------------------------------------------------------------------
8
8
  module AmazingPrint
9
9
  def self.version
10
- '1.5.0'
10
+ '1.8.1'
11
11
  end
12
12
  end
data/lib/amazing_print.rb CHANGED
@@ -33,12 +33,6 @@ unless defined?(AmazingPrint::Inspector)
33
33
  require_relative 'amazing_print/ext/action_view'
34
34
  end
35
35
  end
36
- require_relative 'amazing_print/ext/mongo_mapper' if defined?(MongoMapper)
37
- require_relative 'amazing_print/ext/mongoid' if defined?(Mongoid)
38
- require_relative 'amazing_print/ext/nokogiri' if defined?(Nokogiri)
39
- require_relative 'amazing_print/ext/nobrainer' if defined?(NoBrainer)
40
- require_relative 'amazing_print/ext/ripple' if defined?(Ripple)
41
- require_relative 'amazing_print/ext/sequel' if defined?(Sequel)
42
- require_relative 'amazing_print/ext/ostruct' if defined?(OpenStruct)
36
+
37
+ AmazingPrint::ExtLoader.call
43
38
  end
44
- # test