awesome_print 0.4.0 → 1.0.2

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 (44) hide show
  1. data/.gitignore +22 -0
  2. data/CHANGELOG +18 -0
  3. data/Gemfile +3 -0
  4. data/Gemfile.lock +26 -0
  5. data/README.md +64 -16
  6. data/Rakefile +2 -46
  7. data/lib/ap.rb +3 -17
  8. data/lib/{ap → awesome_print}/core_ext/class.rb +7 -2
  9. data/lib/{ap → awesome_print}/core_ext/kernel.rb +2 -2
  10. data/lib/awesome_print/core_ext/logger.rb +20 -0
  11. data/lib/{ap → awesome_print}/core_ext/object.rb +7 -2
  12. data/lib/{ap → awesome_print}/core_ext/string.rb +8 -5
  13. data/lib/awesome_print/ext/action_view.rb +18 -0
  14. data/lib/awesome_print/ext/active_record.rb +64 -0
  15. data/lib/awesome_print/ext/active_support.rb +47 -0
  16. data/lib/awesome_print/ext/mongo_mapper.rb +38 -0
  17. data/lib/awesome_print/ext/mongoid.rb +65 -0
  18. data/lib/awesome_print/ext/nokogiri.rb +45 -0
  19. data/lib/awesome_print/formatter.rb +384 -0
  20. data/lib/awesome_print/inspector.rb +140 -0
  21. data/{rails/init.rb → lib/awesome_print/version.rb} +5 -4
  22. data/lib/awesome_print.rb +16 -12
  23. data/spec/colors_spec.rb +106 -0
  24. data/spec/{awesome_print_spec.rb → formats_spec.rb} +187 -37
  25. data/spec/methods_spec.rb +30 -0
  26. data/spec/objects_spec.rb +79 -0
  27. data/spec/spec_helper.rb +1 -1
  28. metadata +49 -53
  29. data/VERSION +0 -1
  30. data/init.rb +0 -1
  31. data/lib/ap/awesome_print.rb +0 -352
  32. data/lib/ap/core_ext/logger.rb +0 -18
  33. data/lib/ap/mixin/action_view.rb +0 -17
  34. data/lib/ap/mixin/active_record.rb +0 -54
  35. data/lib/ap/mixin/active_support.rb +0 -46
  36. data/lib/ap/mixin/mongo_mapper.rb +0 -54
  37. data/spec/action_view_spec.rb +0 -25
  38. data/spec/active_record_spec.rb +0 -136
  39. data/spec/colorization_spec.rb +0 -84
  40. data/spec/logger_spec.rb +0 -43
  41. data/spec/mongo_mapper_spec.rb +0 -63
  42. data/spec/string_spec.rb +0 -20
  43. /data/lib/{ap → awesome_print}/core_ext/array.rb +0 -0
  44. /data/lib/{ap → awesome_print}/core_ext/method.rb +0 -0
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awesome_print
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 4
9
- - 0
10
- version: 0.4.0
4
+ prerelease:
5
+ version: 1.0.2
11
6
  platform: ruby
12
7
  authors:
13
8
  - Michael Dvorkin
@@ -15,8 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-05-13 00:00:00 -07:00
19
- default_executable:
13
+ date: 2011-12-20 00:00:00 Z
20
14
  dependencies:
21
15
  - !ruby/object:Gem::Dependency
22
16
  name: rspec
@@ -26,55 +20,59 @@ dependencies:
26
20
  requirements:
27
21
  - - ">="
28
22
  - !ruby/object:Gem::Version
29
- hash: 27
30
- segments:
31
- - 2
32
- - 5
33
- - 0
34
- version: 2.5.0
23
+ version: 2.6.0
35
24
  type: :development
36
25
  version_requirements: *id001
37
- description: "Great Ruby dubugging companion: pretty print Ruby objects to visualize their structure. Supports Rails ActiveRecord objects via included mixin."
26
+ - !ruby/object:Gem::Dependency
27
+ name: fakefs
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 0.2.1
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: "Great Ruby dubugging companion: pretty print Ruby objects to visualize their structure. Supports custom object formatting via plugins"
38
38
  email: mike@dvorkin.net
39
39
  executables: []
40
40
 
41
41
  extensions: []
42
42
 
43
- extra_rdoc_files:
44
- - LICENSE
45
- - README.md
43
+ extra_rdoc_files: []
44
+
46
45
  files:
47
46
  - CHANGELOG
47
+ - Gemfile
48
+ - Gemfile.lock
48
49
  - LICENSE
49
- - README.md
50
50
  - Rakefile
51
- - VERSION
52
- - init.rb
51
+ - README.md
53
52
  - lib/ap.rb
54
- - lib/ap/awesome_print.rb
55
- - lib/ap/core_ext/array.rb
56
- - lib/ap/core_ext/class.rb
57
- - lib/ap/core_ext/kernel.rb
58
- - lib/ap/core_ext/logger.rb
59
- - lib/ap/core_ext/method.rb
60
- - lib/ap/core_ext/object.rb
61
- - lib/ap/core_ext/string.rb
62
- - lib/ap/mixin/action_view.rb
63
- - lib/ap/mixin/active_record.rb
64
- - lib/ap/mixin/active_support.rb
65
- - lib/ap/mixin/mongo_mapper.rb
53
+ - lib/awesome_print/core_ext/array.rb
54
+ - lib/awesome_print/core_ext/class.rb
55
+ - lib/awesome_print/core_ext/kernel.rb
56
+ - lib/awesome_print/core_ext/logger.rb
57
+ - lib/awesome_print/core_ext/method.rb
58
+ - lib/awesome_print/core_ext/object.rb
59
+ - lib/awesome_print/core_ext/string.rb
60
+ - lib/awesome_print/ext/action_view.rb
61
+ - lib/awesome_print/ext/active_record.rb
62
+ - lib/awesome_print/ext/active_support.rb
63
+ - lib/awesome_print/ext/mongo_mapper.rb
64
+ - lib/awesome_print/ext/mongoid.rb
65
+ - lib/awesome_print/ext/nokogiri.rb
66
+ - lib/awesome_print/formatter.rb
67
+ - lib/awesome_print/inspector.rb
68
+ - lib/awesome_print/version.rb
66
69
  - lib/awesome_print.rb
67
- - rails/init.rb
68
- - spec/action_view_spec.rb
69
- - spec/active_record_spec.rb
70
- - spec/awesome_print_spec.rb
71
- - spec/colorization_spec.rb
72
- - spec/logger_spec.rb
70
+ - spec/colors_spec.rb
71
+ - spec/formats_spec.rb
73
72
  - spec/methods_spec.rb
74
- - spec/mongo_mapper_spec.rb
73
+ - spec/objects_spec.rb
75
74
  - spec/spec_helper.rb
76
- - spec/string_spec.rb
77
- has_rdoc: true
75
+ - .gitignore
78
76
  homepage: http://github.com/michaeldv/awesome_print
79
77
  licenses: []
80
78
 
@@ -88,25 +86,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
88
86
  requirements:
89
87
  - - ">="
90
88
  - !ruby/object:Gem::Version
91
- hash: 3
92
- segments:
93
- - 0
94
89
  version: "0"
95
90
  required_rubygems_version: !ruby/object:Gem::Requirement
96
91
  none: false
97
92
  requirements:
98
93
  - - ">="
99
94
  - !ruby/object:Gem::Version
100
- hash: 3
101
- segments:
102
- - 0
103
95
  version: "0"
104
96
  requirements: []
105
97
 
106
98
  rubyforge_project: awesome_print
107
- rubygems_version: 1.3.7
99
+ rubygems_version: 1.8.5
108
100
  signing_key:
109
101
  specification_version: 3
