awesome_print 1.8.0 → 2.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/Appraisals +16 -48
  4. data/CHANGELOG.md +10 -1
  5. data/LICENSE +16 -19
  6. data/README.md +3 -10
  7. data/lib/ap.rb +1 -5
  8. data/lib/awesome_print.rb +7 -29
  9. data/lib/awesome_print/colorize.rb +2 -0
  10. data/lib/awesome_print/core_ext/active_support.rb +7 -0
  11. data/lib/awesome_print/core_ext/awesome_method_array.rb +0 -6
  12. data/lib/awesome_print/core_ext/class.rb +0 -5
  13. data/lib/awesome_print/core_ext/kernel.rb +0 -5
  14. data/lib/awesome_print/core_ext/logger.rb +0 -5
  15. data/lib/awesome_print/core_ext/object.rb +0 -5
  16. data/lib/awesome_print/core_ext/string.rb +0 -5
  17. data/lib/awesome_print/formatter.rb +38 -98
  18. data/lib/awesome_print/formatters/array_formatter.rb +10 -7
  19. data/lib/awesome_print/formatters/base_formatter.rb +17 -50
  20. data/lib/awesome_print/formatters/bigdecimal_formatter.rb +20 -0
  21. data/lib/awesome_print/formatters/class_formatter.rb +5 -6
  22. data/lib/awesome_print/formatters/dir_formatter.rb +4 -6
  23. data/lib/awesome_print/formatters/ext/active_model_error_formatter.rb +44 -0
  24. data/lib/awesome_print/formatters/ext/active_record_attributeset_formatter.rb +27 -0
  25. data/lib/awesome_print/formatters/ext/active_record_class_formatter.rb +43 -0
  26. data/lib/awesome_print/formatters/ext/active_record_instance_formatter.rb +48 -0
  27. data/lib/awesome_print/formatters/ext/active_record_relation_formatter.rb +19 -0
  28. data/lib/awesome_print/formatters/ext/bson_objectid_formatter.rb +20 -0
  29. data/lib/awesome_print/formatters/ext/hash_with_indifferent_access_formatter.rb +20 -0
  30. data/lib/awesome_print/formatters/ext/mongoid_document_formatter.rb +59 -0
  31. data/lib/awesome_print/formatters/ext/nokogiri_document_formatter.rb +30 -0
  32. data/lib/awesome_print/formatters/ext/nokogiri_node_formatter.rb +29 -0
  33. data/lib/awesome_print/formatters/ext/nokogiri_nodeset_formatter.rb +32 -0
  34. data/lib/awesome_print/formatters/ext/sequel_dataset_formatter.rb +20 -0
  35. data/lib/awesome_print/formatters/ext/sequel_model_class_formatter.rb +25 -0
  36. data/lib/awesome_print/formatters/ext/sequel_model_formatter.rb +26 -0
  37. data/lib/awesome_print/formatters/ext/time_with_zone_formatter.rb +24 -0
  38. data/lib/awesome_print/formatters/fallback_formatter.rb +55 -0
  39. data/lib/awesome_print/formatters/false_class_formatter.rb +15 -0
  40. data/lib/awesome_print/formatters/file_formatter.rb +4 -6
  41. data/lib/awesome_print/formatters/hash_formatter.rb +12 -7
  42. data/lib/awesome_print/formatters/integer_formatter.rb +11 -0
  43. data/lib/awesome_print/formatters/method_formatter.rb +8 -6
  44. data/lib/awesome_print/formatters/module_formatter.rb +18 -0
  45. data/lib/awesome_print/formatters/nil_class_formatter.rb +19 -0
  46. data/lib/awesome_print/formatters/object_formatter.rb +20 -7
  47. data/lib/awesome_print/formatters/open_struct.rb +19 -0
  48. data/lib/awesome_print/formatters/range_formatter.rb +11 -0
  49. data/lib/awesome_print/formatters/rational_formatter.rb +15 -0
  50. data/lib/awesome_print/formatters/set_formatter.rb +19 -0
  51. data/lib/awesome_print/formatters/simple_formatter.rb +10 -8
  52. data/lib/awesome_print/formatters/string_formatter.rb +14 -0
  53. data/lib/awesome_print/formatters/struct_formatter.rb +8 -6
  54. data/lib/awesome_print/formatters/symbol_formatter.rb +18 -0
  55. data/lib/awesome_print/formatters/true_class_formatter.rb +15 -0
  56. data/lib/awesome_print/formatters/unbound_method_formatter.rb +15 -0
  57. data/lib/awesome_print/inspector.rb +20 -16
  58. data/lib/awesome_print/limiter.rb +63 -0
  59. data/lib/awesome_print/registrar.rb +20 -0
  60. data/lib/awesome_print/version.rb +6 -6
  61. data/spec/formatters/array_spec.rb +251 -0
  62. data/spec/formatters/big_decimal_spec.rb +28 -0
  63. data/spec/formatters/class_spec.rb +91 -0
  64. data/spec/formatters/dir_file_spec.rb +43 -0
  65. data/spec/{ext → formatters/ext}/active_record_spec.rb +27 -63
  66. data/spec/{ext → formatters/ext}/active_support_spec.rb +6 -6
  67. data/spec/{ext → formatters/ext}/mongoid_spec.rb +4 -41
  68. data/spec/{ext → formatters/ext}/nokogiri_spec.rb +0 -0
  69. data/spec/formatters/hash_spec.rb +284 -0
  70. data/spec/{methods_spec.rb → formatters/methods_spec.rb} +13 -7
  71. data/spec/{objects_spec.rb → formatters/objects_spec.rb} +49 -0
  72. data/spec/{ext → formatters}/ostruct_spec.rb +1 -0
  73. data/spec/formatters/set_spec.rb +49 -0
  74. data/spec/formatters/struct_spec.rb +61 -0
  75. data/spec/merge_options_spec.rb +13 -0
  76. data/spec/misc_spec.rb +21 -21
  77. data/spec/spec_helper.rb +5 -10
  78. data/spec/support/active_record_data/5_1_diana.txt +104 -0
  79. data/spec/support/active_record_data/5_1_multi.txt +210 -0
  80. data/spec/support/active_record_data/5_2_diana.txt +104 -0
  81. data/spec/support/active_record_data/5_2_multi.txt +210 -0
  82. data/spec/support/ext_verifier.rb +0 -15
  83. data/spec/support/mongoid_versions.rb +4 -4
  84. data/spec/support/rails_versions.rb +10 -20
  85. metadata +98 -87
  86. data/lib/awesome_print/core_ext/method.rb +0 -21
  87. data/lib/awesome_print/ext/action_view.rb +0 -22
  88. data/lib/awesome_print/ext/active_record.rb +0 -78
  89. data/lib/awesome_print/ext/active_support.rb +0 -47
  90. data/lib/awesome_print/ext/mongo_mapper.rb +0 -124
  91. data/lib/awesome_print/ext/mongoid.rb +0 -67
  92. data/lib/awesome_print/ext/nobrainer.rb +0 -52
  93. data/lib/awesome_print/ext/nokogiri.rb +0 -45
  94. data/lib/awesome_print/ext/ostruct.rb +0 -27
  95. data/lib/awesome_print/ext/ripple.rb +0 -71
  96. data/lib/awesome_print/ext/sequel.rb +0 -58
  97. data/spec/ext/action_view_spec.rb +0 -18
  98. data/spec/ext/mongo_mapper_spec.rb +0 -261
  99. data/spec/ext/nobrainer_spec.rb +0 -59
  100. data/spec/ext/ripple_spec.rb +0 -48
  101. data/spec/formats_spec.rb +0 -779
  102. data/spec/support/active_record_data/3_2_diana.txt +0 -24
  103. data/spec/support/active_record_data/3_2_diana_legacy.txt +0 -24
  104. data/spec/support/active_record_data/3_2_multi.txt +0 -50
  105. data/spec/support/active_record_data/3_2_multi_legacy.txt +0 -50
  106. data/spec/support/active_record_data/4_0_diana.txt +0 -98
  107. data/spec/support/active_record_data/4_0_multi.txt +0 -198
  108. data/spec/support/active_record_data/4_1_diana.txt +0 -97
  109. data/spec/support/active_record_data/4_1_multi.txt +0 -196
  110. data/spec/support/active_record_data/4_2_diana.txt +0 -109
  111. data/spec/support/active_record_data/4_2_diana_legacy.txt +0 -109
  112. data/spec/support/active_record_data/4_2_multi.txt +0 -220
  113. data/spec/support/active_record_data/4_2_multi_legacy.txt +0 -220
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cda3844883c76081eb1cbaf6e6188492b51d741d
4
- data.tar.gz: be404804711ff00ce4f3720153bf84ba10d012ef
3
+ metadata.gz: b78e2becdd2b08753b1e78e1c2c46e531f6750a2
4
+ data.tar.gz: 9e79ebd10b6b47e9026d0e412f83615466e67c89
5
5
  SHA512:
6
- metadata.gz: 3a83fe61488d2df035585c6d54f9e93dfa5c950bef2a7e05707fdb334090bf91db46921d88f2fac136edf13904ee0c76393e5a7c4d4d5f53cf9b460a0f724696
7
- data.tar.gz: 87d8c72172448c0897c668cbefcfe4713802effa219469297ce8b242307998c70f82a087feeb7f0b485ee34941bd833aa5eb14c322143242f09b8f3e7418bc5c
6
+ metadata.gz: 2b6b91f195831126213e10ffba034c1ed5f6be030b5bc89870aeead7c6edb95a39a3968d24c60f06c4d2c88e16b4c7f7974ee0b81e3b33c66de5e36ce916e4e9
7
+ data.tar.gz: 4bb762cc93185b014c5f56b8a4b1cccaa2a52277b88efe68c8cc4c85728db7fd9a7e6fd5c1f63677e865a34aa671c03a2239df5a6d91eb4fffb4dc83cb54703e
data/.gitignore CHANGED
@@ -33,3 +33,4 @@ Gemfile.lock
33
33
  # PROJECT::RBENV
34
34
  .ruby-gemset
35
35
  .awesome-print/
36
+ .byebug_history
data/Appraisals CHANGED
@@ -1,63 +1,31 @@
1
- appraise 'rails-3.2' do
2
- gem 'rails', '~> 3.2.0'
3
- end
4
-
5
- appraise 'rails-4.0' do
6
- gem 'rails', '~> 4.0.0'
7
- gem 'json', '~> 1.8', :platforms => :ruby_19 # Json 2.0 requires Ruby >= 2.0
8
-
9
- # The last version that doesn't need Ruby 2.0 and works with version 4.0 of
10
- # Rails. This addresses a build problem with Travis for version 1.9.3 of Ruby
11
- gem 'mime-types', '2.6.2', :platforms => :ruby_19
12
- end
13
-
14
- appraise 'rails-4.1' do
15
- gem 'rails', '~> 4.1.0'
16
-
17
- # The last version that doesn't need Ruby 2.0 and works with version 4.1 of
18
- # Rails. This addresses a build problem with Travis for version 1.9.3 of Ruby
19
- gem 'mime-types', '2.6.2', :platforms => :ruby_19
20
- end
21
-
22
- appraise 'rails-4.2' do
23
- gem 'rails', '~> 4.2.0'
24
-
25
- # The last version that doesn't need Ruby 2.0 and works with version 4.2 of
26
- # Rails. This addresses a build problem with Travis for version 1.9.3 of Ruby
27
- gem 'mime-types', '2.6.2', :platforms => :ruby_19
28
- end
1
+ # appraise 'rails-4.2' do
2
+ # gem 'rails', '~> 4.2.0'
3
+ #
4
+ # # The last version that doesn't need Ruby 2.0 and works with version 4.2 of
5
+ # # Rails. This addresses a build problem with Travis for version 1.9.3 of Ruby
6
+ # gem 'mime-types', '2.6.2', :platforms => :ruby_19
7
+ # end
29
8
 
