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
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ .rvmrc
data/CHANGELOG CHANGED
@@ -1,3 +1,21 @@
1
+ 1.0.2
2
+ - Added formatting of Mongoid documents (Adam Doppelt)
3
+ - ActiveRecord objects display attributes only. Use :raw => true to display the entire object
4
+ - ActiveSupport::Date objects get formatted as regular Date
5
+ - Rails.logger.ap colorizes output based on ActiveSupport::LogSubscriber.colorize_logging (default is true)
6
+ - Improved formatting of methods array
7
+
8
+ 1.0.1
9
+ - Updated repo tags for Rubygems.org
10
+
11
+ 1.0.0 Thanksgiving edition
12
+ - Added ability to format *arbitrary* Ruby object
13
+ - Added :limit option to limit large output for arrays and hashes (Andrew Horsman)
14
+ - Improved HTML formatting when :html => true (Daniel Johnson)
15
+ - Added Mongoid extension (Adam Doppelt)
16
+ - Added Nokogiri extension (Adam Doppelt)
17
+ - Removed Jeweler gem dependency
18
+
1
19
  0.4.0
2
20
  - 'ap object' now returns the object (Stephan Hagemann)
3
21
  - Added :html => true option to enable HTML colors rather that ANSI (ex. Sinatra templates)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+ gemspec
