awesome_print 1.9.2 → 2.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -1
  3. data/Appraisals +2 -33
  4. data/CHANGELOG.md +6 -15
  5. data/CONTRIBUTING.md +0 -1
  6. data/README.md +7 -49
  7. data/lib/ap.rb +1 -5
  8. data/lib/awesome_print.rb +7 -29
  9. data/lib/awesome_print/colorize.rb +2 -0
  10. data/lib/awesome_print/core_ext/active_support.rb +7 -0
  11. data/lib/awesome_print/core_ext/awesome_method_array.rb +0 -6
  12. data/lib/awesome_print/core_ext/class.rb +0 -5
  13. data/lib/awesome_print/core_ext/kernel.rb +0 -5
  14. data/lib/awesome_print/core_ext/logger.rb +0 -5
  15. data/lib/awesome_print/core_ext/object.rb +0 -5
  16. data/lib/awesome_print/core_ext/string.rb +0 -5
  17. data/lib/awesome_print/custom_defaults.rb +1 -1
  18. data/lib/awesome_print/formatter.rb +38 -106
  19. data/lib/awesome_print/formatters/array_formatter.rb +11 -8
  20. data/lib/awesome_print/formatters/base_formatter.rb +20 -53
  21. data/lib/awesome_print/formatters/bigdecimal_formatter.rb +20 -0
  22. data/lib/awesome_print/formatters/class_formatter.rb +5 -6
  23. data/lib/awesome_print/formatters/dir_formatter.rb +4 -6
  24. data/lib/awesome_print/formatters/ext/active_model_error_formatter.rb +44 -0
  25. data/lib/awesome_print/formatters/ext/active_record_attributeset_formatter.rb +27 -0
  26. data/lib/awesome_print/formatters/ext/active_record_class_formatter.rb +43 -0
  27. data/lib/awesome_print/formatters/ext/active_record_instance_formatter.rb +48 -0
  28. data/lib/awesome_print/formatters/ext/active_record_relation_formatter.rb +19 -0
  29. data/lib/awesome_print/formatters/ext/bson_objectid_formatter.rb +20 -0
  30. data/lib/awesome_print/formatters/ext/hash_with_indifferent_access_formatter.rb +20 -0
  31. data/lib/awesome_print/formatters/ext/mongoid_document_formatter.rb +59 -0
  32. data/lib/awesome_print/formatters/ext/nokogiri_document_formatter.rb +30 -0
  33. data/lib/awesome_print/formatters/ext/nokogiri_node_formatter.rb +29 -0
  34. data/lib/awesome_print/formatters/ext/nokogiri_nodeset_formatter.rb +32 -0
  35. data/lib/awesome_print/formatters/ext/sequel_dataset_formatter.rb +20 -0
  36. data/lib/awesome_print/formatters/ext/sequel_model_class_formatter.rb +25 -0
  37. data/lib/awesome_print/formatters/ext/sequel_model_formatter.rb +26 -0
  38. data/lib/awesome_print/formatters/ext/time_with_zone_formatter.rb +24 -0
  39. data/lib/awesome_print/formatters/fallback_formatter.rb +55 -0
  40. data/lib/awesome_print/formatters/false_class_formatter.rb +15 -0
  41. data/lib/awesome_print/formatters/file_formatter.rb +4 -6
  42. data/lib/awesome_print/formatters/hash_formatter.rb +12 -7
  43. data/lib/awesome_print/formatters/integer_formatter.rb +11 -0
  44. data/lib/awesome_print/formatters/method_formatter.rb +8 -6
  45. data/lib/awesome_print/formatters/module_formatter.rb +18 -0
  46. data/lib/awesome_print/formatters/nil_class_formatter.rb +19 -0
  47. data/lib/awesome_print/formatters/object_formatter.rb +19 -9
  48. data/lib/awesome_print/formatters/open_struct.rb +19 -0
  49. data/lib/awesome_print/formatters/range_formatter.rb +11 -0
  50. data/lib/awesome_print/formatters/rational_formatter.rb +15 -0
  51. data/lib/awesome_print/formatters/set_formatter.rb +19 -0
  52. data/lib/awesome_print/formatters/simple_formatter.rb +10 -8
  53. data/lib/awesome_print/formatters/string_formatter.rb +14 -0
  54. data/lib/awesome_print/formatters/struct_formatter.rb +8 -6
  55. data/lib/awesome_print/formatters/symbol_formatter.rb +18 -0
  56. data/lib/awesome_print/formatters/true_class_formatter.rb +15 -0
  57. data/lib/awesome_print/formatters/unbound_method_formatter.rb +15 -0
  58. data/lib/awesome_print/inspector.rb +18 -17
  59. data/lib/awesome_print/limiter.rb +63 -0
  60. data/lib/awesome_print/registrar.rb +20 -0
  61. data/lib/awesome_print/version.rb +6 -6
  62. data/spec/formatters/array_spec.rb +251 -0
  63. data/spec/formatters/big_decimal_spec.rb +28 -0
  64. data/spec/formatters/class_spec.rb +91 -0
  65. data/spec/formatters/dir_file_spec.rb +43 -0
  66. data/spec/{ext → formatters/ext}/active_record_spec.rb +14 -58
  67. data/spec/{ext → formatters/ext}/active_support_spec.rb +7 -11
  68. data/spec/{ext → formatters/ext}/mongoid_spec.rb +2 -2
  69. data/spec/{ext → formatters/ext}/nokogiri_spec.rb +0 -0
  70. data/spec/formatters/hash_spec.rb +284 -0
  71. data/spec/{methods_spec.rb → formatters/methods_spec.rb} +3 -5
  72. data/spec/{objects_spec.rb → formatters/objects_spec.rb} +0 -0
  73. data/spec/{ext → formatters}/ostruct_spec.rb +1 -0
  74. data/spec/formatters/set_spec.rb +49 -0
  75. data/spec/formatters/struct_spec.rb +61 -0
  76. data/spec/merge_options_spec.rb +13 -0
  77. data/spec/misc_spec.rb +3 -10
  78. data/spec/spec_helper.rb +2 -9
  79. data/spec/support/ext_verifier.rb +0 -15
  80. data/spec/support/mongoid_versions.rb +4 -8
  81. data/spec/support/rails_versions.rb +2 -32
  82. metadata +100 -78
  83. data/awesome_print.gemspec +0 -33
  84. data/init.rb +0 -1
  85. data/lib/awesome_print/core_ext/method.rb +0 -21
  86. data/lib/awesome_print/ext/action_view.rb +0 -22
  87. data/lib/awesome_print/ext/active_record.rb +0 -103
  88. data/lib/awesome_print/ext/active_support.rb +0 -47
  89. data/lib/awesome_print/ext/mongo_mapper.rb +0 -124
  90. data/lib/awesome_print/ext/mongoid.rb +0 -67
  91. data/lib/awesome_print/ext/nobrainer.rb +0 -52
  92. data/lib/awesome_print/ext/nokogiri.rb +0 -45
  93. data/lib/awesome_print/ext/ostruct.rb +0 -27
  94. data/lib/awesome_print/ext/ripple.rb +0 -71
  95. data/lib/awesome_print/ext/sequel.rb +0 -58
  96. data/spec/ext/action_view_spec.rb +0 -21
  97. data/spec/ext/mongo_mapper_spec.rb +0 -261
  98. data/spec/ext/nobrainer_spec.rb +0 -59
  99. data/spec/ext/ripple_spec.rb +0 -48
  100. data/spec/formats_spec.rb +0 -779
  101. data/spec/support/active_record_data/3_2_diana.txt +0 -24
  102. data/spec/support/active_record_data/3_2_diana_legacy.txt +0 -24
  103. data/spec/support/active_record_data/3_2_multi.txt +0 -50
  104. data/spec/support/active_record_data/3_2_multi_legacy.txt +0 -50
  105. data/spec/support/active_record_data/4_0_diana.txt +0 -98
  106. data/spec/support/active_record_data/4_0_multi.txt +0 -198
  107. data/spec/support/active_record_data/4_1_diana.txt +0 -97
  108. data/spec/support/active_record_data/4_1_multi.txt +0 -196
  109. data/spec/support/active_record_data/4_2_diana.txt +0 -109
  110. data/spec/support/active_record_data/4_2_diana_legacy.txt +0 -109
  111. data/spec/support/active_record_data/4_2_multi.txt +0 -220
  112. data/spec/support/active_record_data/4_2_multi_legacy.txt +0 -220
  113. data/spec/support/active_record_data/6_0_diana.txt +0 -104
  114. data/spec/support/active_record_data/6_0_multi.txt +0 -210
  115. data/spec/support/active_record_data/6_1_diana.txt +0 -109
  116. data/spec/support/active_record_data/6_1_multi.txt +0 -220
