awesome_print 1.9.2 → 2.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -1
  3. data/Appraisals +2 -33
  4. data/CHANGELOG.md +6 -15
  5. data/CONTRIBUTING.md +0 -1
  6. data/README.md +7 -49
  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/custom_defaults.rb +1 -1
  18. data/lib/awesome_print/formatter.rb +38 -106
  19. data/lib/awesome_print/formatters/array_formatter.rb +11 -8
  20. data/lib/awesome_print/formatters/base_formatter.rb +20 -53
  21. data/lib/awesome_print/formatters/bigdecimal_formatter.rb +20 -0
  22. data/lib/awesome_print/formatters/class_formatter.rb +5 -6
  23. data/lib/awesome_print/formatters/dir_formatter.rb +4 -6
  24. data/lib/awesome_print/formatters/ext/active_model_error_formatter.rb +44 -0
  25. data/lib/awesome_print/formatters/ext/active_record_attributeset_formatter.rb +27 -0
  26. data/lib/awesome_print/formatters/ext/active_record_class_formatter.rb +43 -0
  27. data/lib/awesome_print/formatters/ext/active_record_instance_formatter.rb +48 -0
  28. data/lib/awesome_print/formatters/ext/active_record_relation_formatter.rb +19 -0
  29. data/lib/awesome_print/formatters/ext/bson_objectid_formatter.rb +20 -0
  30. data/lib/awesome_print/formatters/ext/hash_with_indifferent_access_formatter.rb +20 -0
  31. data/lib/awesome_print/formatters/ext/mongoid_document_formatter.rb +59 -0
  32. data/lib/awesome_print/formatters/ext/nokogiri_document_formatter.rb +30 -0
  33. data/lib/awesome_print/formatters/ext/nokogiri_node_formatter.rb +29 -0
  34. data/lib/awesome_print/formatters/ext/nokogiri_nodeset_formatter.rb +32 -0
  35. data/lib/awesome_print/formatters/ext/sequel_dataset_formatter.rb +20 -0
  36. data/lib/awesome_print/formatters/ext/sequel_model_class_formatter.rb +25 -0
  37. data/lib/awesome_print/formatters/ext/sequel_model_formatter.rb +26 -0
  38. data/lib/awesome_print/formatters/ext/time_with_zone_formatter.rb +24 -0
  39. data/lib/awesome_print/formatters/fallback_formatter.rb +55 -0
  40. data/lib/awesome_print/formatters/false_class_formatter.rb +15 -0
  41. data/lib/awesome_print/formatters/file_formatter.rb +4 -6
  42. data/lib/awesome_print/formatters/hash_formatter.rb +12 -7
  43. data/lib/awesome_print/formatters/integer_formatter.rb +11 -0
  44. data/lib/awesome_print/formatters/method_formatter.rb +8 -6
  45. data/lib/awesome_print/formatters/module_formatter.rb +18 -0
  46. data/lib/awesome_print/formatters/nil_class_formatter.rb +19 -0
  47. data/lib/awesome_print/formatters/object_formatter.rb +19 -9
  48. data/lib/awesome_print/formatters/open_struct.rb +19 -0
  49. data/lib/awesome_print/formatters/range_formatter.rb +11 -0
  50. data/lib/awesome_print/formatters/rational_formatter.rb +15 -0
  51. data/lib/awesome_print/formatters/set_formatter.rb +19 -0
  52. data/lib/awesome_print/formatters/simple_formatter.rb +10 -8
  53. data/lib/awesome_print/formatters/string_formatter.rb +14 -0
  54. data/lib/awesome_print/formatters/struct_formatter.rb +8 -6
  55. data/lib/awesome_print/formatters/symbol_formatter.rb +18 -0
  56. data/lib/awesome_print/formatters/true_class_formatter.rb +15 -0
  57. data/lib/awesome_print/formatters/unbound_method_formatter.rb +15 -0
  58. data/lib/awesome_print/inspector.rb +18 -17
  59. data/lib/awesome_print/limiter.rb +63 -0
  60. data/lib/awesome_print/registrar.rb +20 -0
  61. data/lib/awesome_print/version.rb +6 -6
  62. data/spec/formatters/array_spec.rb +251 -0
  63. data/spec/formatters/big_decimal_spec.rb +28 -0
  64. data/spec/formatters/class_spec.rb +91 -0
  65. data/spec/formatters/dir_file_spec.rb +43 -0
  66. data/spec/{ext → formatters/ext}/active_record_spec.rb +14 -58
  67. data/spec/{ext → formatters/ext}/active_support_spec.rb +7 -11
  68. data/spec/{ext → formatters/ext}/mongoid_spec.rb +2 -2
  69. data/spec/{ext → formatters/ext}/nokogiri_spec.rb +0 -0
  70. data/spec/formatters/hash_spec.rb +284 -0
  71. data/spec/{methods_spec.rb → formatters/methods_spec.rb} +3 -5
  72. data/spec/{objects_spec.rb → formatters/objects_spec.rb} +0 -0
  73. data/spec/{ext → formatters}/ostruct_spec.rb +1 -0
  74. data/spec/formatters/set_spec.rb +49 -0
  75. data/spec/formatters/struct_spec.rb +61 -0
  76. data/spec/merge_options_spec.rb +13 -0
  77. data/spec/misc_spec.rb +3 -10
  78. data/spec/spec_helper.rb +2 -9
  79. data/spec/support/ext_verifier.rb +0 -15
  80. data/spec/support/mongoid_versions.rb +4 -8
  81. data/spec/support/rails_versions.rb +2 -32
  82. metadata +100 -78
  83. data/awesome_print.gemspec +0 -33
  84. data/init.rb +0 -1
  85. data/lib/awesome_print/core_ext/method.rb +0 -21
  86. data/lib/awesome_print/ext/action_view.rb +0 -22
  87. data/lib/awesome_print/ext/active_record.rb +0 -103
  88. data/lib/awesome_print/ext/active_support.rb +0 -47
  89. data/lib/awesome_print/ext/mongo_mapper.rb +0 -124
  90. data/lib/awesome_print/ext/mongoid.rb +0 -67
  91. data/lib/awesome_print/ext/nobrainer.rb +0 -52
  92. data/lib/awesome_print/ext/nokogiri.rb +0 -45
  93. data/lib/awesome_print/ext/ostruct.rb +0 -27
  94. data/lib/awesome_print/ext/ripple.rb +0 -71
  95. data/lib/awesome_print/ext/sequel.rb +0 -58
  96. data/spec/ext/action_view_spec.rb +0 -21
  97. data/spec/ext/mongo_mapper_spec.rb +0 -261
  98. data/spec/ext/nobrainer_spec.rb +0 -59
  99. data/spec/ext/ripple_spec.rb +0 -48
  100. data/spec/formats_spec.rb +0 -779
  101. data/spec/support/active_record_data/3_2_diana.txt +0 -24
  102. data/spec/support/active_record_data/3_2_diana_legacy.txt +0 -24
  103. data/spec/support/active_record_data/3_2_multi.txt +0 -50
  104. data/spec/support/active_record_data/3_2_multi_legacy.txt +0 -50
  105. data/spec/support/active_record_data/4_0_diana.txt +0 -98
  106. data/spec/support/active_record_data/4_0_multi.txt +0 -198
  107. data/spec/support/active_record_data/4_1_diana.txt +0 -97
  108. data/spec/support/active_record_data/4_1_multi.txt +0 -196
  109. data/spec/support/active_record_data/4_2_diana.txt +0 -109
  110. data/spec/support/active_record_data/4_2_diana_legacy.txt +0 -109
  111. data/spec/support/active_record_data/4_2_multi.txt +0 -220
  112. data/spec/support/active_record_data/4_2_multi_legacy.txt +0 -220
  113. data/spec/support/active_record_data/6_0_diana.txt +0 -104
  114. data/spec/support/active_record_data/6_0_multi.txt +0 -210
  115. data/spec/support/active_record_data/6_1_diana.txt +0 -109
  116. data/spec/support/active_record_data/6_1_multi.txt +0 -220
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: bc6d20d854c529b902cfe961bd8271b30f1aa9f82c4db11bfdcaa6ecb6b1f469
4
- data.tar.gz: c4f69e0cf2663cb4889b27414bd9f242789fea28790c3252dc5b2788da70bca2
2
+ SHA1:
3
+ metadata.gz: b78e2becdd2b08753b1e78e1c2c46e531f6750a2
4
+ data.tar.gz: 9e79ebd10b6b47e9026d0e412f83615466e67c89
5
5
  SHA512:
