awesome_print 1.2.0 → 1.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -2
  3. data/Appraisals +62 -0
  4. data/CHANGELOG.md +180 -0
  5. data/CONTRIBUTING.md +82 -0
  6. data/Gemfile +3 -2
  7. data/LICENSE +16 -19
  8. data/README.md +112 -87
  9. data/Rakefile +17 -4
  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.rb +22 -16
  14. data/lib/awesome_print/colorize.rb +24 -0
  15. data/lib/awesome_print/core_ext/{array.rb → awesome_method_array.rb} +16 -18
  16. data/lib/awesome_print/core_ext/class.rb +3 -2
  17. data/lib/awesome_print/core_ext/kernel.rb +1 -1
  18. data/lib/awesome_print/core_ext/logger.rb +1 -1
  19. data/lib/awesome_print/core_ext/method.rb +2 -2
  20. data/lib/awesome_print/core_ext/object.rb +3 -2
  21. data/lib/awesome_print/core_ext/string.rb +3 -3
  22. data/lib/awesome_print/custom_defaults.rb +57 -0
  23. data/lib/awesome_print/ext/action_view.rb +8 -4
  24. data/lib/awesome_print/ext/active_record.rb +47 -13
  25. data/lib/awesome_print/ext/active_support.rb +1 -1
  26. data/lib/awesome_print/ext/mongo_mapper.rb +16 -13
  27. data/lib/awesome_print/ext/mongoid.rb +9 -7
  28. data/lib/awesome_print/ext/nobrainer.rb +52 -0
  29. data/lib/awesome_print/ext/nokogiri.rb +4 -4
  30. data/lib/awesome_print/ext/ostruct.rb +27 -0
  31. data/lib/awesome_print/ext/ripple.rb +6 -7
  32. data/lib/awesome_print/ext/sequel.rb +7 -6
  33. data/lib/awesome_print/formatter.rb +54 -327
  34. data/lib/awesome_print/formatters.rb +15 -0
  35. data/lib/awesome_print/formatters/array_formatter.rb +139 -0
  36. data/lib/awesome_print/formatters/base_formatter.rb +140 -0
  37. data/lib/awesome_print/formatters/class_formatter.rb +25 -0
  38. data/lib/awesome_print/formatters/dir_formatter.rb +22 -0
  39. data/lib/awesome_print/formatters/file_formatter.rb +22 -0
  40. data/lib/awesome_print/formatters/hash_formatter.rb +106 -0
  41. data/lib/awesome_print/formatters/method_formatter.rb +22 -0
  42. data/lib/awesome_print/formatters/object_formatter.rb +78 -0
  43. data/lib/awesome_print/formatters/simple_formatter.rb +21 -0
  44. data/lib/awesome_print/formatters/struct_formatter.rb +71 -0
  45. data/lib/awesome_print/indentator.rb +18 -0
  46. data/lib/awesome_print/inspector.rb +92 -94
  47. data/lib/awesome_print/version.rb +2 -2
  48. data/spec/active_record_helper.rb +30 -0
  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 +272 -205
  62. data/spec/methods_spec.rb +173 -177
  63. data/spec/misc_spec.rb +129 -105
  64. data/spec/objects_spec.rb +162 -27
  65. data/spec/spec_helper.rb +75 -37
  66. data/spec/support/active_record_data.rb +20 -0
  67. data/spec/support/active_record_data/3_2_diana.txt +24 -0
  68. data/spec/support/active_record_data/3_2_diana_legacy.txt +24 -0
  69. data/spec/support/active_record_data/3_2_multi.txt +50 -0
  70. data/spec/support/active_record_data/3_2_multi_legacy.txt +50 -0
  71. data/spec/support/active_record_data/4_0_diana.txt +98 -0
  72. data/spec/support/active_record_data/4_0_multi.txt +198 -0
  73. data/spec/support/active_record_data/4_1_diana.txt +97 -0
  74. data/spec/support/active_record_data/4_1_multi.txt +196 -0
  75. data/spec/support/active_record_data/4_2_diana.txt +109 -0
  76. data/spec/support/active_record_data/4_2_diana_legacy.txt +109 -0
  77. data/spec/support/active_record_data/4_2_multi.txt +220 -0
  78. data/spec/support/active_record_data/4_2_multi_legacy.txt +220 -0
  79. data/spec/support/active_record_data/5_0_diana.txt +105 -0
  80. data/spec/support/active_record_data/5_0_multi.txt +212 -0
  81. data/spec/support/active_record_data/5_1_diana.txt +104 -0
  82. data/spec/support/active_record_data/5_1_multi.txt +210 -0
  83. data/spec/support/active_record_data/5_2_diana.txt +104 -0
  84. data/spec/support/active_record_data/5_2_multi.txt +210 -0
  85. data/spec/support/active_record_data/6_0_diana.txt +104 -0
  86. data/spec/support/active_record_data/6_0_multi.txt +210 -0
  87. data/spec/support/active_record_data/6_1_diana.txt +109 -0
  88. data/spec/support/active_record_data/6_1_multi.txt +220 -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 +163 -32
  93. data/CHANGELOG +0 -96
  94. data/lib/awesome_print/ext/no_brainer.rb +0 -58
