awesome_print 1.6.1 → 1.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +4 -0
  3. data/Appraisals +45 -22
  4. data/{CHANGELOG → CHANGELOG.md} +83 -18
  5. data/CONTRIBUTING.md +45 -4
  6. data/Gemfile.lock +28 -22
  7. data/LICENSE +16 -19
  8. data/README.md +97 -92
  9. data/Rakefile +5 -5
  10. data/awesome_print.gemspec +33 -0
  11. data/init.rb +1 -0
  12. data/lib/ap.rb +2 -2
  13. data/lib/awesome_print/colorize.rb +24 -0
  14. data/lib/awesome_print/core_ext/{array.rb → awesome_method_array.rb} +16 -18
  15. data/lib/awesome_print/core_ext/class.rb +3 -2
  16. data/lib/awesome_print/core_ext/kernel.rb +1 -1
  17. data/lib/awesome_print/core_ext/logger.rb +1 -1
  18. data/lib/awesome_print/core_ext/method.rb +2 -2
  19. data/lib/awesome_print/core_ext/object.rb +3 -2
  20. data/lib/awesome_print/core_ext/string.rb +3 -3
  21. data/lib/awesome_print/custom_defaults.rb +57 -0
  22. data/lib/awesome_print/ext/action_view.rb +8 -4
  23. data/lib/awesome_print/ext/active_record.rb +45 -12
  24. data/lib/awesome_print/ext/active_support.rb +1 -1
  25. data/lib/awesome_print/ext/mongo_mapper.rb +16 -13
  26. data/lib/awesome_print/ext/mongoid.rb +8 -6
  27. data/lib/awesome_print/ext/nobrainer.rb +52 -0
  28. data/lib/awesome_print/ext/nokogiri.rb +4 -4
  29. data/lib/awesome_print/ext/ostruct.rb +1 -1
  30. data/lib/awesome_print/ext/ripple.rb +5 -6
  31. data/lib/awesome_print/ext/sequel.rb +7 -6
  32. data/lib/awesome_print/formatter.rb +54 -327
  33. data/lib/awesome_print/formatters/array_formatter.rb +139 -0
  34. data/lib/awesome_print/formatters/base_formatter.rb +140 -0
  35. data/lib/awesome_print/formatters/class_formatter.rb +25 -0
  36. data/lib/awesome_print/formatters/dir_formatter.rb +22 -0
  37. data/lib/awesome_print/formatters/file_formatter.rb +22 -0
  38. data/lib/awesome_print/formatters/hash_formatter.rb +106 -0
  39. data/lib/awesome_print/formatters/method_formatter.rb +22 -0
  40. data/lib/awesome_print/formatters/object_formatter.rb +78 -0
  41. data/lib/awesome_print/formatters/simple_formatter.rb +21 -0
  42. data/lib/awesome_print/formatters/struct_formatter.rb +71 -0
  43. data/lib/awesome_print/formatters.rb +15 -0
  44. data/lib/awesome_print/indentator.rb +18 -0
  45. data/lib/awesome_print/inspector.rb +92 -94
  46. data/lib/awesome_print/version.rb +2 -2
  47. data/lib/awesome_print.rb +19 -18
  48. data/spec/active_record_helper.rb +24 -28
  49. data/spec/colors_spec.rb +31 -31
  50. data/spec/core_ext/logger_spec.rb +43 -0
  51. data/spec/core_ext/string_spec.rb +20 -0
  52. data/spec/ext/action_view_spec.rb +21 -0
  53. data/spec/ext/active_record_spec.rb +260 -0
  54. data/spec/ext/active_support_spec.rb +30 -0
  55. data/spec/ext/mongo_mapper_spec.rb +261 -0
  56. data/spec/ext/mongoid_spec.rb +67 -0
  57. data/spec/ext/nobrainer_spec.rb +59 -0
  58. data/spec/ext/nokogiri_spec.rb +46 -0
  59. data/spec/ext/ostruct_spec.rb +22 -0
  60. data/spec/ext/ripple_spec.rb +48 -0
  61. data/spec/formats_spec.rb +251 -184
  62. data/spec/methods_spec.rb +126 -130
  63. data/spec/misc_spec.rb +98 -93
  64. data/spec/objects_spec.rb +162 -27
  65. data/spec/spec_helper.rb +72 -38
  66. data/spec/support/active_record_data/3_2_diana.txt +24 -0
  67. data/spec/support/active_record_data/3_2_diana_legacy.txt +24 -0
  68. data/spec/support/active_record_data/3_2_multi.txt +50 -0
  69. data/spec/support/active_record_data/3_2_multi_legacy.txt +50 -0
  70. data/spec/support/active_record_data/4_0_diana.txt +98 -0
  71. data/spec/support/active_record_data/4_0_multi.txt +198 -0
  72. data/spec/support/active_record_data/4_1_diana.txt +97 -0
  73. data/spec/support/active_record_data/4_1_multi.txt +196 -0
  74. data/spec/support/active_record_data/4_2_diana.txt +109 -0
  75. data/spec/support/active_record_data/4_2_diana_legacy.txt +109 -0
  76. data/spec/support/active_record_data/4_2_multi.txt +220 -0
  77. data/spec/support/active_record_data/4_2_multi_legacy.txt +220 -0
  78. data/spec/support/active_record_data/5_0_diana.txt +105 -0
  79. data/spec/support/active_record_data/5_0_multi.txt +212 -0
  80. data/spec/support/active_record_data/5_1_diana.txt +104 -0
  81. data/spec/support/active_record_data/5_1_multi.txt +210 -0
  82. data/spec/support/active_record_data/5_2_diana.txt +104 -0
  83. data/spec/support/active_record_data/5_2_multi.txt +210 -0
  84. data/spec/support/active_record_data/6_0_diana.txt +104 -0
  85. data/spec/support/active_record_data/6_0_multi.txt +210 -0
  86. data/spec/support/active_record_data/6_1_diana.txt +109 -0
  87. data/spec/support/active_record_data/6_1_multi.txt +220 -0
  88. data/spec/support/active_record_data.rb +20 -0
  89. data/spec/support/ext_verifier.rb +42 -0
  90. data/spec/support/mongoid_versions.rb +26 -0
  91. data/spec/support/rails_versions.rb +55 -0
  92. metadata +104 -15
  93. data/lib/awesome_print/ext/no_brainer.rb +0 -58
