awesome_print 1.7.0 → 1.8.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.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/Appraisals +11 -0
  4. data/CHANGELOG.md +10 -0
  5. data/CONTRIBUTING.md +1 -1
  6. data/README.md +47 -38
  7. data/Rakefile +5 -5
  8. data/lib/ap.rb +2 -2
  9. data/lib/awesome_print.rb +18 -17
  10. data/lib/awesome_print/colorize.rb +1 -1
  11. data/lib/awesome_print/core_ext/{array.rb → awesome_method_array.rb} +16 -18
  12. data/lib/awesome_print/core_ext/class.rb +3 -2
  13. data/lib/awesome_print/core_ext/kernel.rb +1 -1
  14. data/lib/awesome_print/core_ext/logger.rb +1 -1
  15. data/lib/awesome_print/core_ext/method.rb +2 -2
  16. data/lib/awesome_print/core_ext/object.rb +3 -2
  17. data/lib/awesome_print/core_ext/string.rb +3 -3
  18. data/lib/awesome_print/custom_defaults.rb +57 -0
  19. data/lib/awesome_print/ext/action_view.rb +8 -4
  20. data/lib/awesome_print/ext/active_record.rb +19 -11
  21. data/lib/awesome_print/ext/active_support.rb +1 -1
  22. data/lib/awesome_print/ext/mongo_mapper.rb +16 -13
  23. data/lib/awesome_print/ext/mongoid.rb +8 -6
  24. data/lib/awesome_print/ext/nobrainer.rb +8 -5
  25. data/lib/awesome_print/ext/nokogiri.rb +4 -4
  26. data/lib/awesome_print/ext/ostruct.rb +1 -1
  27. data/lib/awesome_print/ext/ripple.rb +5 -6
  28. data/lib/awesome_print/ext/sequel.rb +7 -6
  29. data/lib/awesome_print/formatter.rb +11 -19
  30. data/lib/awesome_print/formatters.rb +15 -0
  31. data/lib/awesome_print/formatters/array_formatter.rb +108 -42
  32. data/lib/awesome_print/formatters/base_formatter.rb +13 -11
  33. data/lib/awesome_print/formatters/class_formatter.rb +2 -1
  34. data/lib/awesome_print/formatters/dir_formatter.rb +1 -1
  35. data/lib/awesome_print/formatters/file_formatter.rb +1 -1
  36. data/lib/awesome_print/formatters/hash_formatter.rb +74 -22
  37. data/lib/awesome_print/formatters/object_formatter.rb +9 -14
  38. data/lib/awesome_print/formatters/struct_formatter.rb +71 -0
  39. data/lib/awesome_print/inspector.rb +77 -93
  40. data/lib/awesome_print/version.rb +2 -2
  41. data/spec/active_record_helper.rb +8 -2
  42. data/spec/colors_spec.rb +30 -30
  43. data/spec/core_ext/logger_spec.rb +43 -0
  44. data/spec/core_ext/string_spec.rb +20 -0
  45. data/spec/ext/action_view_spec.rb +18 -0
  46. data/spec/ext/active_record_spec.rb +252 -0
  47. data/spec/ext/active_support_spec.rb +26 -0
  48. data/spec/ext/mongo_mapper_spec.rb +261 -0
  49. data/spec/ext/mongoid_spec.rb +104 -0
  50. data/spec/ext/nobrainer_spec.rb +59 -0
  51. data/spec/ext/nokogiri_spec.rb +46 -0
  52. data/spec/ext/ostruct_spec.rb +22 -0
  53. data/spec/ext/ripple_spec.rb +48 -0
  54. data/spec/formats_spec.rb +193 -165
  55. data/spec/methods_spec.rb +116 -128
  56. data/spec/misc_spec.rb +104 -108
  57. data/spec/objects_spec.rb +70 -28
  58. data/spec/spec_helper.rb +27 -10
  59. data/spec/support/active_record_data.rb +20 -0
  60. data/spec/support/active_record_data/3_2_diana.txt +24 -0
  61. data/spec/support/active_record_data/3_2_diana_legacy.txt +24 -0
  62. data/spec/support/active_record_data/3_2_multi.txt +50 -0
  63. data/spec/support/active_record_data/3_2_multi_legacy.txt +50 -0
  64. data/spec/support/active_record_data/4_0_diana.txt +98 -0
  65. data/spec/support/active_record_data/4_0_multi.txt +198 -0
  66. data/spec/support/active_record_data/4_1_diana.txt +97 -0
  67. data/spec/support/active_record_data/4_1_multi.txt +196 -0
  68. data/spec/support/active_record_data/4_2_diana.txt +109 -0
  69. data/spec/support/active_record_data/4_2_diana_legacy.txt +109 -0
  70. data/spec/support/active_record_data/4_2_multi.txt +220 -0
  71. data/spec/support/active_record_data/4_2_multi_legacy.txt +220 -0
  72. data/spec/support/active_record_data/5_0_diana.txt +105 -0
  73. data/spec/support/active_record_data/5_0_multi.txt +212 -0
  74. data/spec/support/ext_verifier.rb +42 -0
  75. data/spec/support/mongoid_versions.rb +22 -0
  76. data/spec/support/rails_versions.rb +35 -0
  77. metadata +79 -4