data/Rakefile CHANGED
@@ -1,11 +1,24 @@
1
- require "bundler"
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'bundler'
2
5
  Bundler::GemHelper.install_tasks
3
6
 
4
- task :default => :spec
7
+ task :default do
8
+ if ENV['BUNDLE_GEMFILE'] =~ /gemfiles/
9
+ Rake::Task['spec'].invoke
10
+ else
11
+ Rake::Task['appraise'].invoke
12
+ end
13
+ end
14
+
15
+ task :appraise do
16
+ exec 'appraisal install && appraisal rake'
17
+ end
5
18
 
6
- desc "Run all awesome_print gem specs"
19
+ desc 'Run all awesome_print gem specs'
7
20
  task :spec do
8
21
  # Run plain rspec command without RSpec::Core::RakeTask overrides.
9
- exec "rspec -c spec"
22
+ exec 'rspec -c spec'
10
23
  end
11
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'
data/lib/awesome_print.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
@@ -8,28 +8,34 @@
8
8
  # so do nothing for subsequent requires.
9
9
  #
10
10
  unless defined?(AwesomePrint::Inspector)
11
- %w(array string method object class kernel).each do |file|
12
- require File.dirname(__FILE__) + "/awesome_print/core_ext/#{file}"
11
+ %w(awesome_method_array string method object class kernel).each do |file|
12
+ require "awesome_print/core_ext/#{file}"
13
13
  end
14
14
 
15
- require File.dirname(__FILE__) + "/awesome_print/inspector"
16
- require File.dirname(__FILE__) + "/awesome_print/formatter"
17
- require File.dirname(__FILE__) + "/awesome_print/version"
18
- require File.dirname(__FILE__) + "/awesome_print/core_ext/logger" if defined?(Logger)
15
+ require 'awesome_print/custom_defaults'
16
+ require 'awesome_print/inspector'
17
+ require 'awesome_print/formatter'
18
+ require 'awesome_print/version'
19
+ require 'awesome_print/core_ext/logger' if defined?(Logger)
19
20
  #
20
21
  # Load the following under normal circumstances as well as in Rails
21
22
  # console when required from ~/.irbrc or ~/.pryrc.
22
23
  #
23
- require File.dirname(__FILE__) + "/awesome_print/ext/active_record" if defined?(ActiveRecord) || AwesomePrint.rails_console?
24
- require File.dirname(__FILE__) + "/awesome_print/ext/active_support" if defined?(ActiveSupport) || AwesomePrint.rails_console?
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?
25
26
  #
26
27
  # Load remaining extensions.
27
28
  #