@@ -0,0 +1,30 @@
1
+ require_relative '../base_formatter'
2
+
3
+ module AwesomePrint
4
+ module Formatters
5
+ class NokogiriDocumentFormatter < BaseFormatter
6
+
7
+ formatter_for :nokogiri_xml_document
8
+
9
+ def self.formattable?(object)
10
+ defined?(::Nokogiri) && object.is_a?(::Nokogiri::XML::Document)
11
+ end
12
+
13
+ def format(object)
14
+
15
+ xml = object.to_xml(indent: 2)
16
+ #
17
+ # Colorize tag, id/class name, and contents.
18
+ #
19
+ xml.gsub!(/(<)(\/?[A-Za-z1-9]+)/) { |tag| "#{$1}#{colorize($2, :keyword)}" }
20
+ xml.gsub!(/(id|class)="[^"]+"/i) { |id| colorize(id, :class) }
21
+ xml.gsub!(/>([^<]+)</) do |contents|
22
+ contents = colorize($1, :trueclass) if contents && !contents.empty?
23
+ ">#{contents}<"
24
+ end
25
+ xml
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ require_relative '../base_formatter'
2
+
3
+ module AwesomePrint
4
+ module Formatters
5
+ class NokogiriNodeFormatter < BaseFormatter
6
+
7
+ formatter_for :nokogiri_xml_node
8
+
9
+ def self.formattable?(object)
10
+ defined?(::Nokogiri) && object.is_a?(::Nokogiri::XML::Node)
11
+ end
12
+
13
+ def format(object)
14
+ xml = object.to_xml(indent: 2)
15
+ #
16
+ # Colorize tag, id/class name, and contents.
17
+ #
18
+ xml.gsub!(/(<)(\/?[A-Za-z1-9]+)/) { |tag| "#{$1}#{colorize($2, :keyword)}" }
19
+ xml.gsub!(/(id|class)="[^"]+"/i) { |id| colorize(id, :class) }
20
+ xml.gsub!(/>([^<]+)</) do |contents|
21
+ contents = colorize($1, :trueclass) if contents && !contents.empty?
22
+ ">#{contents}<"
23
+ end
24
+ xml
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,32 @@
1
+ require_relative '../base_formatter'
2
+
3
+ module AwesomePrint
4
+ module Formatters
5
+ class NokogiriNodesetFormatter < BaseFormatter
6
+
7
+ formatter_for :nokogiri_xml_nodeset
8
+
9
+ def self.formattable?(object)
10
+ defined?(::Nokogiri) && object.is_a?(::Nokogiri::XML::NodeSet)
11
+ end
12
+
13
+ def format(object)
14
+
15
+ return '[]' if object.empty?
16
+
17
+ xml = object.to_xml(indent: 2)
18
+ #
19
+ # Colorize tag, id/class name, and contents.
20
+ #
21
+ xml.gsub!(/(<)(\/?[A-Za-z1-9]+)/) { |tag| "#{$1}#{colorize($2, :keyword)}" }
22
+ xml.gsub!(/(id|class)="[^"]+"/i) { |id| colorize(id, :class) }
23
+ xml.gsub!(/>([^<]+)</) do |contents|
24
+ contents = colorize($1, :trueclass) if contents && !contents.empty?
25
+ ">#{contents}<"
26
+ end
27
+ xml
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,20 @@
1
+ require_relative '../base_formatter'
2
+
3
+ module AwesomePrint
4
+ module Formatters
5
+ class SequelDatasetFormatter < BaseFormatter
6
+
7
+ formatter_for :sequel_dataset
8
+
9
+ def self.formattable?(object)
10
+ defined?(::Sequel::Mysql2::Dataset) && object.class.ancestors.include?(::Sequel::Mysql2::Dataset)
11
+ end
12
+
13
+ def format(object)
14
+ af = Formatters::ArrayFormatter.new(@inspector)
15
+ [af.format(dataset.to_a), colorize(dataset.sql, :string)].join("\n")
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ require_relative '../base_formatter'
2
+
3
+ module AwesomePrint
4
+ module Formatters
5
+ class SequelModelClassFormatter < BaseFormatter
6
+
7
+ formatter_for :sequel_model_class
8
+
9
+ def self.formattable?(object)
10
+ defined?(::Sequel::Model) && object.is_a?(Class) && object.ancestors.include?(::Sequel::Model)
11
+ end
12
+
13
+ def format(object)
14
+ data = object.db_schema.inject({}) { |h, (prop, defn)| h.merge(prop => defn[:db_type]) }
15
+
16
+ [
17
+ "class #{colorize(object.to_s, :class)}",
18
+ "< #{colorize(object.superclass.to_s, :class)}",
19
+ Formatters::HashFormatter.new(@inspector).format(data)
20
+ ].join(' ')
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../base_formatter'
2
+
3
+ module AwesomePrint
4
+ module Formatters
5
+ class SequelModelFormatter < BaseFormatter
6
+
7
+ formatter_for :sequel_model
8
+
9
+ def self.formattable?(object)
10
+ defined?(::Sequel::Model) && object.is_a?(::Sequel::Model)
11
+ end
12
+
13
+ def format(object)
14
+ data = object.values.sort_by { |key| key.to_s }.inject({}) do |hash, c|
15
+ hash[c[0].to_sym] = c[1]
16
+ hash
17
+ end
18
+ data = { errors: object.errors, values: data } if !object.errors.empty?
19
+ hf = Formatters::HashFormatter.new(@inspector)
20
+
21
+ "#{colorize(object.to_s, :sequel)} #{hf.format(data)}"
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ require_relative '../base_formatter'
2
+ require 'date'
3
+
4
+ module AwesomePrint
5
+ module Formatters
6
+ class TimeWithZoneFormatter < BaseFormatter
7
+
8
+ formatter_for :time_with_zone
9
+
10
+ # FIXME: should date/time/datetime not become more formal core formatters
11
+ # [maybe with tz info and other more useful things]?
12
+
13
+ def self.formattable?(object)
14
+ (defined?(::ActiveSupport::TimeWithZone) && object.is_a?(::ActiveSupport::TimeWithZone)) ||
15
+ object.is_a?(::DateTime) || object.is_a?(::Time) || object.is_a?(::Date)
16
+ end
17
+
18
+ def format(object)
19
+ colorize(object.inspect, :time)
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,55 @@
1
+ require_relative 'base_formatter'
2
+
3
+ # this handles some fallback logic to route things we don't know what they are
4
+
5
+ module AwesomePrint
6
+ module Formatters
7
+ class FallbackFormatter < BaseFormatter
8
+
9
+ formatter_for :self
10
+
11
+ def self.formattable?(object)
12
+ true
13
+ end
14
+
15
+ def self.core?
16
+ true
17
+ end
18
+
19
+ def format(object)
20
+ if @options[:raw] && object.instance_variables.any?
21
+ Formatters::ObjectFormatter.new(@inspector).format(object)
22
+ elsif (hash = convert_to_hash(object))
23
+ Formatters::HashFormatter.new(@inspector).format(hash)
24
+ else
25
+ Formatters::SimpleFormatter.new(@inspector).format(object.inspect.to_s)
26
+ end
27
+ end
28
+
29
+
30
+ private
31
+
32
+ # Utility methods.
33
+ #------------------------------------------------------------------------------
34
+ # FIXME: this could be super fixed.
35
+ #
36
+ def convert_to_hash(object)
37
+ if !object.respond_to?(:to_hash)
38
+ return nil
39
+ end
40
+
41
+ if object.method(:to_hash).arity != 0
42
+ return nil
43
+ end
44
+
45
+ hash = object.to_hash
46
+ if !hash.respond_to?(:keys) || !hash.respond_to?('[]')
47
+ return nil
48
+ end
49
+
50
+ return hash
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'simple_formatter'
2
+
3
+ module AwesomePrint
4
+ module Formatters
5
+ class FalseClassFormatter < SimpleFormatter
6
+
7
+ formatter_for :falseclass
8
+
9
+ def self.formattable?(object)
10
+ object == false
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -5,15 +5,13 @@ module AwesomePrint
5
5
  module Formatters