30
9
  appraise 'rails-5.0' do
31
- # Only works with Ruby >= 2.2
32
- gem 'rails', '>= 5.0.0.racecar1', '< 5.1'
10
+ gem 'rails', '>= 5.0.0', '< 5.1'
33
11
  end
34
12
 
35
- appraise 'mongoid-3.0' do
36
- gem 'mongoid', '~> 3.0.0'
13
+ appraise 'rails-5.1' do
14
+ gem 'rails', '>= 5.1.0', '< 5.2'
37
15
  end
38
16
 
39
- appraise 'mongoid-3.1' do
40
- gem 'mongoid', '~> 3.1.0'
41
- gem 'json', '~> 1.8', :platforms => :ruby_19 # Json 2.0 requires Ruby >= 2.0
17
+ appraise 'rails-5.2' do
18
+ gem 'rails', '>= 5.2.0', '< 5.3'
42
19
  end
43
20
 
44
21
  appraise 'mongoid-4.0' do
45
22
  gem 'mongoid', '~> 4.0.0'
46
23
  end
47
24
 
48
- appraise 'mongo_mapper' do
49
- gem 'mongo_mapper'
25
+ appraise 'mongoid-5.0' do
26
+ gem 'mongoid', '~> 5.0.0'
50
27
  end
51
28
 