@@ -0,0 +1,15 @@
1
+ module AwesomePrint
2
+ module Formatters
3
+ require 'awesome_print/formatters/object_formatter'
4
+ require 'awesome_print/formatters/struct_formatter'
5
+ require 'awesome_print/formatters/hash_formatter'
6
+ require 'awesome_print/formatters/array_formatter'
7
+ require 'awesome_print/formatters/simple_formatter'
8
+ require 'awesome_print/formatters/method_formatter'
9
+ require 'awesome_print/formatters/class_formatter'
10
+ require 'awesome_print/formatters/dir_formatter'
11
+ require 'awesome_print/formatters/file_formatter'
12
+ require 'awesome_print/colorize'
13
+ end
14
+ end
15
+
@@ -3,7 +3,6 @@ require_relative 'base_formatter'
3
3
  module AwesomePrint
4
4
  module Formatters
5
5
  class ArrayFormatter < BaseFormatter
6
-
7
6
  attr_reader :array, :inspector, :options
8
7
 
9
8
  def initialize(array, inspector)
@@ -13,60 +12,127 @@ module AwesomePrint
13
12
  end
14
13
 
15
14
  def format
16
- return "[]" if array == []
17
-
18
- if array.instance_variable_defined?('@__awesome_methods__')
19
- methods_array(array)
20
- elsif options[:multiline]
21
- width = (array.size - 1).to_s.size
22
-
23
- data = array.inject([]) do |arr, item|
24
- index = indent
25
- index << colorize("[#{arr.size.to_s.rjust(width)}] ", :array) if options[:index]
26
- indented do
27
- arr << (index << inspector.awesome(item))
28
- end
29
- end
30
-
31
- data = limited(data, width) if should_be_limited?
32
- "[\n" << data.join(",\n") << "\n#{outdent}]"
15
+ if array.empty?
16
+ '[]'
17
+ elsif methods_array?
18
+ methods_array
33
19
  else
34
- "[ " << array.map{ |item| inspector.awesome(item) }.join(", ") << " ]"
20
+ simple_array
35
21
  end
36
22
  end
37
23
 
38
24
  private
39
25
 