6
- metadata.gz: c34cd0b7347ecf6e98afc4ef0647f940a02a5d125c955748917c821e6bbf231830604fcbe0fb524c6bfdaa049d6f945bad658d30433bf878841b30a6d79ad765
7
- data.tar.gz: 9cfae5c9646b5194a52c87b1ea38ba3d711cb616a713e66ce0ab83f255293a4b75f4016f349f047aa43d775d14b9f024fc0edccdf40b9f489e46626908162b00
6
+ metadata.gz: 2b6b91f195831126213e10ffba034c1ed5f6be030b5bc89870aeead7c6edb95a39a3968d24c60f06c4d2c88e16b4c7f7974ee0b81e3b33c66de5e36ce916e4e9
7
+ data.tar.gz: 4bb762cc93185b014c5f56b8a4b1cccaa2a52277b88efe68c8cc4c85728db7fd9a7e6fd5c1f63677e865a34aa671c03a2239df5a6d91eb4fffb4dc83cb54703e
data/.gitignore CHANGED
@@ -26,7 +26,6 @@ Gemfile.lock
26
26
  /tmp
27
27
  /.bundle
28
28
  /gemfiles/.bundle
29
- /gemfiles/vendor/bundle
30
29
 
31
30
  ## PROJECT::RVM
32
31
  .rvmrc
