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.
- checksums.yaml +7 -0
- data/.gitignore +14 -2
- data/Appraisals +62 -0
- data/CHANGELOG.md +180 -0
- data/CONTRIBUTING.md +82 -0
- data/Gemfile +3 -2
- data/LICENSE +16 -19
- data/README.md +112 -87
- data/Rakefile +17 -4
- data/awesome_print.gemspec +33 -0
- data/init.rb +1 -0
- data/lib/ap.rb +2 -2
- data/lib/awesome_print.rb +22 -16
- data/lib/awesome_print/colorize.rb +24 -0
- data/lib/awesome_print/core_ext/{array.rb → awesome_method_array.rb} +16 -18
- data/lib/awesome_print/core_ext/class.rb +3 -2
- data/lib/awesome_print/core_ext/kernel.rb +1 -1
- data/lib/awesome_print/core_ext/logger.rb +1 -1
- data/lib/awesome_print/core_ext/method.rb +2 -2
- data/lib/awesome_print/core_ext/object.rb +3 -2
- data/lib/awesome_print/core_ext/string.rb +3 -3
- data/lib/awesome_print/custom_defaults.rb +57 -0
- data/lib/awesome_print/ext/action_view.rb +8 -4
- data/lib/awesome_print/ext/active_record.rb +47 -13
- data/lib/awesome_print/ext/active_support.rb +1 -1
- data/lib/awesome_print/ext/mongo_mapper.rb +16 -13
- data/lib/awesome_print/ext/mongoid.rb +9 -7
- data/lib/awesome_print/ext/nobrainer.rb +52 -0
- data/lib/awesome_print/ext/nokogiri.rb +4 -4
- data/lib/awesome_print/ext/ostruct.rb +27 -0
- data/lib/awesome_print/ext/ripple.rb +6 -7
- data/lib/awesome_print/ext/sequel.rb +7 -6
- data/lib/awesome_print/formatter.rb +54 -327
- data/lib/awesome_print/formatters.rb +15 -0
- data/lib/awesome_print/formatters/array_formatter.rb +139 -0
- data/lib/awesome_print/formatters/base_formatter.rb +140 -0
- data/lib/awesome_print/formatters/class_formatter.rb +25 -0
- data/lib/awesome_print/formatters/dir_formatter.rb +22 -0
- data/lib/awesome_print/formatters/file_formatter.rb +22 -0
- data/lib/awesome_print/formatters/hash_formatter.rb +106 -0
- data/lib/awesome_print/formatters/method_formatter.rb +22 -0
- data/lib/awesome_print/formatters/object_formatter.rb +78 -0
- data/lib/awesome_print/formatters/simple_formatter.rb +21 -0
- data/lib/awesome_print/formatters/struct_formatter.rb +71 -0
- data/lib/awesome_print/indentator.rb +18 -0
- data/lib/awesome_print/inspector.rb +92 -94
- data/lib/awesome_print/version.rb +2 -2
- data/spec/active_record_helper.rb +30 -0
- data/spec/colors_spec.rb +31 -31
- data/spec/core_ext/logger_spec.rb +43 -0
- data/spec/core_ext/string_spec.rb +20 -0
- data/spec/ext/action_view_spec.rb +21 -0
- data/spec/ext/active_record_spec.rb +260 -0
- data/spec/ext/active_support_spec.rb +30 -0
- data/spec/ext/mongo_mapper_spec.rb +261 -0
- data/spec/ext/mongoid_spec.rb +67 -0
- data/spec/ext/nobrainer_spec.rb +59 -0
- data/spec/ext/nokogiri_spec.rb +46 -0
- data/spec/ext/ostruct_spec.rb +22 -0
- data/spec/ext/ripple_spec.rb +48 -0
- data/spec/formats_spec.rb +272 -205
- data/spec/methods_spec.rb +173 -177
- data/spec/misc_spec.rb +129 -105
- data/spec/objects_spec.rb +162 -27
- data/spec/spec_helper.rb +75 -37
- data/spec/support/active_record_data.rb +20 -0
- data/spec/support/active_record_data/3_2_diana.txt +24 -0
- data/spec/support/active_record_data/3_2_diana_legacy.txt +24 -0
- data/spec/support/active_record_data/3_2_multi.txt +50 -0
- data/spec/support/active_record_data/3_2_multi_legacy.txt +50 -0
- data/spec/support/active_record_data/4_0_diana.txt +98 -0
- data/spec/support/active_record_data/4_0_multi.txt +198 -0
- data/spec/support/active_record_data/4_1_diana.txt +97 -0
- data/spec/support/active_record_data/4_1_multi.txt +196 -0
- data/spec/support/active_record_data/4_2_diana.txt +109 -0
- data/spec/support/active_record_data/4_2_diana_legacy.txt +109 -0
- data/spec/support/active_record_data/4_2_multi.txt +220 -0
- data/spec/support/active_record_data/4_2_multi_legacy.txt +220 -0
- data/spec/support/active_record_data/5_0_diana.txt +105 -0
- data/spec/support/active_record_data/5_0_multi.txt +212 -0
- data/spec/support/active_record_data/5_1_diana.txt +104 -0
- data/spec/support/active_record_data/5_1_multi.txt +210 -0
- data/spec/support/active_record_data/5_2_diana.txt +104 -0
- data/spec/support/active_record_data/5_2_multi.txt +210 -0
- data/spec/support/active_record_data/6_0_diana.txt +104 -0
- data/spec/support/active_record_data/6_0_multi.txt +210 -0
- data/spec/support/active_record_data/6_1_diana.txt +109 -0
- data/spec/support/active_record_data/6_1_multi.txt +220 -0
- data/spec/support/ext_verifier.rb +42 -0
- data/spec/support/mongoid_versions.rb +26 -0
- data/spec/support/rails_versions.rb +55 -0
- metadata +163 -32
- data/CHANGELOG +0 -96
- data/lib/awesome_print/ext/no_brainer.rb +0 -58
data/Rakefile
CHANGED
@@ -1,11 +1,24 @@
|
|
1
|
-
require
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'bundler'
|
2
5
|
Bundler::GemHelper.install_tasks
|
3
6
|
|
4
|
-
task :default
|
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
|
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
|
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-
|
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__) +
|
10
|
+
require File.dirname(__FILE__) + '/awesome_print'
|
data/lib/awesome_print.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
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(
|
12
|
-
require
|
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
|
16
|
-
require
|
17
|
-
require
|
18
|
-
require
|
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
|
24
|
-
require
|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
require
|
34
|
-
require
|
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-
|
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
|
-
|
16
|
-
[ :-, :& ].each do |operator|
|
17
|
-
original_operator = instance_method(operator)
|
15
|
+
module AwesomeMethodArray #:nodoc:
|
18
16
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
58
|
+
super(pattern)
|
59
59
|
else
|
60
|
-
|
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
|
-
|
76
|
-
|
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-
|
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(
|
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-
|
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-
|
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(
|
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-
|
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-
|
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(
|
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
|
-
|
18
|
+
alias ap ap_debug
|
15
19
|
end
|
16
20
|
end
|
17
21
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
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.
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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 ==
|
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
|
-
|
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
|