52
- appraise 'ripple' do
53
- gem 'tzinfo'
54
- gem 'ripple'
55
- end
56
-
57
- appraise 'nobrainer' do
58
- gem 'nobrainer'
59
-
60
- # When activesupport 5 was released, it required ruby 2.2.2 as a minimum.
61
- # Locking this down to 4.2.6 allows our Ruby 1.9 tests to keep working.
62
- gem 'activesupport', '4.2.6', :platforms => :ruby_19
29
+ appraise 'mongoid-6.0' do
30
+ gem 'mongoid', '~> 6.0.0'
63
31
  end
@@ -1,5 +1,14 @@
1
1
  ## master (unreleased)
2
- - new things!
2
+
3
+ ## 2.0.0-pre
4
+ - Massive overhaul in the way formatters work. Now they are registered with a factory pattern, which includes
5
+ a simple method to determine if that formatter can handle the content. [@imajes]
6
+ - Remove support for mongo mapper, and ruby pre 2.x [@imajes]
7
+ - It's probably faster? :)
8
+ - Fixes spec suite to properly work via travis, gets a clean build [@imajes, others]
9
+ - Adds support for ActiveModel::Errors [@dshinzie] - [#301]
10
+ - removes use of `strip_heredoc` from specs as it's a rails dep [@kstephens] - [#303]
11
+ - ArrayFormatter now returns arrays for has_many :through associations [@chadh13] - [#332]
3
12
 
4
13
  ## 1.8.0
5
14
  - stat("$HOME/.aprc") once [@kstephens] - [#304]
data/LICENSE CHANGED
@@ -1,22 +1,19 @@
1
- Copyright (c) 2010-2013 Michael Dvorkin
2
- http://www.dvorkin.net
3
- %w(mike dvorkin.net) * "@" || "twitter.com/mid"
1
+ Copyright (c) 2010-2019 Michael Dvorkin
4
2
 
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
12
9
 
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
15
12
 
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README.md CHANGED
@@ -43,6 +43,8 @@ sort_keys: false, # Do not sort hash keys.
43
43
  sort_vars: true, # Sort instance variables.
44
44
  limit: false, # Limit arrays & hashes. Accepts bool or int.
45
45
  ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output.
46
+ class_name: :class, # Method called to report the instance class name. (e.g. :to_s)
47
+ object_id: true, # Show object id.
46
48
  color: {
47
49
  args: :pale,
48
50
  array: :white,
@@ -50,7 +52,7 @@ color: {
50
52
  class: :yellow,
51
53
  date: :greenish,
52
54
  falseclass: :red,
53
- fixnum: :blue,
55
+ integer: :blue,
54
56
  float: :blue,
55
57
  hash: :pale,
56
58
  keyword: :cyan,
@@ -331,20 +333,11 @@ AwesomePrint.defaults = {
331
333
  ```
332
334
 
333
335
  ## Versioning
334
-
335
336
  AwesomePrint follows the [Semantic Versioning](http://semver.org/) standard.
336
337
 
337
338
  ### Contributing ###
338
339
  See [CONTRIBUTING.md](CONTRIBUTING.md) for information.
339
340
 
340
- ### License ###
341
- Copyright (c) 2010-2016 Michael Dvorkin and contributors
342
-
343
- http://www.dvorkin.net
344
-
345
- %w(mike dvorkin.net) * "@" || "twitter.com/mid"
346
-
347
- Released under the MIT license. See LICENSE file for details.
348
341
 
349
342
  [gem_version_badge]: https://img.shields.io/gem/v/awesome_print.svg?style=flat
350
343
  [gem_downloads_badge]: http://img.shields.io/gem/dt/awesome_print.svg?style=flat
data/lib/ap.rb CHANGED
@@ -1,10 +1,6 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
- #
3
- # Awesome Print is freely distributable under the terms of MIT license.
4
- # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
- #------------------------------------------------------------------------------
6
1
  #
7
2
  # Keeping this for backwards compatibility to allow
8
3
  # require "ap"
9
4
  #
10
5
  require File.dirname(__FILE__) + '/awesome_print'
6
+
@@ -1,41 +1,19 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
- #
3
- # Awesome Print is freely distributable under the terms of MIT license.
4
- # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
- #------------------------------------------------------------------------------
6
- #
7
1
  # AwesomePrint might be loaded implicitly through ~/.irbrc or ~/.pryrc
8
2
  # so do nothing for subsequent requires.
9
3
  #
10
4
  unless defined?(AwesomePrint::Inspector)
11
- %w(awesome_method_array string method object class kernel).each do |file|
5
+ # FIXME: not sure we need these, but..
6
+ require 'awesome_print/custom_defaults'
7
+
8
+ %w(active_support awesome_method_array string object class kernel).each do |file|
12
9
  require "awesome_print/core_ext/#{file}"
13
10
  end
14
11
 
15
- require 'awesome_print/custom_defaults'
16
12
  require 'awesome_print/inspector'
17
13
  require 'awesome_print/formatter'
14
+
15
+ Dir["./lib/awesome_print/formatters/**/*.rb"].each { |f| require f }
16
+
18
17
  require 'awesome_print/version'
19
18
  require 'awesome_print/core_ext/logger' if defined?(Logger)
20
- #
21
- # Load the following under normal circumstances as well as in Rails
22
- # console when required from ~/.irbrc or ~/.pryrc.
23
- #
24
- require 'awesome_print/ext/active_record' if defined?(ActiveRecord) || AwesomePrint.rails_console?
25
- require 'awesome_print/ext/active_support' if defined?(ActiveSupport) || AwesomePrint.rails_console?
26
- #
27
- # Load remaining extensions.
28
- #
29
- if defined?(ActiveSupport.on_load)
30
- ActiveSupport.on_load(:action_view) do
31
- require 'awesome_print/ext/action_view'
32
- end
33
- end
34
- require 'awesome_print/ext/mongo_mapper' if defined?(MongoMapper)
35
- require 'awesome_print/ext/mongoid' if defined?(Mongoid)
36
- require 'awesome_print/ext/nokogiri' if defined?(Nokogiri)
37
- require 'awesome_print/ext/nobrainer' if defined?(NoBrainer)
38
- require 'awesome_print/ext/ripple' if defined?(Ripple)
39
- require 'awesome_print/ext/sequel' if defined?(Sequel)
40
- require 'awesome_print/ext/ostruct' if defined?(OpenStruct)
41
19
  end
@@ -6,6 +6,8 @@ module AwesomePrint
6
6
  # Pick the color and apply it to the given string as necessary.
7
7
  #------------------------------------------------------------------------------
8
8
  def colorize(str, type)
9
+ # puts "[COLORIZING] - using #{options[:color][type]} for #{type}" if AwesomePrint.debug
10
+
9
11
  str = CGI.escapeHTML(str) if options[:html]
10
12
  if options[:plain] || !options[:color][type] || !inspector.colorize?
11
13
  str
@@ -0,0 +1,7 @@
1
+ #
2
+ # Colorize Rails logs.
3
+ #
4
+ if defined?(ActiveSupport::LogSubscriber)
5
+ AwesomePrint.force_colors! ActiveSupport::LogSubscriber.colorize_logging
6
+ end
7
+
@@ -1,9 +1,3 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
- #
3
- # Awesome Print is freely distributable under the terms of MIT license.
4
- # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
- #------------------------------------------------------------------------------
6
- #
7
1
  # The following makes it possible to invoke awesome_print while performing
8
2
  # operations on method arrays, ex:
9
3
  #
@@ -1,8 +1,3 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
- #
3
- # Awesome Print is freely distributable under the terms of MIT license.
4
- # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
- #------------------------------------------------------------------------------
6
1
  class Class #:nodoc:
7
2
  #
8
3
  # Intercept methods below to inject @__awesome_print__ instance variable
@@ -1,8 +1,3 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
- #
3
- # Awesome Print is freely distributable under the terms of MIT license.
4
- # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
- #------------------------------------------------------------------------------
6
1
  module Kernel
7
2
 
8
3
  def ai(options = {})
@@ -1,8 +1,3 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
- #
3
- # Awesome Print is freely distributable under the terms of MIT license.
4
- # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
- #------------------------------------------------------------------------------
6
1
  module AwesomePrint
7
2
  module Logger
8
3
 
@@ -1,8 +1,3 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
- #
3
- # Awesome Print is freely distributable under the terms of MIT license.
4
- # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
- #------------------------------------------------------------------------------
6
1
  class Object #:nodoc:
7
2
  #
8
3
  # Intercept methods below to inject @__awesome_print__ instance variable
@@ -1,8 +1,3 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
- #
3
- # Awesome Print is freely distributable under the terms of MIT license.
4
- # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
- #------------------------------------------------------------------------------
6
1
  class String
7
2
  #
8
3
  # ANSI color codes:
@@ -1,126 +1,66 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
- #
3
- # Awesome Print is freely distributable under the terms of MIT license.
4
- # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
- #------------------------------------------------------------------------------
6
- require 'awesome_print/formatters'
1
+ require_relative 'colorize'
7
2
 
8
3
  module AwesomePrint
9
4
  class Formatter
5
+
10
6
  include Colorize
11
7
 
12
8
  attr_reader :inspector, :options
13
9
 
14
- CORE = [:array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod]
10
+ # Acts as a class ivar
11
+ @registered_formatters = {}
15
12
 
16
- def initialize(inspector)
17
- @inspector = inspector
18
- @options = inspector.options
13
+ # make it accessible
14
+ def self.registered_formatters
15
+ @registered_formatters
19
16
  end
20
17
 
21
- # Main entry point to format an object.
18
+ # register a new formatter..
22
19
  #------------------------------------------------------------------------------
23
- def format(object, type = nil)
24
- core_class = cast(object, type)
25
- awesome = if core_class != :self
26
- send(:"awesome_#{core_class}", object) # Core formatters.
27
- else
28
- awesome_self(object, type) # Catch all that falls back to object.inspect.
29
- end
30
- awesome
20
+ def self.register(formatter)
21
+ @registered_formatters[formatter.formatted_object_type.to_sym] = formatter
31
22
  end
32
23
 
33
- # Hook this when adding custom formatters. Check out lib/awesome_print/ext
34
- # directory for custom formatters that ship with awesome_print.
35
- #------------------------------------------------------------------------------
36
- def cast(object, type)
37
- CORE.grep(type)[0] || :self
24
+ def initialize(inspector)
25
+ @inspector = inspector
26
+ @options = inspector.options
38
27
  end
39
28
 
40
- private
41
29
 
42
- # Catch all method to format an arbitrary object.
30
+ # Main entry point to format an object.
31
+ # type is determined by Inspector#printable
43
32
  #------------------------------------------------------------------------------
44
- def awesome_self(object, type)
45
- if @options[:raw] && object.instance_variables.any?
46
- awesome_object(object)
47
- elsif (hash = convert_to_hash(object))
48
- awesome_hash(hash)
49
- else
50
- awesome_simple(object.inspect.to_s, type, @inspector)
51
- end
52
- end
53
-
54
- def awesome_bigdecimal(n)
55
- o = n.to_s('F')
56
- type = :bigdecimal
57
- awesome_simple(o, type, @inspector)
58
- end
59
-
60
- def awesome_rational(n)
61
- o = n.to_s
62
- type = :rational
63
- awesome_simple(o, type, @inspector)
64
- end
65
-
66
- def awesome_simple(o, type, inspector = @inspector)
67
- AwesomePrint::Formatters::SimpleFormatter.new(o, type, inspector).format
68
- end
69
-
70
- def awesome_array(a)
71
- Formatters::ArrayFormatter.new(a, @inspector).format
72
- end
73
-
74
- def awesome_set(s)
75
- Formatters::ArrayFormatter.new(s.to_a, @inspector).format
76
- end
77
-
78
- def awesome_hash(h)
79
- Formatters::HashFormatter.new(h, @inspector).format
80
- end
81
-
82
- def awesome_object(o)
83
- Formatters::ObjectFormatter.new(o, @inspector).format
84
- end
85
-
86
- def awesome_struct(s)
87
- Formatters::StructFormatter.new(s, @inspector).format
88
- end
89
-
90
- def awesome_method(m)
91
- Formatters::MethodFormatter.new(m, @inspector).format
92
- end
93
- alias :awesome_unboundmethod :awesome_method
33
+ def format(object, type = nil)
34
+ puts "\n\n[FMT] #{type.to_s.red} >>> #{object}" if AwesomePrint.debug
94
35
 
95
- def awesome_class(c)
96
- Formatters::ClassFormatter.new(c, @inspector).format
97
- end
36
+ format_with = active_formatter(type)
37
+ puts "[ACTIVE] using > #{format_with.to_s.blueish} < to format" if format_with && AwesomePrint.debug
98
38
 
99
- def awesome_file(f)
100
- Formatters::FileFormatter.new(f, @inspector).format
101
- end
39
+ # if we have an active formatter, and it's good to go, lets return that
102
40
 
103
- def awesome_dir(d)
104
- Formatters::DirFormatter.new(d, @inspector).format
105
- end
41
+ return format_with.new(@inspector).format(object) if format_with&.formattable?(object)
106
42
 
107
- # Utility methods.
108
- #------------------------------------------------------------------------------
109
- def convert_to_hash(object)
110
- if !object.respond_to?(:to_hash)
111
- return nil
112
- end
43
+ # if that's not working, lets try discover the format via formattable?
44
+ self.class.registered_formatters.each do |fmt|
45
+ puts "[FIND] trying to use [#{fmt.first.to_s.greenish} - #{fmt.last.to_s.blue}] - core: #{fmt.last.core?}" if AwesomePrint.debug
46
+ #{fmt.last.core?}" if AwesomePrint.debug
47
+ next if fmt.last.core? # if it's a core level formatter, move on
113
48
 
114
- if object.method(:to_hash).arity != 0
115
- return nil
49
+ if fmt.last.formattable?(object)
50
+ puts "[FMT] Jackpot! using #{fmt.first.to_s.red} >>> #{object}" if AwesomePrint.debug
51
+ return fmt.last.new(@inspector).format(object)
52
+ end
116
53
  end
117
54
 
118
- hash = object.to_hash
119
- if !hash.respond_to?(:keys) || !hash.respond_to?('[]')
120
- return nil
121
- end
55
+ # we've run out of options. lets try and coerce into something we can work
56
+ # with
57
+ puts "[FALLBACK] well darn, we're just gonna have to fb" if AwesomePrint.debug
58
+ AwesomePrint::Formatters::FallbackFormatter.new(@inspector).format(object)
59
+ end
122
60
 
123
- return hash
61
+ def active_formatter(type)
62
+ self.class.registered_formatters[type]
124
63
  end
125
64
  end
126
65
  end
66
+