amazing_print 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +35 -0
- data/Appraisals +60 -0
- data/CHANGELOG.md +2 -0
- data/CONTRIBUTING.md +81 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +356 -0
- data/Rakefile +23 -0
- data/lib/amazing_print.rb +46 -0
- data/lib/amazing_print/colorize.rb +25 -0
- data/lib/amazing_print/core_ext/awesome_method_array.rb +82 -0
- data/lib/amazing_print/core_ext/class.rb +23 -0
- data/lib/amazing_print/core_ext/kernel.rb +25 -0
- data/lib/amazing_print/core_ext/logger.rb +21 -0
- data/lib/amazing_print/core_ext/method.rb +21 -0
- data/lib/amazing_print/core_ext/object.rb +23 -0
- data/lib/amazing_print/core_ext/string.rb +42 -0
- data/lib/amazing_print/custom_defaults.rb +57 -0
- data/lib/amazing_print/ext/action_view.rb +22 -0
- data/lib/amazing_print/ext/active_record.rb +103 -0
- data/lib/amazing_print/ext/active_support.rb +45 -0
- data/lib/amazing_print/ext/mongo_mapper.rb +125 -0
- data/lib/amazing_print/ext/mongoid.rb +68 -0
- data/lib/amazing_print/ext/nobrainer.rb +53 -0
- data/lib/amazing_print/ext/nokogiri.rb +45 -0
- data/lib/amazing_print/ext/ostruct.rb +27 -0
- data/lib/amazing_print/ext/ripple.rb +71 -0
- data/lib/amazing_print/ext/sequel.rb +55 -0
- data/lib/amazing_print/formatter.rb +120 -0
- data/lib/amazing_print/formatters.rb +14 -0
- data/lib/amazing_print/formatters/array_formatter.rb +139 -0
- data/lib/amazing_print/formatters/base_formatter.rb +148 -0
- data/lib/amazing_print/formatters/class_formatter.rb +24 -0
- data/lib/amazing_print/formatters/dir_formatter.rb +21 -0
- data/lib/amazing_print/formatters/file_formatter.rb +21 -0
- data/lib/amazing_print/formatters/hash_formatter.rb +106 -0
- data/lib/amazing_print/formatters/method_formatter.rb +21 -0
- data/lib/amazing_print/formatters/object_formatter.rb +82 -0
- data/lib/amazing_print/formatters/simple_formatter.rb +20 -0
- data/lib/amazing_print/formatters/struct_formatter.rb +74 -0
- data/lib/amazing_print/indentator.rb +17 -0
- data/lib/amazing_print/inspector.rb +175 -0
- data/lib/amazing_print/version.rb +10 -0
- data/lib/ap.rb +10 -0
- data/spec/active_record_helper.rb +30 -0
- data/spec/colors_spec.rb +114 -0
- data/spec/core_ext/logger_spec.rb +44 -0
- data/spec/core_ext/string_spec.rb +20 -0
- data/spec/ext/action_view_spec.rb +17 -0
- data/spec/ext/active_record_spec.rb +297 -0
- data/spec/ext/active_support_spec.rb +26 -0
- data/spec/ext/mongo_mapper_spec.rb +259 -0
- data/spec/ext/mongoid_spec.rb +66 -0
- data/spec/ext/nobrainer_spec.rb +58 -0
- data/spec/ext/nokogiri_spec.rb +50 -0
- data/spec/ext/ostruct_spec.rb +22 -0
- data/spec/ext/ripple_spec.rb +47 -0
- data/spec/formats_spec.rb +779 -0
- data/spec/methods_spec.rb +478 -0
- data/spec/misc_spec.rb +245 -0
- data/spec/objects_spec.rb +219 -0
- data/spec/spec_helper.rb +106 -0
- 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/ext_verifier.rb +41 -0
- data/spec/support/mongoid_versions.rb +22 -0
- data/spec/support/rails_versions.rb +50 -0
- metadata +243 -0
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'bundler'
|
5
|
+
Bundler::GemHelper.install_tasks
|
6
|
+
|
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
|
18
|
+
|
19
|
+
desc 'Run all amazing_print gem specs'
|
20
|
+
task :spec do
|
21
|
+
# Run plain rspec command without RSpec::Core::RakeTask overrides.
|
22
|
+
exec 'rspec -c spec'
|
23
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
2
|
+
#
|
3
|
+
# AmazingPrint 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
|
+
# AmazingPrint might be loaded implicitly through ~/.irbrc or ~/.pryrc
|
8
|
+
# so do nothing for subsequent requires.
|
9
|
+
#
|
10
|
+
unless defined?(AmazingPrint::Inspector)
|
11
|
+
%w[awesome_method_array string method object class kernel].each do |file|
|
12
|
+
require "amazing_print/core_ext/#{file}"
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'amazing_print/custom_defaults'
|
16
|
+
require 'amazing_print/inspector'
|
17
|
+
require 'amazing_print/formatter'
|
18
|
+
require 'amazing_print/version'
|
19
|
+
require 'amazing_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
|
+
if defined?(ActiveRecord) || AmazingPrint.rails_console?
|
25
|
+
require 'amazing_print/ext/active_record'
|
26
|
+
end
|
27
|
+
if defined?(ActiveSupport) || AmazingPrint.rails_console?
|
28
|
+
require 'amazing_print/ext/active_support'
|
29
|
+
end
|
30
|
+
#
|
31
|
+
# Load remaining extensions.
|
32
|
+
#
|
33
|
+
if defined?(ActiveSupport.on_load)
|
34
|
+
ActiveSupport.on_load(:action_view) do
|
35
|
+
require 'amazing_print/ext/action_view'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
require 'amazing_print/ext/mongo_mapper' if defined?(MongoMapper)
|
39
|
+
require 'amazing_print/ext/mongoid' if defined?(Mongoid)
|
40
|
+
require 'amazing_print/ext/nokogiri' if defined?(Nokogiri)
|
41
|
+
require 'amazing_print/ext/nobrainer' if defined?(NoBrainer)
|
42
|
+
require 'amazing_print/ext/ripple' if defined?(Ripple)
|
43
|
+
require 'amazing_print/ext/sequel' if defined?(Sequel)
|
44
|
+
require 'amazing_print/ext/ostruct' if defined?(OpenStruct)
|
45
|
+
end
|
46
|
+
# test
|
@@ -0,0 +1,25 @@
|
|
1
|
+
autoload :CGI, 'cgi'
|
2
|
+
|
3
|
+
module AmazingPrint
|
4
|
+
module Colorize
|
5
|
+
# Pick the color and apply it to the given string as necessary.
|
6
|
+
#------------------------------------------------------------------------------
|
7
|
+
def colorize(str, type)
|
8
|
+
str = CGI.escapeHTML(str) if options[:html]
|
9
|
+
if options[:plain] || !options[:color][type] || !inspector.colorize?
|
10
|
+
str
|
11
|
+
#
|
12
|
+
# Check if the string color method is defined by amazing_print and accepts
|
13
|
+
# html parameter or it has been overriden by some gem such as colorize.
|
14
|
+
#
|
15
|
+
elsif str.method(options[:color][type]).arity == -1 # Accepts html parameter.
|
16
|
+
str.send(options[:color][type], options[:html])
|
17
|
+
else
|
18
|
+
if options[:html]
|
19
|
+
str = %(<kbd style="color:#{options[:color][type]}">#{str}</kbd>)
|
20
|
+
end
|
21
|
+
str.send(options[:color][type])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
2
|
+
#
|
3
|
+
# AmazingPrint 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
|
+
# The following makes it possible to invoke amazing_print while performing
|
8
|
+
# operations on method arrays, ex:
|
9
|
+
#
|
10
|
+
# ap [].methods - Object.methods
|
11
|
+
# ap ''.methods.grep(/!|\?/)
|
12
|
+
#
|
13
|
+
# If you could think of a better way please let me know :-)
|
14
|
+
#
|
15
|
+
module AwesomeMethodArray #:nodoc:
|
16
|
+
def -(_other_ary)
|
17
|
+
super.tap do |arr|
|
18
|
+
arr.instance_variable_set(:@__awesome_methods__, instance_variable_get(:@__awesome_methods__))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def &(_other_ary)
|
23
|
+
super.tap do |arr|
|
24
|
+
arr.instance_variable_set(:@__awesome_methods__, instance_variable_get(:@__awesome_methods__))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
# Intercepting Array#grep needs a special treatment since grep accepts
|
30
|
+
# an optional block.
|
31
|
+
#
|
32
|
+
def grep(pattern, &blk)
|
33
|
+
#
|
34
|
+
# The following looks rather insane and I've sent numerous hours trying
|
35
|
+
# to figure it out. The problem is that if grep gets called with the
|
36
|
+
# block, for example:
|
37
|
+
#
|
38
|
+
# [].methods.grep(/(.+?)_by/) { $1.to_sym }
|
39
|
+
#
|
40
|
+
# ...then simple:
|
41
|
+
#
|
42
|
+
# original_grep(pattern, &blk)
|
43
|
+
#
|
44
|
+
# doesn't set $1 within the grep block which causes nil.to_sym failure.
|
45
|
+
# The workaround below has been tested with Ruby 1.8.7/Rails 2.3.8 and
|
46
|
+
# Ruby 1.9.2/Rails 3.0.0. For more info see the following thread dating
|
47
|
+
# back to 2003 when Ruby 1.8.0 was as fresh off the grill as Ruby 1.9.2
|
48
|
+
# is in 2010 :-)
|
49
|
+
#
|
50
|
+
# http://www.justskins.com/forums/bug-when-rerouting-string-52852.html
|
51
|
+
#
|
52
|
+
# BTW, if you figure out a better way of intercepting Array#grep please
|
53
|
+
# let me know: twitter.com/mid -- or just say hi so I know you've read
|
54
|
+
# the comment :-)
|
55
|
+
#
|
56
|
+
arr = if blk
|
57
|
+
super(pattern) do |match|
|
58
|
+
#
|
59
|
+
# The binding can only be used with Ruby-defined methods, therefore
|
60
|
+
# we must rescue potential "ArgumentError: Can't create Binding from
|
61
|
+
# C level Proc" error.
|
62
|
+
#
|
63
|
+
# For example, the following raises ArgumentError since #succ method
|
64
|
+
# is defined in C.
|
65
|
+
#
|
66
|
+
# [ 0, 1, 2, 3, 4 ].grep(1..2, &:succ)
|
67
|
+
#
|
68
|
+
begin
|
69
|
+
eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding)
|
70
|
+
rescue StandardError
|
71
|
+
ArgumentError
|
72
|
+
end
|
73
|
+
yield match
|
74
|
+
end
|
75
|
+
else
|
76
|
+
super(pattern)
|
77
|
+
end
|
78
|
+
arr.instance_variable_set(:@__awesome_methods__, instance_variable_get(:@__awesome_methods__))
|
79
|
+
arr.reject! { |item| !(item.is_a?(Symbol) || item.is_a?(String)) } # grep block might return crap.
|
80
|
+
arr
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
2
|
+
#
|
3
|
+
# AmazingPrint is freely distributable under the terms of MIT license.
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
|
+
#------------------------------------------------------------------------------
|
6
|
+
class Class #:nodoc:
|
7
|
+
#
|
8
|
+
# Intercept methods below to inject @__amazing_print__ instance variable
|
9
|
+
# so we know it is the *methods* array when formatting an array.
|
10
|
+
#
|
11
|
+
# Remaining public/private etc. '_methods' are handled in core_ext/object.rb.
|
12
|
+
#
|
13
|
+
%w[instance_methods private_instance_methods protected_instance_methods public_instance_methods].each do |name|
|
14
|
+
original_method = instance_method(name)
|
15
|
+
|
16
|
+
define_method name do |*args|
|
17
|
+
methods = original_method.bind(self).call(*args)
|
18
|
+
methods.instance_variable_set(:@__awesome_methods__, self)
|
19
|
+
methods.extend(AwesomeMethodArray)
|
20
|
+
methods
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
2
|
+
#
|
3
|
+
# AmazingPrint is freely distributable under the terms of MIT license.
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
|
+
#------------------------------------------------------------------------------
|
6
|
+
module Kernel
|
7
|
+
def ai(options = {})
|
8
|
+
ap = AmazingPrint::Inspector.new(options)
|
9
|
+
awesome = ap.awesome self
|
10
|
+
if options[:html]
|
11
|
+
awesome = "<pre>#{awesome}</pre>"
|
12
|
+
awesome = awesome.html_safe if defined? ActiveSupport
|
13
|
+
end
|
14
|
+
awesome
|
15
|
+
end
|
16
|
+
alias awesome_inspect ai
|
17
|
+
|
18
|
+
def ap(object, options = {})
|
19
|
+
puts object.ai(options)
|
20
|
+
object unless AmazingPrint.console?
|
21
|
+
end
|
22
|
+
alias amazing_print ap
|
23
|
+
|
24
|
+
module_function :ap
|
25
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
2
|
+
#
|
3
|
+
# AmazingPrint is freely distributable under the terms of MIT license.
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
|
+
#------------------------------------------------------------------------------
|
6
|
+
module AmazingPrint
|
7
|
+
module Logger
|
8
|
+
# Add ap method to logger
|
9
|
+
#------------------------------------------------------------------------------
|
10
|
+
def ap(object, level = nil)
|
11
|
+
level ||= AmazingPrint.defaults[:log_level] if AmazingPrint.defaults
|
12
|
+
level ||= :debug
|
13
|
+
send level, object.ai
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
Logger.include AmazingPrint::Logger
|
19
|
+
if defined?(ActiveSupport::BufferedLogger)
|
20
|
+
ActiveSupport::BufferedLogger.include AmazingPrint::Logger
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
2
|
+
#
|
3
|
+
# AmazingPrint 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
|
+
# Method#name was intorduced in Ruby 1.8.7 so we define it here as necessary.
|
8
|
+
#
|
9
|
+
unless nil.method(:class).respond_to?(:name)
|
10
|
+
class Method
|
11
|
+
def name
|
12
|
+
inspect.split(/[#.>]/)[-1]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class UnboundMethod
|
17
|
+
def name
|
18
|
+
inspect.split(/[#.>]/)[-1]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
2
|
+
#
|
3
|
+
# AmazingPrint is freely distributable under the terms of MIT license.
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
|
+
#------------------------------------------------------------------------------
|
6
|
+
class Object #:nodoc:
|
7
|
+
#
|
8
|
+
# Intercept methods below to inject @__amazing_print__ instance variable
|
9
|
+
# so we know it is the *methods* array when formatting an array.
|
10
|
+
#
|
11
|
+
# Remaining instance '_methods' are handled in core_ext/class.rb.
|
12
|
+
#
|
13
|
+
%w[methods private_methods protected_methods public_methods singleton_methods].each do |name|
|
14
|
+
original_method = instance_method(name)
|
15
|
+
|
16
|
+
define_method name do |*args|
|
17
|
+
methods = original_method.bind(self).call(*args)
|
18
|
+
methods.instance_variable_set(:@__awesome_methods__, self)
|
19
|
+
methods.extend(AwesomeMethodArray)
|
20
|
+
methods
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
2
|
+
#
|
3
|
+
# AmazingPrint 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
|
+
# frozen_string_literal: true
|
8
|
+
|
9
|
+
class String
|
10
|
+
#
|
11
|
+
# ANSI color codes:
|
12
|
+
# \e => escape
|
13
|
+
# 30 => color base
|
14
|
+
# 1 => bright
|
15
|
+
# 0 => normal
|
16
|
+
#
|
17
|
+
# For HTML coloring we use <kbd> tag instead of <span> to require monospace
|
18
|
+
# font. Note that beloved <tt> has been removed from HTML5.
|
19
|
+
#
|
20
|
+
%w[gray red green yellow blue purple cyan white].zip(
|
21
|
+
%w[black darkred darkgreen brown navy darkmagenta darkcyan slategray]
|
22
|
+
).each_with_index do |(color, shade), i|
|
23
|
+
# NOTE: Format strings are created once only, for performance, and remembered by closures.
|
24
|
+
|
25
|
+
term_bright_seq = "\e[1;#{30 + i}m%s\e[0m"
|
26
|
+
html_bright_seq = %(<kbd style="color:#{color}">%s</kbd>)
|
27
|
+
|
28
|
+
define_method color do |html = false, *|
|
29
|
+
(html ? html_bright_seq : term_bright_seq) % self
|
30
|
+
end
|
31
|
+
|
32
|
+
term_normal_seq = "\e[0;#{30 + i}m%s\e[0m"
|
33
|
+
html_normal_seq = %(<kbd style="color:#{shade}">%s</kbd>)
|
34
|
+
|
35
|
+
define_method "#{color}ish" do |html = false, *|
|
36
|
+
(html ? html_normal_seq : term_normal_seq) % self
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
alias black grayish
|
41
|
+
alias pale whiteish
|
42
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module AmazingPrint
|
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
|
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
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
2
|
+
#
|
3
|
+
# AmazingPrint is freely distributable under the terms of MIT license.
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
|
+
#------------------------------------------------------------------------------
|
6
|
+
module AmazingPrint
|
7
|
+
module ActionView
|
8
|
+
# Use HTML colors and add default "debug_dump" class to the resulting HTML.
|
9
|
+
def ap_debug(object, options = {})
|
10
|
+
object.ai(
|
11
|
+
options.merge(html: true)
|
12
|
+
).sub(
|
13
|
+
/^<pre([\s>])/,
|
14
|
+
'<pre class="debug_dump"\\1'
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
alias ap ap_debug
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
ActionView::Base.include AmazingPrint::ActionView
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# Copyright (c) 2010-2016 Michael Dvorkin and contributors
|
2
|
+
#
|
3
|
+
# AmazingPrint is freely distributable under the terms of MIT license.
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
|
+
#------------------------------------------------------------------------------
|
6
|
+
module AmazingPrint
|
7
|
+
module ActiveRecord
|
8
|
+
def self.included(base)
|
9
|
+
base.send :alias_method, :cast_without_active_record, :cast
|
10
|
+
base.send :alias_method, :cast, :cast_with_active_record
|
11
|
+
end
|
12
|
+
|
13
|
+
# Add ActiveRecord class names to the dispatcher pipeline.
|
14
|
+
#------------------------------------------------------------------------------
|
15
|
+
def cast_with_active_record(object, type)
|
16
|
+
cast = cast_without_active_record(object, type)
|
17
|
+
return cast unless defined?(::ActiveRecord::Base)
|
18
|
+
|
19
|
+
if object.is_a?(::ActiveRecord::Base)
|
20
|
+
cast = :active_record_instance
|
21
|
+
elsif object.is_a?(::ActiveModel::Errors)
|
22
|
+
cast = :active_model_error
|
23
|
+
elsif object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base)
|
24
|
+
cast = :active_record_class
|
25
|
+
elsif type == :activerecord_relation || object.class.ancestors.include?(::ActiveRecord::Relation)
|
26
|
+
cast = :array
|
27
|
+
end
|
28
|
+
cast
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
# Format ActiveRecord instance object.
|
34
|
+
#
|
35
|
+
# NOTE: by default only instance attributes (i.e. columns) are shown. To format
|
36
|
+
# ActiveRecord instance as regular object showing its instance variables and
|
37
|
+
# accessors use :raw => true option:
|
38
|
+
#
|
39
|
+
# ap record, :raw => true
|
40
|
+
#
|
41
|
+
#------------------------------------------------------------------------------
|
42
|
+
def awesome_active_record_instance(object)
|
43
|
+
return object.inspect unless defined?(::ActiveSupport::OrderedHash)
|
44
|
+
return awesome_object(object) if @options[:raw]
|
45
|
+
|
46
|
+
data = if object.class.column_names != object.attributes.keys
|
47
|
+
object.attributes
|
48
|
+
else
|
49
|
+
object.class.column_names.each_with_object(::ActiveSupport::OrderedHash.new) do |name, hash|
|
50
|
+
if object.has_attribute?(name) || object.new_record?
|
51
|
+
value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name)
|
52
|
+
hash[name.to_sym] = value
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
"#{object} " << awesome_hash(data)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Format ActiveRecord class object.
|
60
|
+
#------------------------------------------------------------------------------
|
61
|
+
def awesome_active_record_class(object)
|
62
|
+
if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:columns) || object.to_s == 'ActiveRecord::Base'
|
63
|
+
return object.inspect
|
64
|
+
end
|
65
|
+
if object.respond_to?(:abstract_class?) && object.abstract_class?
|
66
|
+
return awesome_class(object)
|
67
|
+
end
|
68
|
+
|
69
|
+
data = object.columns.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
|
70
|
+
hash[c.name.to_sym] = c.type
|
71
|
+
end
|
72
|
+
|
73
|
+
name = "class #{awesome_simple(object.to_s, :class)}"
|
74
|
+
base = "< #{awesome_simple(object.superclass.to_s, :class)}"
|
75
|
+
|
76
|
+
[name, base, awesome_hash(data)].join(' ')
|
77
|
+
end
|
78
|
+
|
79
|
+
# Format ActiveModel error object.
|
80
|
+
#------------------------------------------------------------------------------
|
81
|
+
def awesome_active_model_error(object)
|
82
|
+
return object.inspect unless defined?(::ActiveSupport::OrderedHash)
|
83
|
+
return awesome_object(object) if @options[:raw]
|
84
|
+
|
85
|
+
object_dump = object.marshal_dump.first
|
86
|
+
data = if object_dump.class.column_names != object_dump.attributes.keys
|
87
|
+
object_dump.attributes
|
88
|
+
else
|
89
|
+
object_dump.class.column_names.each_with_object(::ActiveSupport::OrderedHash.new) do |name, hash|
|
90
|
+
if object_dump.has_attribute?(name) || object_dump.new_record?
|
91
|
+
value = object_dump.respond_to?(name) ? object_dump.send(name) : object_dump.read_attribute(name)
|
92
|
+
hash[name.to_sym] = value
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
data.merge!({ details: object.details, messages: object.messages })
|
98
|
+
"#{object} " << awesome_hash(data)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
AmazingPrint::Formatter.include AmazingPrint::ActiveRecord
|