@@ -34,3 +33,4 @@ Gemfile.lock
34
33
  # PROJECT::RBENV
35
34
  .ruby-gemset
36
35
  .awesome-print/
36
+ .byebug_history
data/Appraisals CHANGED
@@ -8,7 +8,6 @@
8
8
 
9
9
  appraise 'rails-5.0' do
10
10
  gem 'rails', '>= 5.0.0', '< 5.1'
11
- gem 'sqlite3', '~> 1.3.6'
12
11
  end
13
12
 
14
13
  appraise 'rails-5.1' do
@@ -19,44 +18,14 @@ appraise 'rails-5.2' do
19
18
  gem 'rails', '>= 5.2.0', '< 5.3'
20
19
  end
21
20
 
22
- appraise 'rails-6.0' do
23
- gem 'rails', '>= 6.0.0', '< 6.1'
24
- end
25
-
26
- appraise 'rails-6.1' do
27
- gem 'rails', '>= 6.1.0', '< 6.2'
21
+ appraise 'mongoid-4.0' do
22
+ gem 'mongoid', '~> 4.0.0'
28
23
  end
29
24
 
30
25
  appraise 'mongoid-5.0' do
31
26
  gem 'mongoid', '~> 5.0.0'
32
- gem 'bigdecimal', '~> 1.3.5'
33
27
  end
34
28
 
35
29
  appraise 'mongoid-6.0' do
36
30
  gem 'mongoid', '~> 6.0.0'
37
31
  end
38
-
39
- appraise 'mongoid-7.0' do
40
- gem 'mongoid', '~> 7.0.0'
41
- end
42
-
43
- appraise 'mongoid-7.1' do
44
- gem 'mongoid', '~> 7.1.0'
45
- end
46
-
47
- # appraise 'mongo_mapper' do
48
- # gem 'mongo_mapper'
49
- # end
50
- #
51
- # appraise 'ripple' do
52
- # gem 'tzinfo'
53
- # gem 'ripple'
54
- # end
55
- #
56
- # appraise 'nobrainer' do
57
- # gem 'nobrainer'
58
- #
59
- # # When activesupport 5 was released, it required ruby 2.2.2 as a minimum.
60
- # # Locking this down to 4.2.6 allows our Ruby 1.9 tests to keep working.
61
- # gem 'activesupport', '4.2.6', :platforms => :ruby_19
62
- # end
data/CHANGELOG.md CHANGED
@@ -1,19 +1,10 @@
1
1
  ## master (unreleased)
2
2
 
