amazing_print 1.3.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/Appraisals +0 -6
  3. data/CHANGELOG.md +24 -0
  4. data/Gemfile +9 -0
  5. data/Gemfile.lock +51 -47
  6. data/README.md +9 -7
  7. data/lib/amazing_print/colorize.rb +5 -14
  8. data/lib/amazing_print/colors.rb +37 -0
  9. data/lib/amazing_print/core_ext/awesome_method_array.rb +4 -4
  10. data/lib/amazing_print/core_ext/class.rb +1 -1
  11. data/lib/amazing_print/core_ext/logger.rb +1 -3
  12. data/lib/amazing_print/core_ext/object.rb +1 -1
  13. data/lib/amazing_print/custom_defaults.rb +10 -3
  14. data/lib/amazing_print/ext/active_record.rb +5 -9
  15. data/lib/amazing_print/ext/active_support.rb +1 -3
  16. data/lib/amazing_print/ext/mongo_mapper.rb +4 -7
  17. data/lib/amazing_print/ext/mongoid.rb +2 -6
  18. data/lib/amazing_print/ext/nobrainer.rb +3 -5
  19. data/lib/amazing_print/ext/nokogiri.rb +1 -3
  20. data/lib/amazing_print/ext/ostruct.rb +1 -3
  21. data/lib/amazing_print/ext/ripple.rb +4 -5
  22. data/lib/amazing_print/formatter.rb +5 -6
  23. data/lib/amazing_print/formatters/array_formatter.rb +7 -6
  24. data/lib/amazing_print/formatters/base_formatter.rb +20 -25
  25. data/lib/amazing_print/formatters/class_formatter.rb +1 -0
  26. data/lib/amazing_print/formatters/dir_formatter.rb +11 -1
  27. data/lib/amazing_print/formatters/file_formatter.rb +11 -1
  28. data/lib/amazing_print/formatters/hash_formatter.rb +5 -3
  29. data/lib/amazing_print/formatters/method_formatter.rb +1 -0
  30. data/lib/amazing_print/formatters/mswin_helper.rb +63 -0
  31. data/lib/amazing_print/formatters/object_formatter.rb +2 -1
  32. data/lib/amazing_print/formatters/simple_formatter.rb +1 -0
  33. data/lib/amazing_print/formatters/struct_formatter.rb +2 -1
  34. data/lib/amazing_print/inspector.rb +29 -5
  35. data/lib/amazing_print/version.rb +1 -1
  36. data/lib/amazing_print.rb +3 -7
  37. data/spec/active_record_helper.rb +3 -0
  38. data/spec/colors_spec.rb +15 -6
  39. data/spec/core_ext/logger_spec.rb +7 -7
  40. data/spec/ext/action_controller_spec.rb +5 -5
  41. data/spec/ext/active_model_spec.rb +1 -1
  42. data/spec/ext/active_record_spec.rb +39 -73
  43. data/spec/ext/active_support_spec.rb +3 -3
  44. data/spec/ext/mongo_mapper_spec.rb +15 -11
  45. data/spec/ext/mongoid_spec.rb +7 -3
  46. data/spec/ext/nobrainer_spec.rb +6 -2
  47. data/spec/ext/nokogiri_spec.rb +7 -7
  48. data/spec/ext/ripple_spec.rb +6 -2
  49. data/spec/ext/sequel_spec.rb +1 -1
  50. data/spec/formats_spec.rb +101 -90
  51. data/spec/methods_spec.rb +12 -4
  52. data/spec/misc_spec.rb +29 -70
  53. data/spec/objects_spec.rb +4 -0
  54. data/spec/spec_helper.rb +11 -9
  55. metadata +10 -142
  56. data/lib/amazing_print/core_ext/string.rb +0 -45
  57. data/spec/core_ext/string_spec.rb +0 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63ce7e50d17b53a5270709def03c7f3075a7068ada7d11ca7f124bda5cddc735
4
- data.tar.gz: de6d68e51a4e70fdad3fff7fc5050ab6984280afaa3851a9958bf24731bb022d
3
+ metadata.gz: 5d771d603c88c997bca4379eba3dc4cd794994e5810edb0859ad5c47c3b2590c
4
+ data.tar.gz: 87ea8f90780f57731ad4cff0bfbce9eb2d837681829979bd23e839f62415a1b0
5
5
  SHA512:
6
- metadata.gz: cb0a4eb248f06fd34e28d5161ab984653af5661347e4d32e76ec3b2be995ff31f9fec4b5c8355a19908bd16f7219ed2148556d383bbb36af740925e5a4ec9004
7
- data.tar.gz: c48fe1109d33a49653f8f506233c5bf674137008fed98d083022b46a72b26aa0cd469f13bca4ef1b6b2bdee1657bcf8d66481e505cca045de47dd00a40a6475f
6
+ metadata.gz: d1d1d0eb8ca306adbf3af3eec93d9cbb3fa6a47f1ad0e3b836d473bc4a0595be02dcd27af60de86a3f524bb373e5e0d2b9053f6b40dfac437add803dc075a315
7
+ data.tar.gz: 3c74f2a0333fad6e5c34b5209124767b931d56c88423960f55945b694e27dadd41636259f7a1a395e64d4971887c2d1f095b64714acd525748dae5423d670872
data/Appraisals CHANGED
@@ -1,12 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  if RUBY_VERSION < '3'
4
- appraise 'rails-5.1' do
5
- gem 'activerecord-jdbcsqlite3-adapter', '~> 51.0', platform: :jruby
6
- gem 'rails', '~> 5.1.0'
7
- gem 'sqlite3', platform: :mri
8
- end
9
-
10
4
  appraise 'rails-5.2' do
11
5
  gem 'activerecord-jdbcsqlite3-adapter', '~> 52.0', platform: :jruby
12
6
  gem 'rails', '~> 5.2.0'
data/CHANGELOG.md CHANGED
@@ -1,3 +1,27 @@
1
+ ## Unreleased
2
+
3
+ ## 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
8
+
9
+ ## 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
17
+
18
+ ## 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
24
+
1
25
  ## v1.2.2
2
26
  - Support Ruby 3.0 / IRB 1.2.6 - #57
3
27
  - Fix FrozenError - #51
data/Gemfile CHANGED
@@ -4,3 +4,12 @@ source 'https://rubygems.org'
4
4
 
5
5
  # Specify your gem's dependencies in amazing_print.gemspec
6
6
  gemspec