6
6
  class FileFormatter < BaseFormatter
7
7
 
8
- attr_reader :file, :inspector, :options
8
+ formatter_for :file
9
9
 
10
- def initialize(file, inspector)
11
- @file = file
12
- @inspector = inspector
13
- @options = inspector.options
10
+ def self.formattable?(object)
11
+ object.is_a?(File)
14
12
  end
15
13
 
16
- def format
14
+ def format(file)
17
15
  ls = File.directory?(file) ? `ls -adlF #{file.path.shellescape}` : `ls -alF #{file.path.shellescape}`
18
16
  colorize(ls.empty? ? file.inspect : "#{file.inspect}\n#{ls.chop}", :file)
19
17
  end
@@ -3,15 +3,20 @@ require_relative 'base_formatter'
3
3
  module AwesomePrint
4
4
  module Formatters
5
5
  class HashFormatter < BaseFormatter
6
- attr_reader :hash, :inspector, :options
7
6
 
8
- def initialize(hash, inspector)
9
- @hash = hash
10
- @inspector = inspector
11
- @options = inspector.options
7
+ attr_reader :hash
8
+
9
+ formatter_for :hash
10
+
11
+ def self.formattable?(object)
12
+ object.is_a?(Hash)
12
13
  end
13
14
 
14
- def format
15
+ # INSTANCE METHODS BELOW
16
+
17
+ def format(hash)
18
+ @hash = hash
19
+
15
20
  if hash.empty?