28
- require File.dirname(__FILE__) + "/awesome_print/ext/action_view" if defined?(ActionView::Base)
29
- require File.dirname(__FILE__) + "/awesome_print/ext/mongo_mapper" if defined?(MongoMapper)
30
- require File.dirname(__FILE__) + "/awesome_print/ext/mongoid" if defined?(Mongoid)
31
- require File.dirname(__FILE__) + "/awesome_print/ext/nokogiri" if defined?(Nokogiri)
32
- require File.dirname(__FILE__) + "/awesome_print/ext/no_brainer" if defined?(NoBrainer)
33
- require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple)
34
- require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel)
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)
35
41
  end
@@ -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
 
@@ -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,13 +15,15 @@ module AwesomePrint
15
15
  #------------------------------------------------------------------------------
16
16
  def cast_with_active_record(object, type)
17
17
  cast = cast_without_active_record(object, type)
18
- return cast if !defined?(::ActiveRecord)
18
+ return cast if !defined?(::ActiveRecord::Base)
19
19
 
20
20
  if object.is_a?(::ActiveRecord::Base)
21
21
  cast = :active_record_instance
22
+ elsif object.is_a?(::ActiveModel::Errors)
23
+ cast = :active_model_error
22
24
  elsif object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base)
23
25
  cast = :active_record_class
24
- elsif type == :activerecord_relation
26
+ elsif type == :activerecord_relation || object.class.ancestors.include?(::ActiveRecord::Relation)
25
27
  cast = :array
26
28
  end
27
29
  cast
@@ -42,26 +44,58 @@ module AwesomePrint
42
44
  return object.inspect if !defined?(::ActiveSupport::OrderedHash)
43
45
  return awesome_object(object) if @options[:raw]
44
46
 
45
- data = object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
46
- if object.has_attribute?(name) || object.new_record?
47
- value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name)
48
- hash[name.to_sym] = value
49
- end
50
- hash
51
- end
52
- "#{object} " << awesome_hash(data)
47
+ data = if object.class.column_names != object.attributes.keys
48
+ object.attributes
49
+ else
50
+ object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
51
+ if object.has_attribute?(name) || object.new_record?
52
+ value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name)
53
+ hash[name.to_sym] = value
54
+ end
55
+ hash
56
+ end
57
+ end
58
+ "#{object} #{awesome_hash(data)}"
53
59
  end
54
60
 
55
61
  # Format ActiveRecord class object.
56
62
  #------------------------------------------------------------------------------
57
63
  def awesome_active_record_class(object)
58
- return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:columns) || object.to_s == "ActiveRecord::Base"
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?
59
66
 
60
67
  data = object.columns.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
61
68
  hash[c.name.to_sym] = c.type
62
69
  hash
63
70
  end
64
- "class #{object} < #{object.superclass} " << awesome_hash(data)
71
+
72
+ name = "class #{awesome_simple(object.to_s, :class)}"
73
+ base = "< #{awesome_simple(object.superclass.to_s, :class)}"
74
+
75
+ [name, base, awesome_hash(data)].join(' ')
76
+ end
77
+
78
+ # Format ActiveModel error object.
79
+ #------------------------------------------------------------------------------
80
+ def awesome_active_model_error(object)
81
+ return object.inspect if !defined?(::ActiveSupport::OrderedHash)
82
+ return awesome_object(object) if @options[:raw]
83
+
84
+ object_dump = object.marshal_dump.first
85
+ data = if object_dump.class.column_names != object_dump.attributes.keys
86
+ object_dump.attributes
87
+ else
88
+ object_dump.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
89
+ if object_dump.has_attribute?(name) || object_dump.new_record?
90
+ value = object_dump.respond_to?(name) ? object_dump.send(name) : object_dump.read_attribute(name)
91
+ hash[name.to_sym] = value
92
+ end
93
+ hash
94
+ end
95
+ end
96
+
97
+ data.merge!({details: object.details, messages: object.messages})
98
+ "#{object} #{awesome_hash(data)}"
65
99
  end
66
100
  end
67
101
  end