7
+
8
+ # Development dependencies
9
+ gem 'appraisal', '~> 2.3'
10
+ gem 'fakefs', '~> 1.2'
11
+ gem 'nokogiri', '~> 1.10'
12
+ gem 'pry'
13
+ gem 'rspec', '~> 3.9'
14
+ gem 'rubocop', '~> 1.20'
15
+ gem 'rubocop-rspec', '~> 2.4'
data/Gemfile.lock CHANGED
@@ -1,70 +1,73 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- amazing_print (1.2.3.pre)
4
+ amazing_print (1.5.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- appraisal (2.3.0)
9
+ appraisal (2.4.1)
10
10
  bundler
11
11
  rake
12
12
  thor (>= 0.14.0)
13
- ast (2.4.1)
13
+ ast (2.4.2)
14
14
  coderay (1.1.3)
15
- diff-lcs (1.4.4)
16
- fakefs (1.2.2)
17
- ffi (1.13.1-java)
18
- jaro_winkler (1.5.4)
19
- jaro_winkler (1.5.4-java)
15
+ diff-lcs (1.5.0)
16
+ fakefs (1.9.0)
17
+ json (2.6.3)
20
18
  method_source (1.0.0)
21
- mini_portile2 (2.4.0)
22
- nokogiri (1.10.10)
23
- mini_portile2 (~> 2.4.0)
24
- nokogiri (1.10.10-java)
25
- parallel (1.20.0)
26
- parser (2.7.2.0)
19
+ nokogiri (1.15.2-x86_64-linux)
20
+ racc (~> 1.4)
21
+ parallel (1.23.0)
22
+ parser (3.2.2.1)
27
23
  ast (~> 2.4.1)
28
- pry (0.13.1)
24
+ pry (0.14.2)
29
25
  coderay (~> 1.1)
30
26
  method_source (~> 1.0)
31
- pry (0.13.1-java)
32
- coderay (~> 1.1)
33
- method_source (~> 1.0)
34
- spoon (~> 0.0)
35
- rainbow (3.0.0)
36
- rake (13.0.1)
37
- rexml (3.2.4)
38
- rspec (3.10.0)
39
- rspec-core (~> 3.10.0)
40
- rspec-expectations (~> 3.10.0)
41
- rspec-mocks (~> 3.10.0)
42
- rspec-core (3.10.0)
43
- rspec-support (~> 3.10.0)
44
- rspec-expectations (3.10.0)
27
+ racc (1.6.2)
28
+ rainbow (3.1.1)
29
+ rake (13.0.6)
30
+ regexp_parser (2.8.0)
31
+ rexml (3.2.5)
32
+ rspec (3.12.0)
33
+ rspec-core (~> 3.12.0)
34
+ rspec-expectations (~> 3.12.0)
35
+ rspec-mocks (~> 3.12.0)
36
+ rspec-core (3.12.2)
37
+ rspec-support (~> 3.12.0)
38
+ rspec-expectations (3.12.3)
45
39
  diff-lcs (>= 1.2.0, < 2.0)
46
- rspec-support (~> 3.10.0)
47
- rspec-mocks (3.10.0)
40
+ rspec-support (~> 3.12.0)
41
+ rspec-mocks (3.12.5)
48
42
  diff-lcs (>= 1.2.0, < 2.0)
49
- rspec-support (~> 3.10.0)
50
- rspec-support (3.10.0)
51
- rubocop (0.81.0)
52
- jaro_winkler (~> 1.5.1)
43
+ rspec-support (~> 3.12.0)
44
+ rspec-support (3.12.0)
45
+ rubocop (1.51.0)
46
+ json (~> 2.3)
53
47
  parallel (~> 1.10)
54
- parser (>= 2.7.0.1)
48
+ parser (>= 3.2.0.0)
55
49
  rainbow (>= 2.2.2, < 4.0)
56
- rexml
50
+ regexp_parser (>= 1.8, < 3.0)
51
+ rexml (>= 3.2.5, < 4.0)
52
+ rubocop-ast (>= 1.28.0, < 2.0)
57
53
  ruby-progressbar (~> 1.7)
58
- unicode-display_width (>= 1.4.0, < 2.0)
59
- ruby-progressbar (1.10.1)
60
- spoon (0.0.6)
61
- ffi
62
- thor (1.0.1)
63
- unicode-display_width (1.7.0)
54
+ unicode-display_width (>= 2.4.0, < 3.0)
55
+ rubocop-ast (1.28.1)
56
+ parser (>= 3.2.1.0)
57
+ rubocop-capybara (2.18.0)
58
+ rubocop (~> 1.41)
59
+ rubocop-factory_bot (2.23.1)
60
+ rubocop (~> 1.33)
61
+ rubocop-rspec (2.22.0)
62
+ rubocop (~> 1.33)
63
+ rubocop-capybara (~> 2.17)
64
+ rubocop-factory_bot (~> 2.22)
65
+ ruby-progressbar (1.13.0)
66
+ thor (1.2.2)
67
+ unicode-display_width (2.4.2)
64
68
 
65
69
  PLATFORMS
66
- java
67
- ruby
70
+ x86_64-linux
68
71
 
69
72
  DEPENDENCIES
70
73
  amazing_print!
@@ -73,7 +76,8 @@ DEPENDENCIES
73
76
  nokogiri (~> 1.10)
74
77
  pry
75
78
  rspec (~> 3.9)
76
- rubocop (~> 0.81.0)
79
+ rubocop (~> 1.20)
80
+ rubocop-rspec (~> 2.4)
77
81
 
78
82
  BUNDLED WITH
79
- 2.1.4
83
+ 2.3.12
data/README.md CHANGED
@@ -3,9 +3,8 @@
3
3
  [![RubyGems][gem_version_badge]][ruby_gems]
4
4
  [![RubyGems][gem_downloads_badge]][ruby_gems]
5
5
  |
6
- [![Travis CI][travis_ci_badge]][travis_ci]
7
6
  ![Specs](https://github.com/amazing-print/amazing_print/workflows/Specs/badge.svg)
8
- ![Lint](https://github.com/amazing-print/amazing_print/workflows/Lint/badge.svg)
7
+ ![Lint](https://github.com/amazing-print/amazing_print/workflows/Lints/badge.svg)
9
8
  |
10
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)
11
10
 
@@ -15,8 +14,8 @@ AmazingPrint is a fork of [AwesomePrint](https://github.com/awesome-print/awesom
15
14
 
16
15
  ### Supported Versions ###
17
16
 
18
- - Ruby >= 2.3
19
- - Rails >= 5.1
17
+ - Ruby >= 2.5
18
+ - Rails >= 5.2
20
19
 
21
20
  ### Installation ###
22
21
  # Installing as Ruby gem
@@ -325,12 +324,13 @@ red text # (it's red)
325
324
  ```
326
325
 
327
326
  ### Setting Custom Defaults ###
328
- You can set your own default options by creating ``.aprc`` file in your home
329
- directory. Within that file assign your defaults to ``AmazingPrint.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
329
+ to ``AmazingPrint.defaults``.
330
330
  For example:
331
331
 
332
332
  ```ruby
333
- # ~/.aprc file.
333
+ # ~/.config/aprc file.
334
334
  AmazingPrint.defaults = {
335
335
  :indent => -2,
336
336
  :color => {
@@ -340,6 +340,8 @@ AmazingPrint.defaults = {
340
340
  }
341
341
  ```
342
342
 
343
+ The previous `~/.aprc` location is still supported as fallback.
344
+
343
345
  ## Versioning
344
346
 
345
347
  AmazingPrint follows the [Semantic Versioning](http://semver.org/) standard.
@@ -2,26 +2,17 @@
2
2
 
3
3
  autoload :CGI, 'cgi'
4
4
 
5
+ require_relative 'colors'
6
+
5
7
  module AmazingPrint
6
8
  module Colorize
7
9
  # Pick the color and apply it to the given string as necessary.
8
10
  #------------------------------------------------------------------------------
9
11
  def colorize(str, type)
10
12
  str = CGI.escapeHTML(str) if options[:html]
11
- if options[:plain] || !options[:color][type] || !inspector.colorize?
12
- str
13
- #
14
- # Check if the string color method is defined by amazing_print and accepts
15
- # html parameter or it has been overriden by some gem such as colorize.
16
- #
17
- elsif str.method(options[:color][type]).arity == -1 # Accepts html parameter.
18
- str.send(options[:color][type], options[:html])
19
- else
20
- if options[:html]
21
- str = %(<kbd style="color:#{options[:color][type]}">#{str}</kbd>)
22
- end
23
- str.send(options[:color][type])
24
- end
13
+ return str if options[:plain] || !options[:color][type] || !inspector.colorize?
14
+
15
+ AmazingPrint::Colors.public_send(options[:color][type], str, options[:html])
25
16
  end
26
17
  end
27
18
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AmazingPrint
4
+ module Colors
5
+ module_function
6
+
7
+ #
8
+ # ANSI color codes:
9
+ # \e => escape
10
+ # 30 => color base
11
+ # 1 => bright
12
+ # 0 => normal
13
+ #
14
+ # For HTML coloring we use <kbd> tag instead of <span> to require monospace
15
+ # font. Note that beloved <tt> has been removed from HTML5.
16
+ #
17
+ %w[gray red green yellow blue purple cyan white].zip(
18
+ %w[black darkred darkgreen brown navy darkmagenta darkcyan slategray]
19
+ ).each_with_index do |(color, shade), i|
20
+ # NOTE: Format strings are created once only, for performance, and remembered by closures.
21
+
22
+ term_bright_seq = "\e[1;#{i + 30}m%s\e[0m"
23
+ html_bright_seq = %(<kbd style="color:#{color}">%s</kbd>)
24
+
25
+ define_method color do |str, html = false|
26
+ (html ? html_bright_seq : term_bright_seq) % str
27
+ end
28
+
29
+ term_normal_seq = "\e[0;#{i + 30}m%s\e[0m"
30
+ html_normal_seq = %(<kbd style="color:#{shade}">%s</kbd>)
31
+
32
+ define_method "#{color}ish" do |str, html = false|
33
+ (html ? html_normal_seq : term_normal_seq) % str
34
+ end
35
+ end
36
+ end
37
+ end
@@ -14,14 +14,14 @@
14
14
  #
15
15
  # If you could think of a better way please let me know :-)
16
16
  #
17
- module AwesomeMethodArray #:nodoc:
18
- def -(_other_ary)
17
+ module AwesomeMethodArray # :nodoc:
18
+ def -(other)
19
19
  super.tap do |arr|
20
20
  arr.instance_variable_set(:@__awesome_methods__, instance_variable_get(:@__awesome_methods__))
21
21
  end
22
22
  end
23
23
 
24
- def &(_other_ary)
24
+ def &(other)
25
25
  super.tap do |arr|
26
26
  arr.instance_variable_set(:@__awesome_methods__, instance_variable_get(:@__awesome_methods__))
27
27
  end
@@ -68,7 +68,7 @@ module AwesomeMethodArray #:nodoc:
68
68
  # [ 0, 1, 2, 3, 4 ].grep(1..2, &:succ)
69
69
  #
70
70
  begin
71
- eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding)
71
+ eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding, __FILE__, __LINE__)
72
72
  rescue StandardError
73
73
  ArgumentError
74
74
  end
@@ -5,7 +5,7 @@
5
5
  # AmazingPrint is freely distributable under the terms of MIT license.
6
6
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
7
7
  #------------------------------------------------------------------------------
8
- class Class #:nodoc:
8
+ class Class # :nodoc:
9
9
  #
10
10
  # Intercept methods below to inject @__amazing_print__ instance variable
11
11
  # so we know it is the *methods* array when formatting an array.
@@ -25,6 +25,4 @@ module AmazingPrint
25
25
  end
26
26
 
27
27
  Logger.include AmazingPrint::Logger
28
- if defined?(ActiveSupport::BufferedLogger)
29
- ActiveSupport::BufferedLogger.include AmazingPrint::Logger
30
- end
28
+ ActiveSupport::BufferedLogger.include AmazingPrint::Logger if defined?(ActiveSupport::BufferedLogger)
@@ -5,7 +5,7 @@
5
5
  # AmazingPrint is freely distributable under the terms of MIT license.
6
6
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
7
7
  #------------------------------------------------------------------------------
8
- class Object #:nodoc:
8
+ class Object # :nodoc:
9
9
  #
10
10
  # Intercept methods below to inject @__amazing_print__ instance variable
11
11
  # so we know it is the *methods* array when formatting an array.
@@ -7,8 +7,8 @@ module AmazingPrint
7
7
  # Class accessor to force colorized output (ex. forked subprocess where TERM
8
8
  # might be dumb).
9
9
  #---------------------------------------------------------------------------
10
- def force_colors!(value = true)
11
- @force_colors = value
10
+ def force_colors!(colors: true)
11
+ @force_colors = colors
12
12
  end
13
13
 
14
14
  def console?
@@ -29,7 +29,7 @@ module AmazingPrint
29
29
 
30
30
  def usual_rb
31
31
  IRB::Irb.class_eval do
32
- def output_value(_omit = false)
32
+ def output_value(_omit = false) # rubocop:disable Style/OptionalBooleanParameter
33
33
  ap @context.last_value
34
34
  rescue NoMethodError
35
35
  puts "(Object doesn't support #ai)"
@@ -47,6 +47,13 @@ module AmazingPrint
47
47
  Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry)
48
48
  end
49
49
 
50
+ ##
51
+ # Reload the cached custom configurations.
52
+ #
53
+ def reload!
54
+ AmazingPrint::Inspector.reload_dotfile
55
+ end
56
+
50
57
  private
51
58
 
52
59
  # Takes a value and returns true unless it is false or nil
@@ -45,15 +45,15 @@ module AmazingPrint
45
45
  return object.inspect unless defined?(::ActiveSupport::OrderedHash)
46
46
  return awesome_object(object) if @options[:raw]
47
47
 
48
- data = if object.class.column_names != object.attributes.keys
49
- object.attributes
50
- else
48
+ data = if object.class.column_names == object.attributes.keys
51
49
  object.class.column_names.each_with_object(::ActiveSupport::OrderedHash.new) do |name, hash|
52
50
  if object.has_attribute?(name) || object.new_record?
53
51
  value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name)
54
52
  hash[name.to_sym] = value
55
53
  end
56
54
  end
55
+ else
56
+ object.attributes
57
57
  end
58
58
  [awesome_simple(object.to_s, :active_record_instance), awesome_hash(data)].join(' ')
59
59
  end
@@ -61,12 +61,8 @@ module AmazingPrint
61
61
  # Format ActiveRecord class object.
62
62
  #------------------------------------------------------------------------------
63
63
  def awesome_active_record_class(object)
64
- if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:columns) || object.to_s == 'ActiveRecord::Base'
65
- return object.inspect
66
- end
67
- if object.respond_to?(:abstract_class?) && object.abstract_class?
68
- return awesome_class(object)
69
- end
64
+ return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:columns) || object.to_s == 'ActiveRecord::Base'
65
+ return awesome_class(object) if object.respond_to?(:abstract_class?) && object.abstract_class?
70
66
 
71
67
  data = object.columns.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
72
68
  hash[c.name.to_sym] = c.type
@@ -42,6 +42,4 @@ AmazingPrint::Formatter.include AmazingPrint::ActiveSupport
42
42
  #
43
43
  # Colorize Rails logs.
44
44
  #
45
- if defined?(ActiveSupport::LogSubscriber)
46
- AmazingPrint.force_colors! ActiveSupport::LogSubscriber.colorize_logging
47
- end
45
+ AmazingPrint.force_colors! colors: ActiveSupport::LogSubscriber.colorize_logging if defined?(ActiveSupport::LogSubscriber)
@@ -19,7 +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, ::MongoMapper::EmbeddedDocument]).empty?
22
+ if object.is_a?(Class) && !(object.ancestors & [::MongoMapper::Document,
23
+ ::MongoMapper::EmbeddedDocument]).empty?
23
24
  cast = :mongo_mapper_class
24
25
  elsif object.is_a?(::MongoMapper::Document) || object.is_a?(::MongoMapper::EmbeddedDocument)
25
26
  cast = :mongo_mapper_instance
@@ -36,9 +37,7 @@ module AmazingPrint
36
37
  # Format MongoMapper class object.
37
38
  #------------------------------------------------------------------------------
38
39
  def awesome_mongo_mapper_class(object)
39
- if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:keys)
40
- return object.inspect
41
- end
40
+ return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:keys)
42
41
 
43
42
  data = object.keys.sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
44
43
  hash[c.first] = (c.last.type || 'undefined').to_s.underscore.intern
@@ -86,9 +85,7 @@ module AmazingPrint
86
85
  end
87
86
 
88
87
  label = object.to_s
89
- if object.is_a?(::MongoMapper::EmbeddedDocument)
90
- label = "#{colorize('embedded', :assoc)} #{label}"
91
- end
88
+ label = "#{colorize('embedded', :assoc)} #{label}" if object.is_a?(::MongoMapper::EmbeddedDocument)
92
89
 
93
90
  [label, awesome_hash(data)].join(' ')
94
91
  end
@@ -31,9 +31,7 @@ module AmazingPrint
31
31
  # Format Mongoid class object.
32
32
  #------------------------------------------------------------------------------
33
33
  def awesome_mongoid_class(object)
34
- if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields)
35
- return object.inspect
36
- end
34
+ return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields)
37
35
 
38
36
  data = object.fields.sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
39
37
  hash[c[1].name.to_sym] = (c[1].type || 'undefined').to_s.underscore.intern
@@ -53,9 +51,7 @@ module AmazingPrint
53
51
  data = (object.attributes || {}).sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
54
52
  hash[c[0].to_sym] = c[1]
55
53
  end
56
- unless object.errors.empty?
57
- data = { errors: object.errors, attributes: data }
58
- end
54
+ data = { errors: object.errors, attributes: data } unless object.errors.empty?
59
55
  "#{object} #{awesome_hash(data)}"
60
56
  end
61
57
 
@@ -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 = Hash[object.fields.map do |field, options|
35
+ data = object.fields.map do |field, options|
36
36
  [field, (options[:type] || Object).to_s.underscore.to_sym]
37
- end]
37
+ end.to_h
38
38
 
39
39
  name = "class #{awesome_simple(object.to_s, :class)}"
40
40
  base = "< #{awesome_simple(object.superclass.to_s, :class)}"
@@ -46,9 +46,7 @@ module AmazingPrint
46
46
  #------------------------------------------------------------------------------
47
47
  def awesome_nobrainer_document(object)
48
48
  data = object.inspectable_attributes.symbolize_keys
49
- if object.errors.present?
50
- data = { errors: object.errors, attributes: data }
51
- end
49
+ data = { errors: object.errors, attributes: data } if object.errors.present?
52
50
  "#{object} #{awesome_hash(data)}"
53
51
  end
54
52
  end
@@ -34,9 +34,7 @@ module AmazingPrint
34
34
  xml.gsub!(%r{(<)(/?[A-Za-z1-9]+)}) { |_tag| "#{Regexp.last_match(1)}#{colorize(Regexp.last_match(2), :keyword)}" }
35
35
  xml.gsub!(/(id|class)="[^"]+"/i) { |id| colorize(id, :class) }
36
36
  xml.gsub!(/>([^<]+)</) do |contents|
37
- if contents && !contents.empty?
38
- contents = colorize(Regexp.last_match(1), :trueclass)
39
- end
37
+ contents = colorize(Regexp.last_match(1), :trueclass) if contents && !contents.empty?
40
38
  ">#{contents}<"
41
39
  end
42
40
  xml
@@ -14,9 +14,7 @@ module AmazingPrint
14
14
 
15
15
  def cast_with_ostruct(object, type)
16
16
  cast = cast_without_ostruct(object, type)
17
- if defined?(::OpenStruct) && object.is_a?(::OpenStruct)
18
- cast = :open_struct_instance
19
- end
17
+ cast = :open_struct_instance if defined?(::OpenStruct) && object.is_a?(::OpenStruct)
20
18
  cast
21
19
  end
22
20
 
@@ -18,9 +18,10 @@ module AmazingPrint
18
18
  cast = cast_without_ripple(object, type)
19
19
  return cast unless defined?(::Ripple)
20
20
 
21
- if object.is_a?(::Ripple::AttributeMethods) # Module used to access attributes across documents and embedded documents
21
+ case object
22
+ when ::Ripple::AttributeMethods # Module used to access attributes across documents and embedded documents
22
23
  cast = :ripple_document_instance
23
- elsif object.is_a?(::Ripple::Properties) # Used to access property metadata on Ripple classes
24
+ when ::Ripple::Properties # Used to access property metadata on Ripple classes
24
25
  cast = :ripple_document_class
25
26
  end
26
27
  cast
@@ -58,9 +59,7 @@ module AmazingPrint
58
59
  # Format Ripple class object.
59
60
  #------------------------------------------------------------------------------
60
61
  def awesome_ripple_document_class(object)
61
- if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:properties)
62
- return object.inspect
63
- end
62
+ return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:properties)
64
63
 
65
64
  name = "class #{awesome_simple(object.to_s, :class)}"
66
65
  base = "< #{awesome_simple(object.superclass.to_s, :class)}"
@@ -24,12 +24,11 @@ module AmazingPrint
24
24
  #------------------------------------------------------------------------------
25
25
  def format(object, type = nil)
26
26
  core_class = cast(object, type)
27
- awesome = if core_class != :self
28
- send(:"awesome_#{core_class}", object) # Core formatters.
29
- else
30
- awesome_self(object, type) # Catch all that falls back to object.inspect.
31
- end
32
- awesome
27
+ if core_class == :self
28
+ awesome_self(object, type) # Catch all that falls back to object.inspect.
29
+ else
30
+ send(:"awesome_#{core_class}", object) # Core formatters.
31
+ end
33
32
  end
34
33
 
35
34
  # Hook this when adding custom formatters. Check out lib/amazing_print/ext
@@ -8,13 +8,14 @@ module AmazingPrint
8
8
  attr_reader :array, :inspector, :options
9
9
 
10
10
  def initialize(array, inspector)
11
+ super()
11
12
  @array = array
12
13
  @inspector = inspector
13
14
  @options = inspector.options
14
15
  end
15
16
 
16
17
  def format
17
- if array.length.zero?
18
+ if array.empty?
18
19
  '[]'
19
20
  elsif methods_array?
20
21
  methods_array
@@ -33,7 +34,7 @@ module AmazingPrint
33
34
  if options[:multiline]
34
35
  multiline_array
35
36
  else
36
- '[ ' + array.map { |item| inspector.awesome(item) }.join(', ') + ' ]'
37
+ "[ #{array.map { |item| inspector.awesome(item) }.join(', ')} ]"
37
38
  end
38
39
  end
39
40
 
@@ -114,14 +115,14 @@ module AmazingPrint
114
115
 
115
116
  meth = begin
116
117
  object.method(name)
117
- rescue NameError, ArgumentError
118
- nil
118
+ rescue NameError, ArgumentError
119
+ nil
119
120
  end
120
121
 
121
122
  meth || begin
122
123
  object.instance_method(name)
123
- rescue NameError
124
- nil
124
+ rescue NameError
125
+ nil
125
126
  end
126
127
  end
127
128