3
-
4
- ## 1.9.2
5
- - Tests work with Ruby 2.6.6, 2.7.2 and 3.0.0
6
- - Update awsome_print.gemspec nokogiri dependency to resolve CVEs [@gvwirth]
7
-
8
- ## 1.9.1
9
- - Updated code so it can be released to rubygems.
10
- - Updated nokogiri to resolve vulnerability scan
11
-
12
- ## 1.9.0
13
- - Update method signature after change in IRB [@febeling]
14
- - Fixes block and proc definition to work with Ruby 3.0.0 [@csalvato] - [#392]
15
- - Removed check for Ruby 2.4 and earlier [@bryanh]
16
- - Rails 5.0 builds are failing, because the sqlite3 version being installed is wrong version [#366]
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? :)
17
8
  - Fixes spec suite to properly work via travis, gets a clean build [@imajes, others]
18
9
  - Adds support for ActiveModel::Errors [@dshinzie] - [#301]
19
10
  - removes use of `strip_heredoc` from specs as it's a rails dep [@kstephens] - [#303]
@@ -23,7 +14,7 @@
23
14
  - stat("$HOME/.aprc") once [@kstephens] - [#304]
24
15
  - ActiveRecord: #joins now show the columns #select'ed [@adrianomitre] - [#211]
25
16
  - Handles NoMethodError for IRB implicit `ai` [@jtnegrotto] - [#212]
26
- - Replaced Fixnum reference with Integer
17
+ - Replaced Fixnum reference with Integer
27
18
  - Colorize ORM class names [@ixti]
28
19
 
29
20
  ## 1.7.0
data/CONTRIBUTING.md CHANGED
@@ -51,7 +51,6 @@ Special thanks goes to awesome team of contributors, namely:
51
51
  * Barry Allard -- https://github.com/steakknife
52
52
  * Benoit Daloze -- http://github.com/eregon
53
53
  * Brandon Zylstra -- https://github.com/brandondrew
54
- * Bryan Hanks, PMP -- https://github.com/bryanh
55
54
  * Dan Lynn -- https://github.com/danlynn
56
55
  * Daniel Johnson -- https://github.com/adhd360
57
56
  * Daniel Bretoi -- http://github.com/danielb2
data/README.md CHANGED
@@ -12,8 +12,9 @@ Awesome Print is a Ruby library that pretty prints Ruby objects in full color
12
12
  exposing their internal structure with proper indentation. Rails ActiveRecord
13
13
  objects and usage within Rails templates are supported via included mixins.
14
14
 
15
- __NOTE__: awesome_print v1.9.0 may not work on Ruby versions 2.4 or older or Rails versions 4.2 or older.
16
- The upcoming awesome_print v2.0 will *require* Ruby v1.9.3 or later and Rails v3.0 or later.
15
+ __NOTE__: awesome_print v1.2.0 is the last release supporting Ruby versions
16
+ prior to v1.9.3 and Rails versions prior to v3.0. The upcoming awesome_print
17
+ v2.0 will *require* Ruby v1.9.3 or later and Rails v3.0 or later.
17
18
 
18
19
  ### Installation ###
19
20
  # Installing as Ruby gem
@@ -83,8 +84,6 @@ irb> "awesome print".ai
83
84
 
84
85
  ### Examples ###
85
86
 
86
- #### Array
87
-
88
87
  ```ruby
89
88
  $ cat > 1.rb
90
89
  require "awesome_print"
@@ -105,10 +104,7 @@ $ ruby 1.rb
105
104
  :distance => 4.2e+43
106
105
  }
107
106
  ]
108
- ```
109
- #### Hash
110
107
 
111
- ```ruby
112
108
  $ cat > 2.rb
113
109
  require "awesome_print"
114
110
  data = { :now => Time.now, :class => Time.now.class, :distance => 42e42 }
@@ -120,11 +116,7 @@ $ ruby 2.rb
120
116
  :now => Fri Apr 02 19:55:53 -0700 2010,
121
117
  :distance => 4.2e+43
122
118
  }
123
- ```
124
119
 
125
- #### Nested array
126
-
127
- ```ruby
128
120
  $ cat > 3.rb
129
121
  require "awesome_print"
130
122
  data = [ false, 42, %w(forty two) ]
@@ -134,11 +126,6 @@ ap data, :multiline => false
134
126
  $ ruby 3.rb
135
127
  [ false, 42, [ "forty", "two" ], [...] ]
136
128
 
137
- ```
138
-
139
- #### Class methods
140
-
141
- ```ruby
142
129
  $ cat > 4.rb
143
130
  require "awesome_print"
144
131
  class Hello
@@ -152,11 +139,6 @@ $ ruby 4.rb
152
139
  [0] world(x, y, *z, &blk) Hello
153
140
  ]
154
141
 
155
- ```
156
-
157
- #### Object methods
158
-
159
- ```ruby
160
142
  $ cat > 5.rb
161
143
  require "awesome_print"
162
144
  ap (''.methods - Object.methods).grep(/!/)
@@ -185,11 +167,6 @@ $ ruby 5.rb
185
167
  [19] upcase!() String
186
168
  ]
187
169
 
188
- ```
189
-
190
- #### Compare output to value
191
-
192
- ```ruby
193
170
  $ cat > 6.rb
194
171
  require "awesome_print"
195
172
  ap 42 == ap(42)
@@ -197,13 +174,7 @@ ap 42 == ap(42)
197
174
  $ ruby 6.rb
198
175
  42
199
176
  true
200
-
201
- ```
202
-
203
- #### Array with default output limit
204
-
205
- ```ruby
206
- $ cat > 7.rb
177
+ $ cat 7.rb
207
178
  require "awesome_print"
208
179
  some_array = (1..1000).to_a
209
180
  ap some_array, :limit => true
@@ -218,12 +189,8 @@ $ ruby 7.rb
218
189
  [998] 999,
219
190
  [999] 1000
220
191
  ]
221
- ```
222
192
 
223
- #### Array with specific output limit
224
-
225
- ```ruby
226
- $ cat > 8.rb
193
+ $ cat 8.rb
227
194
  require "awesome_print"
228
195
  some_array = (1..1000).to_a
229
196
  ap some_array, :limit => 5
@@ -238,7 +205,7 @@ $ ruby 8.rb
238
205
  ]
239
206
  ```
240
207
 
241
- #### Rails console
208
+ ### Example (Rails console) ###
242
209
  ```ruby
243
210
  $ rails console
244
211
  rails> require "awesome_print"
@@ -340,7 +307,7 @@ With other web frameworks (ex: in Sinatra templates) you can explicitly request
340
307
  formatting:
341
308
 
342
309
  <%= ap @accounts.first, :html => true %>
343
-
310
+
344
311
  ### String Convenience Methods ###
345
312
  Use methods such as `.red` to set string color:
346
313
 
@@ -366,20 +333,11 @@ AwesomePrint.defaults = {
366
333
  ```
367
334
 
368
335
  ## Versioning
369
-
370
336
  AwesomePrint follows the [Semantic Versioning](http://semver.org/) standard.
371
337
 
372
338
  ### Contributing ###
373
339
  See [CONTRIBUTING.md](CONTRIBUTING.md) for information.
374
340
 
375
- ### License ###
376
- Copyright (c) 2010-2016 Michael Dvorkin and contributors
377
-
378
- http://www.dvorkin.net
379
-
380
- %w(mike dvorkin.net) * "@" || "twitter.com/mid"
381
-
382
- Released under the MIT license. See LICENSE file for details.
383
341
 
384
342
  [gem_version_badge]: https://img.shields.io/gem/v/awesome_print.svg?style=flat
385
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
+
data/lib/awesome_print.rb CHANGED
@@ -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:
@@ -27,7 +27,7 @@ module AwesomePrint
27
27
 
28
28
  def usual_rb
29
29
  IRB::Irb.class_eval do
30
- def output_value(*args)
30
+ def output_value
31
31
  ap @context.last_value
32
32
  rescue NoMethodError
33
33
  puts "(Object doesn't support #ai)"
@@ -1,134 +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_FORMATTERS = [: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_FORMATTERS.include?(type) ? type : :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
33
+ def format(object, type = nil)
34
+ puts "\n\n[FMT] #{type.to_s.red} >>> #{object}" if AwesomePrint.debug
89
35
 
90
- def awesome_method(m)
91
- Formatters::MethodFormatter.new(m, @inspector).format
92
- end
93
- alias :awesome_unboundmethod :awesome_method
36
+ format_with = active_formatter(type)
37
+ puts "[ACTIVE] using > #{format_with.to_s.blueish} < to format" if format_with && AwesomePrint.debug
94
38
 
95
- def awesome_class(c)
96
- Formatters::ClassFormatter.new(c, @inspector).format
97
- end
39
+ # if we have an active formatter, and it's good to go, lets return that
98
40
 
99
- def awesome_file(f)
100
- Formatters::FileFormatter.new(f, @inspector).format
101
- end
41
+ return format_with.new(@inspector).format(object) if format_with&.formattable?(object)
102
42
 
103
- def awesome_dir(d)
104
- Formatters::DirFormatter.new(d, @inspector).format
105
- 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
106
48
 
107
- # Utility methods.
108
- #------------------------------------------------------------------------------
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
53
+ end
109
54
 
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
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)
117
59
  end
118
60
 
119
- def convert_to_hash(object)
120
- return nil if has_method_accessor?(object)
121
- return nil if !object.respond_to?(:to_hash) || object.method(:to_hash).arity != 0
122
-
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?
127
-
128
- hash = object.to_hash
129
- return nil if !hash.respond_to?(:keys) || !hash.respond_to?(:[])
130
-
131
- hash
61
+ def active_formatter(type)
62
+ self.class.registered_formatters[type]
132
63
  end
133
64
  end
134
65
  end
66
+