data/README.md CHANGED
@@ -3,24 +3,24 @@
3
3
  [![RubyGems][gem_version_badge]][ruby_gems]
4
4
  [![Travis CI][travis_ci_badge]][travis_ci]
5
5
  [![Code Climate][code_climate_badge]][code_climate]
6
+ [![Code Climate Coverage][code_climate_coverage_badge]][code_climate]
6
7
  [![RubyGems][gem_downloads_badge]][ruby_gems]
7
- [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/michaeldv/awesome_print?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
8
+ [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/awesome-print/awesome_print?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
8
9
 
9
10
 
10
11
  Awesome Print is a Ruby library that pretty prints Ruby objects in full color
11
12
  exposing their internal structure with proper indentation. Rails ActiveRecord
12
13
  objects and usage within Rails templates are supported via included mixins.
13
14
 
14
- __NOTE__: awesome_print v1.2.0 is the last release supporting Ruby versions
15
- prior to v1.9.3 and Rails versions prior to v3.0. The upcoming awesome_print
16
- v2.0 will *require* Ruby v1.9.3 or later and Rails v3.0 or later.
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.
17
17
 
18
18
  ### Installation ###
19
19
  # Installing as Ruby gem
20
20
  $ gem install awesome_print
21
21
 
22
22
  # Cloning the repository
23
- $ git clone git://github.com/michaeldv/awesome_print.git
23
+ $ git clone git://github.com/awesome-print/awesome_print.git
24
24
 
25
25
  ### Usage ###
26
26
 
@@ -32,34 +32,38 @@ ap object, options = {}
32
32
  Default options:
33
33
 
34
34
  ```ruby
35
- :indent => 4, # Indent using 4 spaces.
36
- :index => true, # Display array indices.
37
- :html => false, # Use ANSI color codes rather than HTML.
38
- :multiline => true, # Display in multiple lines.
39
- :plain => false, # Use colors.
40
- :raw => false, # Do not recursively format object instance variables.
41
- :sort_keys => false, # Do not sort hash keys.
42
- :limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer.
43
- :color => {
44
- :args => :pale,
45
- :array => :white,
46
- :bigdecimal => :blue,
47
- :class => :yellow,
48
- :date => :greenish,
49
- :falseclass => :red,
50
- :fixnum => :blue,
51
- :float => :blue,
52
- :hash => :pale,
53
- :keyword => :cyan,
54
- :method => :purpleish,
55
- :nilclass => :red,
56
- :rational => :blue,
57
- :string => :yellowish,
58
- :struct => :pale,
59
- :symbol => :cyanish,
60
- :time => :greenish,
61
- :trueclass => :green,
62
- :variable => :cyanish
35
+ indent: 4, # Number of spaces for indenting.
36
+ index: true, # Display array indices.
37
+ html: false, # Use ANSI color codes rather than HTML.
38
+ multiline: true, # Display in multiple lines.
39
+ plain: false, # Use colors.
40
+ raw: false, # Do not recursively format instance variables.
41
+ sort_keys: false, # Do not sort hash keys.
42
+ sort_vars: true, # Sort instance variables.
43
+ limit: false, # Limit arrays & hashes. Accepts bool or int.
44
+ ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output.
45
+ class_name: :class, # Method called to report the instance class name. (e.g. :to_s)
46
+ object_id: true, # Show object id.
47
+ color: {
48
+ args: :pale,
49
+ array: :white,
50
+ bigdecimal: :blue,
51
+ class: :yellow,
52
+ date: :greenish,
53
+ falseclass: :red,
54
+ integer: :blue,
55
+ float: :blue,
56
+ hash: :pale,
57
+ keyword: :cyan,
58
+ method: :purpleish,
59
+ nilclass: :red,
60
+ rational: :blue,
61
+ string: :yellowish,
62
+ struct: :pale,
63
+ symbol: :cyanish,
64
+ time: :greenish,
65
+ trueclass: :green,
66
+ variable: :cyanish
63
67
  }
64
68
  ```
65
69
 
@@ -70,8 +74,17 @@ Supported color names:
70
74
  :black, :redish, :greenish, :yellowish, :blueish, :purpleish, :cyanish, :pale
71
75
  ```
72
76
 
77
+ Use `Object#ai` to return an ASCII encoded string:
78
+
79
+ ```ruby
80
+ irb> "awesome print".ai
81
+ => "\e[0;33m\"awesome print\"\e[0m"
82
+ ```
83
+
73
84
  ### Examples ###
74
85
 
86
+ #### Array
87
+
75
88
  ```ruby
76
89
  $ cat > 1.rb
77
90
  require "awesome_print"
@@ -92,7 +105,10 @@ $ ruby 1.rb
92
105
  :distance => 4.2e+43
93
106
  }
94
107
  ]
108
+ ```
109
+ #### Hash
95
110
 
111
+ ```ruby
96
112
  $ cat > 2.rb
97
113
  require "awesome_print"
98
114
  data = { :now => Time.now, :class => Time.now.class, :distance => 42e42 }
@@ -104,7 +120,11 @@ $ ruby 2.rb
104
120
  :now => Fri Apr 02 19:55:53 -0700 2010,
105
121
  :distance => 4.2e+43
106
122
  }
123
+ ```
124
+
125
+ #### Nested array
107
126
 
127
+ ```ruby
108
128
  $ cat > 3.rb
109
129
  require "awesome_print"
110
130
  data = [ false, 42, %w(forty two) ]
@@ -114,6 +134,11 @@ ap data, :multiline => false
114
134
  $ ruby 3.rb
115
135
  [ false, 42, [ "forty", "two" ], [...] ]
116
136
 
137
+ ```
138
+
139
+ #### Class methods
140
+
141
+ ```ruby
117
142
  $ cat > 4.rb
118
143
  require "awesome_print"
119
144
  class Hello
@@ -127,6 +152,11 @@ $ ruby 4.rb
127
152
  [0] world(x, y, *z, &blk) Hello
128
153
  ]
129
154
 
155
+ ```
156
+
157
+ #### Object methods
158
+
159
+ ```ruby
130
160
  $ cat > 5.rb
131
161
  require "awesome_print"
132
162
  ap (''.methods - Object.methods).grep(/!/)
@@ -155,6 +185,11 @@ $ ruby 5.rb
155
185
  [19] upcase!() String
156
186
  ]
157
187
 
188
+ ```
189
+
190
+ #### Compare output to value
191
+
192
+ ```ruby
158
193
  $ cat > 6.rb
159
194
  require "awesome_print"
160
195
  ap 42 == ap(42)
@@ -162,7 +197,13 @@ ap 42 == ap(42)
162
197
  $ ruby 6.rb
163
198
  42
164
199
  true
165
- $ cat 7.rb
200
+
201
+ ```
202
+
203
+ #### Array with default output limit
204
+
205
+ ```ruby
206
+ $ cat > 7.rb
166
207
  require "awesome_print"
167
208
  some_array = (1..1000).to_a
168
209
  ap some_array, :limit => true
@@ -177,8 +218,12 @@ $ ruby 7.rb
177
218
  [998] 999,
178
219
  [999] 1000
179
220
  ]
221
+ ```
180
222
 
181
- $ cat 8.rb
223
+ #### Array with specific output limit
224
+
225
+ ```ruby
226
+ $ cat > 8.rb
182
227
  require "awesome_print"
183
228
  some_array = (1..1000).to_a
184
229
  ap some_array, :limit => 5
@@ -193,7 +238,7 @@ $ ruby 8.rb
193
238
  ]
194
239
  ```
195
240
 
196
- ### Example (Rails console) ###
241
+ #### Rails console
197
242
  ```ruby
198
243
  $ rails console
199
244
  rails> require "awesome_print"
@@ -296,6 +341,14 @@ formatting:
296
341
 
297
342
  <%= ap @accounts.first, :html => true %>
298
343
 
344
+ ### String Convenience Methods ###
345
+ Use methods such as `.red` to set string color:
346
+
347
+ ```ruby
348
+ irb> puts "red text".red
349
+ red text # (it's red)
350
+ ```
351
+
299
352
  ### Setting Custom Defaults ###
300
353
  You can set your own default options by creating ``.aprc`` file in your home
301
354
  directory. Within that file assign your defaults to ``AwesomePrint.defaults``.
@@ -316,60 +369,11 @@ AwesomePrint.defaults = {
316
369
 
317
370
  AwesomePrint follows the [Semantic Versioning](http://semver.org/) standard.
318
371
 
319
- ### Running Specs ###
320
-
321
- $ gem install rspec # RSpec 2.x is the requirement.
322
- $ rake spec # Run the entire spec suite.
323
- $ rspec spec/logger_spec.rb # Run individual spec file.
324
-
325
- ### Note on Patches/Pull Requests ###
326
- * Fork the project on Github.
327
- * Make your feature addition or bug fix.
328
- * Add specs for it, making sure $ rake spec is all green.
329
- * Commit, do not mess with rakefile, version, or history.
330
- * Send commit URL (*do not* send pull requests).
331
-
332
- ### Contributors ###
333
- Special thanks goes to awesome team of contributors, namely:
334
-
335
- * 6fusion.com -- https://github.com/6fusion
336
- * Adam Doppelt -- https://github.com/gurgeous
337
- * Andrew O'Brien -- https://github.com/AndrewO
338
- * Andrew Horsman -- https://github.com/basicxman
339
- * Barry Allard -- https://github.com/steakknife
340
- * Benoit Daloze -- http://github.com/eregon
341
- * Brandon Zylstra -- https://github.com/brandondrew
342
- * Dan Lynn -- https://github.com/danlynn
343
- * Daniel Johnson -- https://github.com/adhd360
344
- * Daniel Bretoi -- http://github.com/danielb2
345
- * Eloy Duran -- http://github.com/alloy
346
- * Elpizo Choi -- https://github.com/fuJiin
347
- * Evan Senter -- https://github.com/evansenter
348
- * George . -- https://github.com/gardelea
349
- * Greg Weber -- https://github.com/gregwebs
350
- * Jan Vansteenkiste -- https://github.com/vStone
351
- * Jeff Felchner -- https://github.com/jfelchner
352
- * Jonathan Davies -- send your Github URL ;-)
353
- * Kevin Olbrich -- https://github.com/olbrich
354
- * Matthew Schulkind -- https://github.com/mschulkind
355
- * Mike McQuaid -- https://github.com/mikemcquaid
356
- * Nami-Doc -- https://github.com/Nami-Doc
357
- * Nicolas Viennot -- https://github.com/nviennot
358
- * Nikolaj Nikolajsen -- https://github.com/nikolajsen
359
- * Richard Hall -- https://github.com/richardardrichard
360
- * Ryan Schlesinger -- https://github.com/ryansch
361
- * Scott Hyndman -- https://github.com/shyndman
362
- * Sean Gallagher -- http://github.com/torandu
363
- * Stephan Hagemann -- https://github.com/shageman
364
- * Tim Harper -- http://github.com/timcharper
365
- * Tobias Crawley -- http://github.com/tobias
366
- * Thibaut Barrère -- https://github.com/thbar
367
- * Trevor Wennblom -- https://github.com/trevor
368
- * vfrride -- https://github.com/vfrride
369
- * Viktar Basharymau -- https://github.com/DNNX
372
+ ### Contributing ###
373
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for information.
370
374
 
371
375
  ### License ###
372
- Copyright (c) 2010-2013 Michael Dvorkin
376
+ Copyright (c) 2010-2016 Michael Dvorkin and contributors
373
377
 
374
378
  http://www.dvorkin.net
375
379
 
@@ -380,7 +384,8 @@ Released under the MIT license. See LICENSE file for details.
380
384
  [gem_version_badge]: https://img.shields.io/gem/v/awesome_print.svg?style=flat
381
385
  [gem_downloads_badge]: http://img.shields.io/gem/dt/awesome_print.svg?style=flat
382
386
  [ruby_gems]: http://rubygems.org/gems/awesome_print
383
- [travis_ci]: http://travis-ci.org/michaeldv/awesome_print
384
- [travis_ci_badge]: https://img.shields.io/travis/michaeldv/awesome_print.svg?style=flat
385
- [code_climate]: https://codeclimate.com/github/michaeldv/awesome_print
386
- [code_climate_badge]: http://img.shields.io/codeclimate/github/michaeldv/awesome_print.svg?style=flat
387
+ [travis_ci]: http://travis-ci.org/awesome-print/awesome_print
388
+ [travis_ci_badge]: https://img.shields.io/travis/awesome-print/awesome_print/master.svg?style=flat
389
+ [code_climate]: https://codeclimate.com/github/awesome-print/awesome_print
390
+ [code_climate_badge]: http://img.shields.io/codeclimate/github/awesome-print/awesome_print.svg?style=flat
391
+ [code_climate_coverage_badge]: https://codeclimate.com/github/awesome-print/awesome_print/badges/coverage.svg
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
- require "rubygems"
2
- require "bundler/setup"
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
3
 
4
- require "bundler"
4
+ require 'bundler'
5
5
  Bundler::GemHelper.install_tasks
6
6
 
7
7
  task :default do
@@ -16,9 +16,9 @@ task :appraise do
16
16
  exec 'appraisal install && appraisal rake'
17
17
  end
18
18
 
19
- desc "Run all awesome_print gem specs"
19
+ desc 'Run all awesome_print gem specs'
20
20
  task :spec do
21
21
  # Run plain rspec command without RSpec::Core::RakeTask overrides.
22
- exec "rspec -c spec"
22
+ exec 'rspec -c spec'
23
23
  end
24
24
 
@@ -0,0 +1,33 @@
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
+ $:.push File.expand_path('../lib', __FILE__)
8
+ require 'awesome_print/version'
9
+
10
+ Gem::Specification.new do |s|
11
+ s.name = 'awesome_print'
12
+ s.version = AwesomePrint.version
13
+ s.authors = 'Michael Dvorkin'
14
+ s.date = Time.now.strftime('%Y-%m-%d')
15
+ s.email = 'mike@dvorkin.net'
16
+ s.homepage = 'https://github.com/awesome-print/awesome_print'
17
+ s.summary = 'Pretty print Ruby objects with proper indentation and colors'
18
+ s.description = 'Great Ruby debugging companion: pretty print Ruby objects to visualize their structure. Supports custom object formatting via plugins'
19
+ s.license = 'MIT'
20
+
21
+ s.files = Dir['[A-Z]*[^~]'] + Dir['lib/**/*.rb'] + Dir['spec/**/*'] + ['.gitignore']
22
+ s.test_files = Dir['spec/**/*']
23
+ s.executables = []
24
+ s.require_paths = ['lib']
25
+
26
+ s.add_development_dependency 'rspec', '>= 3.0.0'
27
+ s.add_development_dependency 'appraisal'
28
+ s.add_development_dependency 'fakefs', '>= 0.2.1'
29
+ s.add_development_dependency 'sqlite3'
30
+ s.add_development_dependency 'nokogiri', '>= 1.11.0'
31
+ # s.add_development_dependency 'simplecov'
32
+ # s.add_development_dependency 'codeclimate-test-reporter'
33
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'lib', 'awesome_print')
data/lib/ap.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2010-2013 Michael Dvorkin
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
@@ -7,4 +7,4 @@
7
7
  # Keeping this for backwards compatibility to allow
8
8
  # require "ap"
9
9
  #
10
- require File.dirname(__FILE__) + "/awesome_print"
10
+ require File.dirname(__FILE__) + '/awesome_print'
@@ -0,0 +1,24 @@
1
+ autoload :CGI, 'cgi'
2
+
3
+ module AwesomePrint
4
+ module Colorize
5
+
6
+ # Pick the color and apply it to the given string as necessary.
7
+ #------------------------------------------------------------------------------
8
+ def colorize(str, type)
9
+ str = CGI.escapeHTML(str) if options[:html]
10
+ if options[:plain] || !options[:color][type] || !inspector.colorize?
11
+ str
12
+ #
13
+ # Check if the string color method is defined by awesome_print and accepts
14
+ # html parameter or it has been overriden by some gem such as colorize.
15
+ #
16
+ elsif str.method(options[:color][type]).arity == -1 # Accepts html parameter.
17
+ str.send(options[:color][type], options[:html])
18
+ else
19
+ str = %Q|<kbd style="color:#{options[:color][type]}">#{str}</kbd>| if options[:html]
20
+ str.send(options[:color][type])
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2010-2013 Michael Dvorkin
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
@@ -12,24 +12,24 @@
12
12
  #
13
13
  # If you could think of a better way please let me know :-)
14
14
  #
15
- class Array #:nodoc:
16
- [ :-, :& ].each do |operator|
17
- original_operator = instance_method(operator)
15
+ module AwesomeMethodArray #:nodoc:
18
16
 
19
- define_method operator do |*args|
20
- arr = original_operator.bind(self).call(*args)
21
- if self.instance_variable_defined?('@__awesome_methods__')
22
- arr.instance_variable_set('@__awesome_methods__', self.instance_variable_get('@__awesome_methods__'))
23
- arr.sort! { |a, b| a.to_s <=> b.to_s } # Need the block since Ruby 1.8.x can't sort arrays of symbols.
24
- end
25
- arr
17
+ def -(_other_ary)
18
+ super.tap do |arr|
19
+ arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__))
20
+ end
21
+ end
22
+
23
+ def &(_other_ary)
24
+ super.tap do |arr|
25
+ arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__))
26
26
  end
27
27
  end
28
+
28
29
  #
29
30
  # Intercepting Array#grep needs a special treatment since grep accepts
30
31
  # an optional block.
31
32
  #
32
- alias :original_grep :grep
33
33
  def grep(pattern, &blk)
34
34
  #
35
35
  # The following looks rather insane and I've sent numerous hours trying
@@ -55,9 +55,9 @@ class Array #:nodoc:
55
55
  # the comment :-)
56
56
  #
57
57
  arr = unless blk
58
- original_grep(pattern)
58
+ super(pattern)
59
59
  else
60
- original_grep(pattern) do |match|
60
+ super(pattern) do |match|
61
61
  #
62
62
  # The binding can only be used with Ruby-defined methods, therefore
63
63
  # we must rescue potential "ArgumentError: Can't create Binding from
@@ -72,10 +72,8 @@ class Array #:nodoc:
72
72
  yield match
73
73
  end
74
74
  end
75
- if self.instance_variable_defined?('@__awesome_methods__')
76
- arr.instance_variable_set('@__awesome_methods__', self.instance_variable_get('@__awesome_methods__'))
77
- arr.reject! { |item| !(item.is_a?(Symbol) || item.is_a?(String)) } # grep block might return crap.
78
- end
75
+ arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__))
76
+ arr.reject! { |item| !(item.is_a?(Symbol) || item.is_a?(String)) } # grep block might return crap.
79
77
  arr
80
78
  end
81
79
  end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2010-2013 Michael Dvorkin
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
@@ -15,7 +15,8 @@ class Class #:nodoc:
15
15
 
16
16
  define_method name do |*args|
17
17
  methods = original_method.bind(self).call(*args)
18
- methods.instance_variable_set('@__awesome_methods__', self)
18
+ methods.instance_variable_set(:@__awesome_methods__, self)
19
+ methods.extend(AwesomeMethodArray)
19
20
  methods
20
21
  end
21
22
  end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2010-2013 Michael Dvorkin
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2010-2013 Michael Dvorkin
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2010-2013 Michael Dvorkin
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
@@ -18,4 +18,4 @@ unless nil.method(:class).respond_to?(:name)
18
18
  inspect.split(/[#.>]/)[-1]
19
19
  end
20
20
  end
21
- end
21
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2010-2013 Michael Dvorkin
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
@@ -15,7 +15,8 @@ class Object #:nodoc:
15
15
 
16
16
  define_method name do |*args|
17
17
  methods = original_method.bind(self).call(*args)
18
- methods.instance_variable_set('@__awesome_methods__', self)
18
+ methods.instance_variable_set(:@__awesome_methods__, self)
19
+ methods.extend(AwesomeMethodArray)
19
20
  methods
20
21
  end
21
22
  end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2010-2013 Michael Dvorkin
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
@@ -17,11 +17,11 @@ class String
17
17
  %w(gray red green yellow blue purple cyan white).zip(
18
18
  %w(black darkred darkgreen brown navy darkmagenta darkcyan slategray)).each_with_index do |(color, shade), i|
19
19
  define_method color do |*html|
20
- html[0] ? %Q|<kbd style="color:#{color}">#{self}</kbd>| : "\e[1;#{30+i}m#{self}\e[0m"
20
+ html[0] ? %Q|<kbd style="color:#{color}">#{self}</kbd>| : "\e[1;#{30 + i}m#{self}\e[0m"
21
21
  end
22
22
 
23
23
  define_method "#{color}ish" do |*html|
24
- html[0] ? %Q|<kbd style="color:#{shade}">#{self}</kbd>| : "\e[0;#{30+i}m#{self}\e[0m"
24
+ html[0] ? %Q|<kbd style="color:#{shade}">#{self}</kbd>| : "\e[0;#{30 + i}m#{self}\e[0m"
25
25
  end
26
26
  end
27
27
 
@@ -0,0 +1,57 @@
1
+ module AwesomePrint
2
+ class << self
3
+ attr_accessor :defaults, :force_colors
4
+
5
+ # Class accessor to force colorized output (ex. forked subprocess where TERM
6
+ # might be dumb).
7
+ #---------------------------------------------------------------------------
8
+ def force_colors!(value = true)
9
+ @force_colors = value
10
+ end
11
+
12
+ def console?
13
+ boolean(defined?(IRB) || defined?(Pry))
14
+ end
15
+
16
+ def rails_console?
17
+ console? && boolean(defined?(Rails::Console) || ENV['RAILS_ENV'])
18
+ end
19
+
20
+ def diet_rb
21
+ IRB.formatter = Class.new(IRB::Formatter) do
22
+ def inspect_object(object)
23
+ object.ai
24
+ end
25
+ end.new
26
+ end
27
+
28
+ def usual_rb
29
+ IRB::Irb.class_eval do
30
+ def output_value(*args)
31
+ ap @context.last_value
32
+ rescue NoMethodError
33
+ puts "(Object doesn't support #ai)"
34
+ end
35
+ end
36
+ end
37
+
38
+ def irb!
39
+ return unless defined?(IRB)
40
+
41
+ IRB.version.include?('DietRB') ? diet_rb : usual_rb
42
+ end
43
+
44
+ def pry!
45
+ Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry)
46
+ end
47
+
48
+ private
49
+
50
+ # Takes a value and returns true unless it is false or nil
51
+ # This is an alternative to the less readable !!(value)
52
+ # https://github.com/bbatsov/ruby-style-guide#no-bang-bang
53
+ def boolean(value)
54
+ value ? true : false
55
+ end
56
+ end
57
+ end
@@ -1,17 +1,21 @@
1
- # Copyright (c) 2010-2013 Michael Dvorkin
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
5
  #------------------------------------------------------------------------------
6
6
  module AwesomePrint
7
7
  module ActionView
8
-
9
8
  # Use HTML colors and add default "debug_dump" class to the resulting HTML.
10
9
  def ap_debug(object, options = {})
11
- object.ai(options.merge(:html => true)).sub(/^<pre([\s>])/, '<pre class="debug_dump"\\1')
10
+ object.ai(
11
+ options.merge(html: true)
12
+ ).sub(
13
+ /^<pre([\s>])/,
14
+ '<pre class="debug_dump"\\1'
15
+ )
12
16
  end
13
17
 
14
- alias_method :ap, :ap_debug
18
+ alias ap ap_debug
15
19
  end
16
20
  end
17
21