3
+
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ awesome_print (1.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ fakefs (0.3.2)
11
+ rspec (2.6.0)
12
+ rspec-core (~> 2.6.0)
13
+ rspec-expectations (~> 2.6.0)
14
+ rspec-mocks (~> 2.6.0)
15
+ rspec-core (2.6.4)
16
+ rspec-expectations (2.6.0)
17
+ diff-lcs (~> 1.1.2)
18
+ rspec-mocks (2.6.0)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ awesome_print!
25
+ fakefs (>= 0.2.1)
26
+ rspec (>= 2.6.0)
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  ## Awesome Print ##
2
- Awesome Print is Ruby library that pretty prints Ruby objects in full color
2
+ Awesome Print is a Ruby library that pretty prints Ruby objects in full color
3
3
  exposing their internal structure with proper indentation. Rails ActiveRecord
4
4
  objects and usage within Rails templates are supported via included mixins.
5
5
 
@@ -7,9 +7,6 @@ objects and usage within Rails templates are supported via included mixins.
7
7
  # Installing as Ruby gem
8
8
  $ gem install awesome_print
9
9
 
10
- # Installing as Rails plugin
11
- $ ruby script/plugin install http://github.com/michaeldv/awesome_print.git
12
-
13
10
  # Cloning the repository
14
11
  $ git clone git://github.com/michaeldv/awesome_print.git
15
12
 
@@ -20,26 +17,32 @@ objects and usage within Rails templates are supported via included mixins.
20
17
 
21
18
  Default options:
22
19
 
23
- :multiline => true, # Display in multiple lines.
24
- :plain => false, # Use colors.
25
- :indent => 4, # Indent using 4 spaces.
26
- :index => true, # Display array indices.
27
- :html => false, # Use ANSI color codes rather than HTML.
28
- :sorted_hash_keys => false, # Do not sort hash keys.
20
+ :indent => 4, # Indent using 4 spaces.
21
+ :index => true, # Display array indices.
22
+ :html => false, # Use ANSI color codes rather than HTML.
23
+ :multiline => true, # Display in multiple lines.
24
+ :plain => false, # Use colors.
25
+ :sort_keys => false, # Do not sort hash keys.
26
+ :limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer.
29
27
  :color => {
28
+ :args => :pale,
30
29
  :array => :white,
31
- :bignum => :blue,
30
+ :bigdecimal => :blue,
32
31
  :class => :yellow,
33
32
  :date => :greenish,
34
33
  :falseclass => :red,
35
34
  :fixnum => :blue,
36
35
  :float => :blue,
37
- :hash => :gray,
36
+ :hash => :pale,
37
+ :keyword => :cyan,
38
+ :method => :purpleish,
38
39
  :nilclass => :red,
39
40
  :string => :yellowish,
41
+ :struct => :pale,
40
42
  :symbol => :cyanish,
41
43
  :time => :greenish,
42
- :trueclass => :green
44
+ :trueclass => :green,
45
+ :variable => :cyanish
43
46
  }
44
47
 
45
48
  Supported color names:
@@ -137,6 +140,35 @@ Supported color names:
137
140
  $ ruby 6.rb
138
141
  42
139
142
  true
143
+ $ cat 7.rb
144
+ require "awesome_print"
145
+ some_array = (1..1000).to_a
146
+ ap some_array, :limit => true
147
+ ^D
148
+ $ ruby 7.rb
149
+ [
150
+ [ 0] 1,
151
+ [ 1] 2,
152
+ [ 2] 3,
153
+ [ 3] .. [996],
154
+ [997] 998,
155
+ [998] 999,
156
+ [999] 1000
157
+ ]
158
+
159
+ $ cat 8.rb
160
+ require "awesome_print"
161
+ some_array = (1..1000).to_a
162
+ ap some_array, :limit => 5
163
+ ^D
164
+ $ ruby 8.rb
165
+ [
166
+ [ 0] 1,
167
+ [ 1] 2,
168
+ [ 2] .. [997],
169
+ [998] 999,
170
+ [999] 1000
171
+ ]
140
172
 
141
173
  ### Example (Rails console) ###
142
174
  $ rails console
@@ -197,7 +229,7 @@ Supported color names:
197
229
 
198
230
  ### IRB integration ###
199
231
  To use awesome_print as default formatter in irb and Rails console add the following
200
- lines into your ~/.irbrc file:
232
+ code to your ~/.irbrc file:
201
233
 
202
234
  require "rubygems"
203
235
  require "awesome_print"
@@ -216,6 +248,15 @@ lines into your ~/.irbrc file:
216
248
  end.new
217
249
  end
218
250
 
251
+ ### PRY integration ###
252
+ If you miss awesome_print's way of formatting output, here's how you can use it in place
253
+ of the formatting which comes with pry. Add the following code to your ~/.pryrc:
254
+
255
+ require "rubygems"
256
+ require "awesome_print"
257
+
258
+ Pry.print = proc { |output, value| output.puts value.ai }
259
+
219
260
  ### Logger Convenience Method ###
220
261
  awesome_print adds the 'ap' method to the Logger and ActiveSupport::BufferedLogger classes
221
262
  letting you call:
@@ -234,7 +275,8 @@ in the custom defaults (see below). You can also override on a per call basis wi
234
275
  awesome_print adds the 'ap' method to the ActionView::Base class making it available
235
276
  within Rails templates. For example:
236
277
 
237
- <%= ap @accounts.first %>
278
+ <%= ap @accounts.first %> # ERB
279
+ != ap @accounts.first # HAML
238
280
 
239
281
  With other web frameworks (ex: in Sinatra templates) you can explicitly request HTML
240
282
  formatting:
@@ -270,11 +312,17 @@ For example:
270
312
 
271
313
  ### Contributors ###
272
314
 
315
+ * Adam Doppelt -- https://github.com/gurgeous
273
316
  * Andrew O'Brien -- https://github.com/AndrewO
317
+ * Andrew Horsman -- https://github.com/basicxman
318
+ * Benoit Daloze -- http://github.com/eregon
319
+ * Brandon Zylstra -- https://github.com/brandondrew
320
+ * Daniel Johnson -- https://github.com/adhd360
274
321
  * Daniel Bretoi -- http://github.com/danielb2
275
322
  * Eloy Duran -- http://github.com/alloy
276
323
  * Elpizo Choi -- https://github.com/fuJiin
277
- * Benoit Daloze -- http://github.com/eregon
324
+ * Greg Weber -- https://github.com/gregwebs
325
+ * Jeff Felchner -- https://github.com/jfelchner
278
326
  * Sean Gallagher -- http://github.com/torandu
279
327
  * Stephan Hagemann -- https://github.com/shageman
280
328
  * Tim Harper -- http://github.com/timcharper
data/Rakefile CHANGED
@@ -1,46 +1,2 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "awesome_print"
8
- gem.rubyforge_project = "awesome_print"
9
- gem.summary = %Q{Pretty print Ruby objects with proper indentation and colors.}
10
- gem.description = %Q{Great Ruby dubugging companion: pretty print Ruby objects to visualize their structure. Supports Rails ActiveRecord objects via included mixin.}
11
- gem.email = "mike@dvorkin.net"
12
- gem.homepage = "http://github.com/michaeldv/awesome_print"
13
- gem.authors = ["Michael Dvorkin"]
14
- gem.add_development_dependency "rspec", ">= 2.5.0"
15
- gem.files = FileList["[A-Z]*", "lib/**/*.rb", "rails/*.rb", "spec/*", "init.rb"]
16
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
- end
18
- Jeweler::GemcutterTasks.new
19
- rescue LoadError
20
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
- end
22
-
23
- require "rspec/core/rake_task"
24
- RSpec::Core::RakeTask.new(:spec) do |spec|
25
- spec.pattern = 'spec/**/*_spec.rb'
26
- spec.rspec_opts = ['--color']
27
- end
28
-
29
- RSpec::Core::RakeTask.new(:rcov) do |spec|
30
- spec.rcov = true
31
- spec.rcov_opts = %q[--exclude "spec"]
32
- end
33
-
34
- task :spec => :check_dependencies
35
-
36
- task :default => :spec
37
-
38
- require 'rake/rdoctask'
39
- Rake::RDocTask.new do |rdoc|
40
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
-
42
- rdoc.rdoc_dir = 'rdoc'
43
- rdoc.title = "ap #{version}"
44
- rdoc.rdoc_files.include('README*')
45
- rdoc.rdoc_files.include('lib/**/*.rb')
46
- end
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/lib/ap.rb CHANGED
@@ -4,21 +4,7 @@
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
5
  #------------------------------------------------------------------------------
6
6
  #
7
- # AwesomePrint might be loaded implicitly through ~/.irbrc so do nothing
8
- # for subsequent requires.
7
+ # Keeping this for backwards compatibility to allow
8
+ # require "ap"
9
9
  #
10
- unless defined?(AwesomePrint)
11
- %w(array string method object class kernel).each do |file|
12
- require File.dirname(__FILE__) + "/ap/core_ext/#{file}"
13
- end
14
-
15
- require File.dirname(__FILE__) + "/ap/awesome_print"
16
- require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(Logger)
17
- require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(ActionView)
18
-
19
- # Load the following under normal circumstatnces as well as in Rails
20
- # console when required from ~/.irbrc.
21
- require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV'])
22
- require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV'])
23
- require File.dirname(__FILE__) + "/ap/mixin/mongo_mapper" if defined?(MongoMapper)
24
- end
10
+ require File.dirname(__FILE__) + "/awesome_print"
@@ -4,14 +4,19 @@
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
5
  #------------------------------------------------------------------------------
6
6
  class Class #:nodoc:
7
+ #
8
+ # Intercept methods below to inject @__awesome_print__ instance variable
9
+ # so we know it is the *methods* array when formatting an array.
10
+ #
7
11
  # Remaining public/private etc. '_methods' are handled in core_ext/object.rb.
12
+ #
8
13
  %w(instance_methods private_instance_methods protected_instance_methods public_instance_methods).each do |name|
9
14
  original_method = instance_method(name)
10
15
 
11
16
  define_method name do |*args|
12
17
  methods = original_method.bind(self).call(*args)
13
- methods.instance_variable_set('@__awesome_methods__', self) # Evil?!
14
- methods.sort!
18
+ methods.instance_variable_set('@__awesome_methods__', self)
19
+ methods
15
20
  end
16
21
  end
17
22
  end
@@ -6,8 +6,8 @@
6
6
  module Kernel
7
7
 
8
8
  def ai(options = {})
9
- ap = AwesomePrint.new(options)
10
- ap.send(:awesome, self)
9
+ ap = AwesomePrint::Inspector.new(options)
10
+ ap.awesome self
11
11
  end
12
12
  alias :awesome_inspect :ai
13
13
 
@@ -0,0 +1,20 @@
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 AwesomePrint
7
+ module Logger
8
+
9
+ # Add ap method to logger
10
+ #------------------------------------------------------------------------------
11
+ def ap(object, level = nil)
12
+ level ||= AwesomePrint.defaults[:log_level] if AwesomePrint.defaults
13
+ level ||= :debug
14
+ send level, object.ai
15
+ end
16
+ end
17
+ end
18
+
19
+ Logger.send(:include, AwesomePrint::Logger)
20
+ ActiveSupport::BufferedLogger.send(:include, AwesomePrint::Logger) if defined?(ActiveSupport::BufferedLogger)
@@ -4,14 +4,19 @@
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
5
  #------------------------------------------------------------------------------
6
6
  class Object #:nodoc:
7
+ #
8
+ # Intercept methods below to inject @__awesome_print__ instance variable
9
+ # so we know it is the *methods* array when formatting an array.
10
+ #
7
11
  # Remaining instance '_methods' are handled in core_ext/class.rb.
12
+ #
8
13
  %w(methods private_methods protected_methods public_methods singleton_methods).each do |name|
9
14
  original_method = instance_method(name)
10
15
 
11
16
  define_method name do |*args|
12
17
  methods = original_method.bind(self).call(*args)
13
- methods.instance_variable_set('@__awesome_methods__', self) # Evil?!
14
- methods.sort!
18
+ methods.instance_variable_set('@__awesome_methods__', self)
19
+ methods
15
20
  end
16
21
  end
17
22
  end
@@ -4,21 +4,24 @@
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
5
  #------------------------------------------------------------------------------
6
6
  class String
7
-
7
+ #
8
8
  # ANSI color codes:
9
- # \033 => escape
9
+ # \e => escape
10
10
  # 30 => color base
11
11
  # 1 => bright
12
12
  # 0 => normal
13
-
13
+ #
14
+ # For HTML coloring we use <kbd> tag instead of <span> to require monospace
15
+ # font. Note that beloved <tt> has been removed from HTML5.
16
+ #
14
17
  %w(gray red green yellow blue purple cyan white).zip(
15
18
  %w(black darkred darkgreen brown navy darkmagenta darkcyan slategray)).each_with_index do |(color, shade), i|
16
19
  define_method color do |*html|
17
- html[0] ? %Q|<pre style="color:#{color}">#{self}</pre>| : "\033[1;#{30+i}m#{self}\033[0m"
20
+ html[0] ? %Q|<kbd style="color:#{color}">#{self}</kbd>| : "\e[1;#{30+i}m#{self}\e[0m"
18
21
  end
19
22
 
20
23
  define_method "#{color}ish" do |*html|
21
- html[0] ? %Q|<pre style="color:#{shade}">#{self}</pre>| : "\033[0;#{30+i}m#{self}\033[0m"
24
+ html[0] ? %Q|<kbd style="color:#{shade}">#{self}</kbd>| : "\e[0;#{30+i}m#{self}\e[0m"
22
25
  end
23
26
  end
24
27
 
@@ -0,0 +1,18 @@
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 AwesomePrint
7
+ module ActionView
8
+
9
+ # Use HTML colors and add default "debug_dump" class to the resulting HTML.
10
+ def ap_debug(object, options = {})
11
+ object.ai(options.merge(:html => true)).sub(/^<pre([\s>])/, '<pre class="debug_dump"\\1')
12
+ end
13
+
14
+ alias_method :ap, :ap_debug
15
+ end
16
+ end
17
+
18
+ ActionView::Base.send(:include, AwesomePrint::ActionView)
@@ -0,0 +1,64 @@
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 AwesomePrint
7
+ module ActiveRecord
8
+
9
+ def self.included(base)
10
+ base.send :alias_method, :cast_without_active_record, :cast
11
+ base.send :alias_method, :cast, :cast_with_active_record
12
+ end
13
+
14
+ # Add ActiveRecord class names to the dispatcher pipeline.
15
+ #------------------------------------------------------------------------------
16
+ def cast_with_active_record(object, type)
17
+ cast = cast_without_active_record(object, type)
18
+ return cast if !defined?(::ActiveRecord)
19
+
20
+ if object.is_a?(::ActiveRecord::Base)
21
+ cast = :active_record_instance
22
+ elsif object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base)
23
+ cast = :active_record_class
24
+ end
25
+ cast
26
+ end
27
+
28
+ private
29
+
30
+ # Format ActiveRecord instance object.
31
+ #
32
+ # NOTE: by default only instance attributes (i.e. columns) are shown. To format
33
+ # ActiveRecord instance as regular object showing its instance variables and
34
+ # accessors use :raw => true option:
35
+ #
36
+ # ap record, :raw => true
37
+ #
38
+ #------------------------------------------------------------------------------
39
+ def awesome_active_record_instance(object)
40
+ return object.inspect if !defined?(::ActiveSupport::OrderedHash)
41
+ return awesome_object(object) if @options[:raw]
42
+
43
+ data = object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
44
+ hash[name.to_sym] = object.send(name) if object.has_attribute?(name) || object.new_record?
45
+ hash
46
+ end
47
+ "#{object} " << awesome_hash(data)
48
+ end
49
+
50
+ # Format ActiveRecord class object.
51
+ #------------------------------------------------------------------------------
52
+ def awesome_active_record_class(object)
53
+ return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:columns) || object.to_s == "ActiveRecord::Base"
54
+
55
+ data = object.columns.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
56
+ hash[c.name.to_sym] = c.type
57
+ hash
58
+ end
59
+ "class #{object} < #{object.superclass} " << awesome_hash(data)
60
+ end
61
+ end
62
+ end
63
+
64
+ AwesomePrint::Formatter.send(:include, AwesomePrint::ActiveRecord)
@@ -0,0 +1,47 @@
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 AwesomePrint
7
+ module ActiveSupport
8
+
9
+ def self.included(base)
10
+ base.send :alias_method, :cast_without_active_support, :cast
11
+ base.send :alias_method, :cast, :cast_with_active_support
12
+ end
13
+
14
+ def cast_with_active_support(object, type)
15
+ cast = cast_without_active_support(object, type)
16
+ if defined?(::ActiveSupport) && defined?(::HashWithIndifferentAccess)
17
+ if object.is_a?(::ActiveSupport::TimeWithZone) || object.is_a?(::Date)
18
+ cast = :active_support_time
19
+ elsif object.is_a?(::HashWithIndifferentAccess)
20
+ cast = :hash_with_indifferent_access
21
+ end
22
+ end
23
+ cast
24
+ end
25
+
26
+ # Format ActiveSupport::TimeWithZone as standard Time.
27
+ #------------------------------------------------------------------------------
28
+ def awesome_active_support_time(object)
29
+ colorize(object.inspect, :time)
30
+ end
31
+
32
+ # Format HashWithIndifferentAccess as standard Hash.
33
+ #------------------------------------------------------------------------------
34
+ def awesome_hash_with_indifferent_access(object)
35
+ awesome_hash(object)
36
+ end
37
+ end
38
+ end
39
+
40
+ AwesomePrint::Formatter.send(:include, AwesomePrint::ActiveSupport)
41
+ #
42
+ # Colorize Rails logs.
43
+ #
44
+ if defined?(ActiveSupport::LogSubscriber)
45
+ AwesomePrint.force_colors! ActiveSupport::LogSubscriber.colorize_logging
46
+ end
47
+
@@ -0,0 +1,38 @@
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 AwesomePrint
7
+ module MongoMapper
8
+
9
+ def self.included(base)
10
+ base.send :alias_method, :cast_without_mongo_mapper, :cast
11
+ base.send :alias_method, :cast, :cast_with_mongo_mapper
12
+ end
13
+
14
+ # Add MongoMapper class names to the dispatcher pipeline.
15
+ #------------------------------------------------------------------------------
16
+ def cast_with_mongo_mapper(object, type)
17
+ cast = cast_without_mongo_mapper(object, type)
18
+ if defined?(::MongoMapper::Document) && object.is_a?(Class) && (object.ancestors & [ ::MongoMapper::Document, ::MongoMapper::EmbeddedDocument ]).size > 0
19
+ cast = :mongo_mapper_class
20
+ end
21
+ cast
22
+ end
23
+
24
+ # Format MongoMapper class object.
25
+ #------------------------------------------------------------------------------
26
+ def awesome_mongo_mapper_class(object)
27
+ return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:keys)
28
+
29
+ data = object.keys.sort.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
30
+ hash[c.first] = (c.last.type || "undefined").to_s.underscore.intern
31
+ hash
32
+ end
33
+ "class #{object} < #{object.superclass} " << awesome_hash(data)
34
+ end
35
+ end
36
+ end
37
+
38
+ AwesomePrint::Formatter.send(:include, AwesomePrint::MongoMapper)
@@ -0,0 +1,65 @@
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 AwesomePrint
7
+ module Mongoid
8
+
9
+ def self.included(base)
10
+ base.send :alias_method, :cast_without_mongoid, :cast
11
+ base.send :alias_method, :cast, :cast_with_mongoid
12
+ end
13
+
14
+ # Add Mongoid class names to the dispatcher pipeline.
15
+ #------------------------------------------------------------------------------
16
+ def cast_with_mongoid(object, type)
17
+ cast = cast_without_mongoid(object, type)
18
+ if defined?(::Mongoid::Document)
19
+ if object.is_a?(Class) && object.ancestors.include?(::Mongoid::Document)
20
+ cast = :mongoid_class
21
+ elsif object.class.ancestors.include?(::Mongoid::Document)
22
+ cast = :mongoid_document
23
+ elsif object.is_a?(::BSON::ObjectId)
24
+ cast = :mongoid_bson_id
25
+ end
26
+ end
27
+ cast
28
+ end
29
+
30
+ # Format Mongoid class object.
31
+ #------------------------------------------------------------------------------
32
+ def awesome_mongoid_class(object)
33
+ return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields)
34
+
35
+ data = object.fields.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
36
+ hash[c[1].name.to_sym] = (c[1].type || "undefined").to_s.underscore.intern
37
+ hash
38
+ end
39
+ "class #{object} < #{object.superclass} " << awesome_hash(data)
40
+ end
41
+
42
+ # Format Mongoid Document object.
43
+ #------------------------------------------------------------------------------
44
+ def awesome_mongoid_document(object)
45
+ return object.inspect if !defined?(::ActiveSupport::OrderedHash)
46
+
47
+ data = object.attributes.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
48
+ hash[c[0].to_sym] = c[1]
49
+ hash
50
+ end
51
+ if !object.errors.empty?
52
+ data = {:errors => object.errors, :attributes => data}
53
+ end
54
+ "#{object} #{awesome_hash(data)}"
55
+ end
56
+
57
+ # Format BSON::ObjectId
58
+ #------------------------------------------------------------------------------
59
+ def awesome_mongoid_bson_id(object)
60
+ object.inspect
61
+ end
62
+ end
63
+ end
64
+
65
+ AwesomePrint::Formatter.send(:include, AwesomePrint::Mongoid)