40
- def methods_array(a)
41
- a.sort! { |x, y| x.to_s <=> y.to_s } # Can't simply a.sort! because of o.methods << [ :blah ]
42
- object = a.instance_variable_get('@__awesome_methods__')
43
- tuples = a.map do |name|
44
- if name.is_a?(Symbol) || name.is_a?(String) # Ignore garbage, ex. 42.methods << [ :blah ]
45
- tuple = if object.respond_to?(name, true) # Is this a regular method?
46
- the_method = object.method(name) rescue nil # Avoid potential ArgumentError if object#method is overridden.
47
- if the_method && the_method.respond_to?(:arity) # Is this original object#method?
48
- method_tuple(the_method) # Yes, we are good.
49
- end
50
- elsif object.respond_to?(:instance_method) # Is this an unbound method?
51
- method_tuple(object.instance_method(name)) rescue nil # Rescue to avoid NameError when the method is not
52
- end # available (ex. File.lchmod on Ubuntu 12).
26
+ def methods_array?
27
+ array.instance_variable_defined?('@__awesome_methods__')
28
+ end
29
+
30
+ def simple_array
31
+ if options[:multiline]
32
+ multiline_array
33
+ else
34
+ '[ ' << array.map { |item| inspector.awesome(item) }.join(', ') << ' ]'
35
+ end
36
+ end
37
+
38
+ def multiline_array
39
+ data = unless should_be_limited?
40
+ generate_printable_array
41
+ else
42
+ limited(generate_printable_array, width(array))
43
+ end
44
+
45
+ %Q([\n#{data.join(",\n")}\n#{outdent}])
46
+ end
47
+
48
+ def generate_printable_array
49
+ array.map.with_index do |item, index|
50
+ array_prefix(index, width(array)).tap do |temp|
51
+ indented { temp << inspector.awesome(item) }
53
52
  end
54
- tuple || [ name.to_s, '(?)', '?' ] # Return WTF default if all the above fails.
55
53
  end
54
+ end
55
+
56
+ def array_prefix(iteration, width)
57
+ generic_prefix(iteration, width)
58
+ end
59
+
60
+ def methods_array
61
+ array.map!(&:to_s).sort!
56
62
 
57
- width = (tuples.size - 1).to_s.size
58
- name_width = tuples.map { |item| item[0].size }.max || 0
59
- args_width = tuples.map { |item| item[1].size }.max || 0
63
+ data = generate_printable_tuples.join("\n")
60
64
 
61
- data = tuples.inject([]) do |arr, item|
62
- index = indent
63
- index << "[#{arr.size.to_s.rjust(width)}]" if @options[:index]
64
- indented do
65
- arr << "#{index} #{colorize(item[0].rjust(name_width), :method)}#{colorize(item[1].ljust(args_width), :args)} #{colorize(item[2], :class)}"
65
+ "[\n#{data}\n#{outdent}]"
66
+ end
67
+
68
+ def generate_printable_tuples
69
+ tuples.map.with_index do |item, index|
70
+ tuple_prefix(index, width(tuples)).tap do |temp|
71
+ indented { temp << tuple_template(item) }
66
72
  end
67
73
  end
74
+ end
75
+
76
+ def tuple_template(item)
77
+ name_width, args_width = name_and_args_width
78
+
79
+ [
80
+ colorize(item[0].rjust(name_width), :method),
81
+ colorize(item[1].ljust(args_width), :args),
82
+ ' ',
83
+ colorize(item[2], :class)
84
+ ].join
85
+ end
86
+
87
+ def tuples
88
+ @tuples ||= array.map { |name| generate_tuple(name) }
89
+ end
90
+
91
+ def name_and_args_width
92
+ name_and_args = tuples.transpose
93
+
94
+ return name_and_args[0].map(&:size).max, name_and_args[1].map(&:size).max
95
+ end
96
+
97
+ def tuple_prefix(iteration, width)
98
+ generic_prefix(iteration, width, ' ')
99
+ end
100
+
101
+ def generate_tuple(name)
102
+ meth = case name
103
+ when Symbol, String
104
+ find_method(name)
105
+ end
106
+
107
+ meth ? method_tuple(meth) : [name.to_s, '(?)', '?']
108
+ end
109
+
110
+ def find_method(name)
111
+ object = array.instance_variable_get('@__awesome_methods__')
112
+
113
+ meth = begin
114
+ object.method(name)
115
+ rescue NameError, ArgumentError
116
+ nil
117
+ end
118
+
119
+ meth || begin
120
+ object.instance_method(name)
121
+ rescue NameError
122
+ nil
123
+ end
124
+ end
125
+
126
+ def generic_prefix(iteration, width, padding='')
127
+ if options[:index]
128
+ indent + colorize("[#{iteration.to_s.rjust(width)}] ", :array)
129
+ else
130
+ indent + padding
131
+ end
132
+ end
68
133
 
69
- "[\n" << data.join("\n") << "\n#{outdent}]"
134
+ def width(items)
135
+ (items.size - 1).to_s.size
70
136
  end
71
137
  end
72
138
  end
@@ -1,4 +1,4 @@
1
- require_relative "../colorize"
1
+ require_relative '../colorize'
2
2
 
3
3
  module AwesomePrint
4
4
  module Formatters
@@ -28,7 +28,7 @@ module AwesomePrint
28
28
  # ]
29
29
  #------------------------------------------------------------------------------
30
30
  def should_be_limited?
31
- options[:limit] or (options[:limit].is_a?(Fixnum) and options[:limit] > 0)
31
+ options[:limit] or (options[:limit].is_a?(Integer) and options[:limit] > 0)
32
32
  end
33
33
 
34
34
  def get_limit_size
@@ -50,13 +50,13 @@ module AwesomePrint
50
50
  tail = head - (limit - 1) % 2
51
51
 
52
52
  # Add the proper elements to the temp array and format the separator.
53
- temp = data[0, head] + [ nil ] + data[-tail, tail]
53
+ temp = data[0, head] + [nil] + data[-tail, tail]
54
54
 
55
- if is_hash
56
- temp[head] = "#{indent}#{data[head].strip} .. #{data[data.length - tail - 1].strip}"
57
- else
58
- temp[head] = "#{indent}[#{head.to_s.rjust(width)}] .. [#{data.length - tail - 1}]"
59
- end
55
+ temp[head] = if is_hash
56
+ "#{indent}#{data[head].strip} .. #{data[data.length - tail - 1].strip}"
57
+ else
58
+ "#{indent}[#{head.to_s.rjust(width)}] .. [#{data.length - tail - 1}]"
59
+ end
60
60
 
61
61
  temp
62
62
  end
@@ -65,7 +65,8 @@ module AwesomePrint
65
65
 
66
66
  def method_tuple(method)
67
67
  if method.respond_to?(:parameters) # Ruby 1.9.2+
68
- # See http://ruby.runpaint.org/methods#method-objects-parameters
68
+ # See http://readruby.chengguangnan.com/methods#method-objects-parameters
69
+ # (mirror: http://archive.is/XguCA#selection-3381.1-3381.11)
69
70
  args = method.parameters.inject([]) do |arr, (type, name)|
70
71
  name ||= (type == :block ? 'block' : "arg#{arr.size + 1}")
71
72
  arr << case type
@@ -90,14 +91,15 @@ module AwesomePrint
90
91
  # #<UnboundMethod: Hello#world>
91
92
  #
92
93
  if method.to_s =~ /(Unbound)*Method: (.*)[#\.]/
93
- unbound, klass = $1 && '(unbound)', $2
94
+ unbound = $1 && '(unbound)'
95
+ klass = $2
94
96
  if klass && klass =~ /(\(\w+:\s.*?\))/ # Is this ActiveRecord-style class?
95
97
  klass.sub!($1, '') # Yes, strip the fields leaving class name only.
96
98
  end
97
99
  owner = "#{klass}#{unbound}".gsub('(', ' (')
98
100
  end
99
101
 
100
- [ method.name.to_s, "(#{args.join(', ')})", owner.to_s ]
102
+ [method.name.to_s, "(#{args.join(', ')})", owner.to_s]
101
103
  end
102
104
 
103
105
  #
@@ -13,7 +13,8 @@ module AwesomePrint
13
13
  end
14
14
 
15
15
  def format
16
- if superclass = klass.superclass # <-- Assign and test if nil.
16
+ superclass = klass.superclass
17
+ if superclass
17
18
  colorize("#{klass.inspect} < #{superclass}", :class)
18
19
  else
19
20
  colorize(klass.inspect, :class)
@@ -1,5 +1,5 @@
1
1
  require_relative 'base_formatter'
2
- require "shellwords"
2
+ require 'shellwords'
3
3
 
4
4
  module AwesomePrint
5
5
  module Formatters
@@ -1,5 +1,5 @@
1
1
  require_relative 'base_formatter'
2
- require "shellwords"
2
+ require 'shellwords'
3
3
 
4
4
  module AwesomePrint
5
5
  module Formatters
@@ -3,7 +3,6 @@ require_relative 'base_formatter'
3
3
  module AwesomePrint
4
4
  module Formatters
5
5
  class HashFormatter < BaseFormatter
6
-
7
6
  attr_reader :hash, :inspector, :options
8
7
 
9
8
  def initialize(hash, inspector)
@@ -13,41 +12,94 @@ module AwesomePrint
13
12
  end
14
13
 
15
14
  def format
16
- return "{}" if hash == {}
17
-
18
- keys = hash.keys
19
- keys = keys.sort { |a, b| a.to_s <=> b.to_s } if options[:sort_keys]
20
- data = keys.map do |key|
21
- plain_single_line do
22
- [ inspector.awesome(key), hash[key] ]
23
- end
15
+ if hash.empty?
16
+ empty_hash
17
+ elsif multiline_hash?
18
+ multiline_hash
19
+ else
20
+ simple_hash
24
21
  end
22
+ end
23
+
24
+ private
25
25
 
26
- width = data.map { |key, | key.size }.max || 0
27
- width += indentation if options[:indent] > 0
26
+ def empty_hash
27
+ '{}'
28
+ end
29
+
30
+ def multiline_hash?
31
+ options[:multiline]
32
+ end
33
+
34
+ def multiline_hash
35
+ "{\n" << printable_hash.join(",\n") << "\n#{outdent}}"
36
+ end
28
37
 
29
- data = data.map do |key, value|
38
+ def simple_hash
39
+ "{ #{printable_hash.join(', ')} }"
40
+ end
41
+
42
+ def printable_hash
43
+ data = printable_keys
44
+ width = left_width(data)
45
+
46
+ data.map! do |key, value|
30
47
  indented do
31
- align(key, width) << colorize(" => ", :hash) << inspector.awesome(value)
48
+ if options[:ruby19_syntax] && symbol?(key)
49
+ ruby19_syntax(key, value, width)
50
+ else
51
+ pre_ruby19_syntax(key, value, width)
52
+ end
32
53
  end
33
54
  end
34
55
 
35
- data = limited(data, width, :hash => true) if should_be_limited?
36
- if options[:multiline]
37
- "{\n" << data.join(",\n") << "\n#{outdent}}"
38
- else
39
- "{ #{data.join(', ')} }"
56
+ should_be_limited? ? limited(data, width, hash: true) : data
57
+ end
58
+
59
+ def left_width(keys)
60
+ result = max_key_width(keys)
61
+ result += indentation if options[:indent] > 0
62
+ result
63
+ end
64
+
65
+ def max_key_width(keys)
66
+ keys.map { |key, _value| key.size }.max || 0
67
+ end
68
+
69
+ def printable_keys
70
+ keys = hash.keys
71
+
72
+ keys.sort! { |a, b| a.to_s <=> b.to_s } if options[:sort_keys]
73
+
74
+ keys.map! do |key|
75
+ plain_single_line do
76
+ [inspector.awesome(key), hash[key]]
77
+ end
40
78
  end
41
79
  end
42
80
 
43
- private
81
+ def symbol?(key)
82
+ key[0] == ':'
83
+ end
84
+
85
+ def ruby19_syntax(key, value, width)
86
+ key[0] = ''
87
+ align(key, width - 1) << colorize(': ', :hash) << inspector.awesome(value)
88
+ end
89
+
90
+ def pre_ruby19_syntax(key, value, width)
91
+ align(key, width) << colorize(' => ', :hash) << inspector.awesome(value)
92
+ end
44
93
 
45
94
  def plain_single_line
46
- plain, multiline = options[:plain], options[:multiline]
47
- options[:plain], options[:multiline] = true, false
95
+ plain = options[:plain]
96
+ multiline = options[:multiline]
97
+ options[:plain] = true
98
+ options[:multiline] = false
48
99
  yield
49
100
  ensure
50
- options[:plain], options[:multiline] = plain, multiline
101
+ options[:plain] = plain
102
+ options[:multiline] = multiline
51
103
  end
52
104
  end
53
105
  end
@@ -6,9 +6,9 @@ module AwesomePrint
6
6
 
7
7
  attr_reader :object, :variables, :inspector, :options
8
8
 
9
- def initialize(object, variables, inspector)
9
+ def initialize(object, inspector)
10
10
  @object = object
11
- @variables = variables
11
+ @variables = object.instance_variables
12
12
  @inspector = inspector
13
13
  @options = inspector.options
14
14
  end
@@ -22,13 +22,13 @@ module AwesomePrint
22
22
  object.respond_to?(property) ? :reader : nil
23
23
  end
24
24
  if accessor
25
- [ "attr_#{accessor} :#{property}", var ]
25
+ ["attr_#{accessor} :#{property}", var]
26
26
  else
27
- [ var.to_s, var ]
27
+ [var.to_s, var]
28
28
  end
29
29
  end
30
30
 
31
- data = vars.sort.map do |declaration, var|
31
+ data = (options[:sort_vars] ? vars.sort : vars).map do |declaration, var|
32
32
  key = left_aligned do
33
33
  align(declaration, declaration.size)
34
34
  end
@@ -42,13 +42,7 @@ module AwesomePrint
42
42
  end
43
43
 
44
44
  indented do
45
- var_contents = if valid_instance_var?(var)
46
- object.instance_variable_get(var)
47
- else
48
- object.send(var) # Enables handling of Struct attributes
49
- end
50
-
51
- key << colorize(" = ", :hash) + inspector.awesome(var_contents)
45
+ key << colorize(' = ', :hash) + inspector.awesome(object.instance_variable_get(var))
52
46
  end
53
47
  end
54
48
 
@@ -59,7 +53,7 @@ module AwesomePrint
59
53
  end
60
54
  end
61
55
 
62
- private
56
+ private
63
57
 
64
58
  def valid_instance_var?(variable_name)
65
59
  variable_name.to_s.start_with?('@')
@@ -70,7 +64,8 @@ module AwesomePrint
70
64
  end
71
65
 
72
66
  def left_aligned
73
- current, options[:indent] = options[:indent], 0
67
+ current = options[:indent]
68
+ options[:indent] = 0
74
69
  yield
75
70
  ensure
76
71
  options[:indent] = current