110
- summary: Pretty print Ruby objects with proper indentation and colors.
111
- test_files: []
112
-
102
+ summary: Pretty print Ruby objects with proper indentation and colors
103
+ test_files:
104
+ - spec/colors_spec.rb
105
+ - spec/formats_spec.rb
106
+ - spec/methods_spec.rb
107
+ - spec/objects_spec.rb
108
+ - spec/spec_helper.rb
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.4.0
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "lib", "awesome_print")
@@ -1,352 +0,0 @@
1
- # Copyright (c) 2010-2011 Michael Dvorkin
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
- require "cgi"
7
- require "shellwords"
8
-
9
- class AwesomePrint
10
- AP = :__awesome_print__ unless defined?(AwesomePrint::AP)
11
- CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational, :struct, :method, :unboundmethod ] unless defined?(AwesomePrint::CORE)
12
- @@force_colors = false
13
-
14
- def initialize(options = {})
15
- @options = {
16
- :multiline => true, # Display in multiple lines.
17
- :plain => false, # Use colors.
18
- :indent => 4, # Indent using 4 spaces.
19
- :index => true, # Display array indices.
20
- :html => false, # Use ANSI color codes rather than HTML.
21
- :sorted_hash_keys => false, # Do not sort hash keys.
22
- :color => {
23
- :array => :white,
24
- :bigdecimal => :blue,
25
- :class => :yellow,
26
- :date => :greenish,
27
- :falseclass => :red,
28
- :fixnum => :blue,
29
- :float => :blue,
30
- :hash => :pale,
31
- :struct => :pale,
32
- :nilclass => :red,
33
- :string => :yellowish,
34
- :symbol => :cyanish,
35
- :time => :greenish,
36
- :trueclass => :green,
37
- :method => :purpleish,
38
- :args => :pale
39
- }
40
- }
41
-
42
- # Merge custom defaults and let explicit options parameter override them.
43
- merge_custom_defaults!
44
- merge_options!(options)
45
-
46
- @indentation = @options[:indent].abs
47
- Thread.current[AP] ||= []
48
- end
49
-
50
-
51
- private
52
-
53
- # Format an array.
54
- #------------------------------------------------------------------------------
55
- def awesome_array(a)
56
- return "[]" if a == []
57
-
58
- if a.instance_variable_defined?('@__awesome_methods__')
59
- methods_array(a)
60
- elsif @options[:multiline]
61
- width = (a.size - 1).to_s.size
62
- data = a.inject([]) do |arr, item|
63
- index = if @options[:index]
64
- colorize("#{indent}[#{arr.size.to_s.rjust(width)}] ", :array)
65
- else
66
- colorize(indent, :array)
67
- end
68
- indented do
69
- arr << (index << awesome(item))
70
- end
71
- end
72
- "[\n" << data.join(",\n") << "\n#{outdent}]"
73
- else
74
- "[ " << a.map{ |item| awesome(item) }.join(", ") << " ]"
75
- end
76
- end
77
-
78
- # Format a hash. If @options[:indent] if negative left align hash keys.
79
- #------------------------------------------------------------------------------
80
- def awesome_hash(h)
81
- return "{}" if h == {}
82
-
83
- keys = @options[:sorted_hash_keys] ? h.keys.sort { |a, b| a.to_s <=> b.to_s } : h.keys
84
- data = keys.map do |key|
85
- plain_single_line do
86
- [ awesome(key), h[key] ]
87
- end
88
- end
89
-
90
- width = data.map { |key, | key.size }.max || 0
91
- width += @indentation if @options[:indent] > 0
92
-
93
- data = data.map do |key, value|
94
- if @options[:multiline]
95
- formatted_key = (@options[:indent] >= 0 ? key.rjust(width) : indent + key.ljust(width))
96
- else
97
- formatted_key = key
98
- end
99
- indented do
100
- formatted_key << colorize(" => ", :hash) << awesome(value)
101
- end
102
- end
103
- if @options[:multiline]
104
- "{\n" << data.join(",\n") << "\n#{outdent}}"
105
- else
106
- "{ #{data.join(', ')} }"
107
- end
108
- end
109
-
110
- # Format a Struct. If @options[:indent] is negative left align hash keys.
111
- #------------------------------------------------------------------------------
112
- def awesome_struct(s)
113
- h = {}
114
- s.each_pair { |k,v| h[k] = v }
115
- awesome_hash(h)
116
- end
117
-
118
- # Format Class object.
119
- #------------------------------------------------------------------------------
120
- def awesome_class(c)
121
- if superclass = c.superclass # <-- Assign and test if nil.
122
- awesome_self(c, :with => " < #{superclass}")
123
- else
124
- awesome_self(c)
125
- end
126
- end
127
-
128
- # Format File object.
129
- #------------------------------------------------------------------------------
130
- def awesome_file(f)
131
- ls = File.directory?(f) ? `ls -adlF #{f.path.shellescape}` : `ls -alF #{f.path.shellescape}`
132
- awesome_self(f, :with => ls.empty? ? nil : "\n#{ls.chop}")
133
- end
134
-
135
- # Format Dir object.
136
- #------------------------------------------------------------------------------
137
- def awesome_dir(d)
138
- ls = `ls -alF #{d.path.shellescape}`
139
- awesome_self(d, :with => ls.empty? ? nil : "\n#{ls.chop}")
140
- end
141
-
142
- # Format BigDecimal and Rational objects by convering them to Float.
143
- #------------------------------------------------------------------------------
144
- def awesome_bigdecimal(n)
145
- awesome_self(n.to_f, :as => :bigdecimal)
146
- end
147
- alias :awesome_rational :awesome_bigdecimal
148
-
149
- # Format a method.
150
- #------------------------------------------------------------------------------
151
- def awesome_method(m)
152
- name, args, owner = method_tuple(m)
153
- "#{colorize(owner, :class)}##{colorize(name, :method)}#{colorize(args, :args)}"
154
- end
155
- alias :awesome_unboundmethod :awesome_method
156
-
157
- # Catch all method to format an arbitrary object.
158
- #------------------------------------------------------------------------------
159
- def awesome_self(object, appear = {})
160
- colorize(object.inspect.to_s << appear[:with].to_s, appear[:as] || declassify(object))
161
- end
162
-
163
- # Dispatcher that detects data nesting and invokes object-aware formatter.
164
- #------------------------------------------------------------------------------
165
- def awesome(object)
166
- if Thread.current[AP].include?(object.object_id)
167
- nested(object)
168
- else
169
- begin
170
- Thread.current[AP] << object.object_id
171
- send(:"awesome_#{printable(object)}", object)
172
- ensure
173
- Thread.current[AP].pop
174
- end
175
- end
176
- end
177
-
178
- # Format object.methods array.
179
- #------------------------------------------------------------------------------
180
- def methods_array(a)
181
- object = a.instance_variable_get('@__awesome_methods__')
182
- tuples = a.map do |name|
183
- tuple = if object.respond_to?(name, true) # Is this a regular method?
184
- the_method = object.method(name) rescue nil # Avoid potential ArgumentError if object#method is overridden.
185
- if the_method && the_method.respond_to?(:arity) # Is this original object#method?
186
- method_tuple(the_method) # Yes, we are good.
187
- end
188
- elsif object.respond_to?(:instance_method) # Is this an unbound method?
189
- method_tuple(object.instance_method(name))
190
- end
191
- tuple || [ name.to_s, '(?)', '' ] # Return WTF default if all the above fails.
192
- end
193
-
194
- width = (tuples.size - 1).to_s.size
195
- name_width = tuples.map { |item| item[0].size }.max || 0
196
- args_width = tuples.map { |item| item[1].size }.max || 0
197
-
198
- data = tuples.inject([]) do |arr, item|
199
- index = if @options[:index]
200
- "#{indent}[#{arr.size.to_s.rjust(width)}]"
201
- else
202
- indent
203
- end
204
- indented do
205
- arr << "#{index} #{colorize(item[0].rjust(name_width), :method)}#{colorize(item[1].ljust(args_width), :args)} #{colorize(item[2], :class)}"
206
- end
207
- end
208
-
209
- "[\n" << data.join("\n") << "\n#{outdent}]"
210
- end
211
-
212
- # Format nested data, for example:
213
- # arr = [1, 2]; arr << arr
214
- # => [1,2, [...]]
215
- # hsh = { :a => 1 }; hsh[:b] = hsh
216
- # => { :a => 1, :b => {...} }
217
- #------------------------------------------------------------------------------
218
- def nested(object)
219
- case printable(object)
220
- when :array then colorize("[...]", :array)
221
- when :hash then colorize("{...}", :hash)
222
- when :struct then colorize("{...}", :struct)
223
- else colorize("...#{object.class}...", :class)
224
- end
225
- end
226
-
227
- # Return one of the "core" types that have a formatter of :self otherwise.
228
- #------------------------------------------------------------------------------
229
- def printable(object)
230
- CORE.grep(declassify(object))[0] || :self
231
- end
232
-
233
- # Turn class name into symbol, ex: Hello::World => :hello_world. Classes that
234
- # inherit from Array, Hash, File, Dir, and Struct are treated as the base class.
235
- #------------------------------------------------------------------------------
236
- def declassify(object)
237
- case object
238
- when Array then :array
239
- when Hash then :hash
240
- when File then :file
241
- when Dir then :dir
242
- when Struct then :struct
243
- else object.class.to_s.gsub(/:+/, "_").downcase.to_sym
244
- end
245
- end
246
-
247
- # Pick the color and apply it to the given string as necessary.
248
- #------------------------------------------------------------------------------
249
- def colorize(s, type)
250
- s = CGI.escapeHTML(s) if @options[:html]
251
- if @options[:plain] || !@options[:color][type] || !colorize?
252
- @options[:html] ? "<pre>#{s}</pre>" : s
253
- else
254
- s.send(@options[:color][type], @options[:html])
255
- end
256
- end
257
-
258
- # Return [ name, arguments, owner ] tuple for a given method.
259
- #------------------------------------------------------------------------------
260
- def method_tuple(method)
261
- if method.respond_to?(:parameters) # Ruby 1.9.2+
262
- # See http://ruby.runpaint.org/methods#method-objects-parameters
263
- args = method.parameters.inject([]) do |arr, (type, name)|
264
- name ||= (type == :block ? 'block' : "arg#{arr.size + 1}")
265
- arr << case type
266
- when :req then name.to_s
267
- when :opt, :rest then "*#{name}"
268
- when :block then "&#{name}"
269
- else '?'
270
- end
271
- end
272
- else # See http://ruby-doc.org/core/classes/Method.html#M001902
273
- args = (1..method.arity.abs).map { |i| "arg#{i}" }
274
- args[-1] = "*#{args[-1]}" if method.arity < 0
275
- end
276
-
277
- if method.to_s =~ /(Unbound)*Method: (.*?)[#\.]/
278
- owner = "#{$2}#{$1 ? '(unbound)' : ''}".gsub('(', ' (')
279
- end
280
-
281
- [ method.name.to_s, "(#{args.join(', ')})", owner.to_s ]
282
- end
283
-
284
- # Format hash keys as plain string regardless of underlying data type.
285
- #------------------------------------------------------------------------------
286
- def plain_single_line
287
- plain, multiline = @options[:plain], @options[:multiline]
288
- @options[:plain], @options[:multiline] = true, false
289
- yield
290
- ensure
291
- @options[:plain], @options[:multiline] = plain, multiline
292
- end
293
-
294
- #------------------------------------------------------------------------------
295
- def indented
296
- @indentation += @options[:indent].abs
297
- yield
298
- ensure
299
- @indentation -= @options[:indent].abs
300
- end
301
-
302
- def indent
303
- @indent = ' ' * @indentation
304
- end
305
-
306
- def outdent
307
- @outdent = ' ' * (@indentation - @options[:indent].abs)
308
- end
309
-
310
- # Update @options by first merging the :color hash and then the remaining keys.
311
- #------------------------------------------------------------------------------
312
- def merge_options!(options = {})
313
- @options[:color].merge!(options.delete(:color) || {})
314
- @options.merge!(options)
315
- end
316
-
317
- # Load ~/.aprc file with custom defaults that override default options.
318
- #------------------------------------------------------------------------------
319
- def merge_custom_defaults!
320
- dotfile = File.join(ENV["HOME"], ".aprc")
321
- if File.readable?(dotfile)
322
- load dotfile
323
- merge_options!(self.class.defaults)
324
- end
325
- rescue => e
326
- $stderr.puts "Could not load #{dotfile}: #{e}"
327
- end
328
-
329
- # Return true if we are to colorize the output.
330
- #------------------------------------------------------------------------------
331
- def colorize?
332
- @@force_colors || (STDOUT.tty? && ((ENV['TERM'] && ENV['TERM'] != 'dumb') || ENV['ANSICON']))
333
- end
334
-
335
- # Class accessor to force colorized output (ex. forked subprocess where TERM
336
- # might be dumb).
337
- #------------------------------------------------------------------------------
338
- def self.force_colors!(value = true)
339
- @@force_colors = value
340
- end
341
-
342
- # Class accessors for custom defaults.
343
- #------------------------------------------------------------------------------
344
- def self.defaults
345
- @@defaults ||= {}
346
- end
347
-
348
- def self.defaults=(args = {})
349
- @@defaults = args
350
- end
351
-
352
- end
@@ -1,18 +0,0 @@
1
- # Copyright (c) 2010-2011 Michael Dvorkin
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
- module AwesomePrintLogger
7
-
8
- # Add ap method to logger
9
- #------------------------------------------------------------------------------
10
- def ap(object, level = nil)
11
- level ||= AwesomePrint.defaults[:log_level] || :debug
12
- send level, object.ai
13
- end
14
-
15
- end
16
-
17
- Logger.send(:include, AwesomePrintLogger)
18
- ActiveSupport::BufferedLogger.send(:include, AwesomePrintLogger) if defined?(::ActiveSupport::BufferedLogger)
@@ -1,17 +0,0 @@
1
- # Copyright (c) 2010-2011 Michael Dvorkin
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
- module AwesomePrintActionView
7
-
8
- # Use HTML colors and add default "debug_dump" class to the resulting HTML.
9
- def ap_debug(object, options = {})
10
- object.ai(options.merge(:html => true)).sub(/^<pre([\s>])/, '<pre class="debug_dump"\\1')
11
- end
12
-
13
- alias_method :ap, :ap_debug
14
-
15
- end
16
-
17
- ActionView::Base.send(:include, AwesomePrintActionView) if defined?(ActionView)
@@ -1,54 +0,0 @@
1
- # Copyright (c) 2010-2011 Michael Dvorkin
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
- module AwesomePrintActiveRecord
7
-
8
- def self.included(base)
9
- base.send :alias_method, :printable_without_active_record, :printable
10
- base.send :alias_method, :printable, :printable_with_active_record
11
- end
12
-
13
- # Add ActiveRecord class names to the dispatcher pipeline.
14
- #------------------------------------------------------------------------------
15
- def printable_with_active_record(object)
16
- printable = printable_without_active_record(object)
17
- return printable if !defined?(ActiveRecord::Base)
18
-
19
- if printable == :self
20
- if object.is_a?(ActiveRecord::Base)
21
- printable = :active_record_instance
22
- end
23
- elsif printable == :class and object.ancestors.include?(ActiveRecord::Base)
24
- printable = :active_record_class
25
- end
26
- printable
27
- end
28
-
29
- # Format ActiveRecord instance object.
30
- #------------------------------------------------------------------------------
31
- def awesome_active_record_instance(object)
32
- return object.inspect if !defined?(ActiveSupport::OrderedHash)
33
-
34
- data = object.class.column_names.inject(ActiveSupport::OrderedHash.new) do |hash, name|
35
- hash[name.to_sym] = object.send(name) if object.has_attribute?(name) || object.new_record?
36
- hash
37
- end
38
- "#{object} " + awesome_hash(data)
39
- end
40
-
41
- # Format ActiveRecord class object.
42
- #------------------------------------------------------------------------------
43
- def awesome_active_record_class(object)
44
- return object.inspect if !defined?(ActiveSupport::OrderedHash) || !object.respond_to?(:columns)
45
-
46
- data = object.columns.inject(ActiveSupport::OrderedHash.new) do |hash, c|
47
- hash[c.name.to_sym] = c.type
48
- hash
49
- end
50
- "class #{object} < #{object.superclass} " << awesome_hash(data)
51
- end
52
- end
53
-
54
- AwesomePrint.send(:include, AwesomePrintActiveRecord)
@@ -1,46 +0,0 @@
1
- # Copyright (c) 2010-2011 Michael Dvorkin
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
- module AwesomePrintActiveSupport
7
-
8
- def self.included(base)
9
- base.send :alias_method, :printable_without_active_support, :printable
10
- base.send :alias_method, :printable, :printable_with_active_support
11
- end
12
-
13
- # Add ActiveSupport class names to the dispatcher pipeline.
14
- #------------------------------------------------------------------------------
15
- def printable_with_active_support(object)
16
- printable = printable_without_active_support(object)
17
- return printable if !defined?(ActiveSupport::TimeWithZone) || !defined?(HashWithIndifferentAccess)
18
-
19
- if printable == :self
20
- if object.is_a?(ActiveSupport::TimeWithZone)
21
- printable = :active_support_time
22
- elsif object.is_a?(HashWithIndifferentAccess)
23
- printable = :hash_with_indifferent_access
24
- end
25
- end
26
- printable
27
- end
28
-
29
- # Format ActiveSupport::TimeWithZone as standard Time.
30
- #------------------------------------------------------------------------------
31
- def awesome_active_support_time(object)
32
- awesome_self(object, :as => :time)
33
- end
34
-
35
- # Format HashWithIndifferentAccess as standard Hash.
36
- #
37
- # NOTE: can't use awesome_self(object, :as => :hash) since awesome_self uses
38
- # object.inspect internally, i.e. it would convert hash to string.
39
- #------------------------------------------------------------------------------
40
- def awesome_hash_with_indifferent_access(object)
41
- awesome_hash(object)
42
- end
43
-
44
- end
45
-
46
- AwesomePrint.send(:include, AwesomePrintActiveSupport)