16
21
  empty_hash
17
22
  elsif multiline_hash?
@@ -73,7 +78,7 @@ module AwesomePrint
73
78
 
74
79
  keys.map! do |key|
75
80
  plain_single_line do
76
- [String.new(inspector.awesome(key)), hash[key]]
81
+ [inspector.awesome(key), hash[key]]
77
82
  end
78
83
  end
79
84
  end
@@ -0,0 +1,11 @@
1
+ require_relative 'simple_formatter'
2
+
3
+ module AwesomePrint
4
+ module Formatters
5
+ class IntegerFormatter < SimpleFormatter
6
+
7
+ formatter_for :integer
8
+
9
+ end
10
+ end
11
+ end
@@ -4,15 +4,17 @@ module AwesomePrint
4
4
  module Formatters
5
5
  class MethodFormatter < BaseFormatter
6
6
 
7
- attr_reader :method, :inspector, :options
7
+ formatter_for :method
8
8
 
9
- def initialize(method, inspector)
10
- @method = method
11
- @inspector = inspector
12
- @options = inspector.options
9
+ def self.core?
10
+ true
13
11
  end
14
12
 
15
- def format
13
+ def self.formattable?(object)
14
+ object.is_a?(Method)
15
+ end
16
+
17
+ def format(method)
16
18
  name, args, owner = method_tuple(method)
17
19
 
18
20
  "#{colorize(owner, :class)}##{colorize(name, :method)}#{colorize(args, :args)}"
@@ -0,0 +1,18 @@
1
+ require_relative 'base_formatter'
2
+
3
+ module AwesomePrint
4
+ module Formatters
5
+ class ModuleFormatter < BaseFormatter
6
+
7
+ formatter_for :module
8
+
9
+ def self.formattable?(object)
10
+ object.is_a?(Module)
11
+ end
12
+
13
+ def format(object)
14
+ colorize(object.inspect, :module)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ require_relative 'simple_formatter'
2
+
3
+ module AwesomePrint
4
+ module Formatters
5
+ class NilClassFormatter < SimpleFormatter
6
+
7
+ formatter_for :nilclass
8
+
9
+ def self.formattable?(object)
10
+ object == nil
11
+ end
12
+
13
+ def format(object)
14
+ colorize('nil', self.class.formatted_object_type)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -4,16 +4,27 @@ module AwesomePrint
4
4
  module Formatters
5
5
  class ObjectFormatter < BaseFormatter
6
6
 
7
- attr_reader :object, :variables, :inspector, :options
7
+ formatter_for :object
8
8
 
9
- def initialize(object, inspector)
9
+ def self.core?
10
+ true
11
+ end
12
+
13
+ def self.formattable?(object)
14
+ object.is_a?(Object)
15
+ end
16
+
17
+ attr_reader :variables
18
+
19
+ def format(object)
10
20
  @object = object
11
21
  @variables = object.instance_variables
12
- @inspector = inspector
13
- @options = inspector.options
14
- end
15
22
 
16
- def format
23
+ # special case for ENV hashes, as they are objects but hash.
24
+ if object.to_s == 'ENV' && object.respond_to?(:to_h)
25
+ return Formatters::HashFormatter.new(@inspector).format(object.to_h)
26
+ end
27
+
17
28
  vars = variables.map do |var|
18
29
  property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet.
19
30
  accessor = if object.respond_to?(:"#{property}=")
@@ -22,7 +33,7 @@ module AwesomePrint
22
33
  object.respond_to?(property) ? :reader : nil
23
34
  end
24
35
  if accessor
25
- [String.new("attr_#{accessor} :#{property}"), var]
36
+ ["attr_#{accessor} :#{property}", var]
26
37
  else
27
38
  [var.to_s, var]
28
39
  end
@@ -60,8 +71,7 @@ module AwesomePrint
60
71
  end
61
72
 
62
73
  def awesome_instance
63
- str = String.new
64
- str << object.send(options[:class_name]).to_s
74
+ str = object.send(options[:class_name]).to_s
65
75
  str << ":0x%08x" % (object.__id__ * 2) if options[:object_id]
66
76
  str
67
77
  end