awesome_print 1.0.0 → 1.1.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.
- data/.gitignore +1 -0
- data/CHANGELOG +21 -0
- data/Gemfile.lock +1 -1
- data/LICENSE +2 -2
- data/README.md +22 -19
- data/Rakefile +10 -1
- data/lib/ap.rb +1 -1
- data/lib/awesome_print/core_ext/array.rb +12 -2
- data/lib/awesome_print/core_ext/class.rb +8 -3
- data/lib/awesome_print/core_ext/kernel.rb +2 -2
- data/lib/awesome_print/core_ext/logger.rb +2 -2
- data/lib/awesome_print/core_ext/method.rb +1 -1
- data/lib/awesome_print/core_ext/object.rb +8 -3
- data/lib/awesome_print/core_ext/string.rb +1 -1
- data/lib/awesome_print/ext/action_view.rb +2 -2
- data/lib/awesome_print/ext/active_record.rb +28 -2
- data/lib/awesome_print/ext/active_support.rb +9 -2
- data/lib/awesome_print/ext/mongo_mapper.rb +86 -3
- data/lib/awesome_print/ext/mongoid.rb +32 -6
- data/lib/awesome_print/ext/nokogiri.rb +1 -1
- data/lib/awesome_print/formatter.rb +74 -25
- data/lib/awesome_print/inspector.rb +36 -5
- data/lib/awesome_print/version.rb +2 -2
- data/lib/awesome_print.rb +12 -10
- data/spec/formats_spec.rb +18 -83
- data/spec/methods_spec.rb +20 -9
- data/spec/misc_spec.rb +213 -0
- data/spec/objects_spec.rb +11 -5
- data/spec/spec_helper.rb +15 -2
- metadata +26 -3
data/.gitignore
CHANGED
data/CHANGELOG
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
1.1.0
|
|
2
|
+
- Objects are no longer recursively formatted by default. Reenable by using :raw => true option.
|
|
3
|
+
- ap(object) now returns nil when running under IRB or Pry
|
|
4
|
+
- Added support for Mongoid 3 and Moped (Nikolaj Nikolajsen)
|
|
5
|
+
- Improved formatting of MongoMapper objects (George .)
|
|
6
|
+
- ActiveRecord::Relation now renders as array (Dan Lynn)
|
|
7
|
+
- Formatting BigDecimal no longer looses precision (Evan Senter)
|
|
8
|
+
- Added AwesomePrint.irb! and AwesomePrint.pry! convenience methods
|
|
9
|
+
- Fixed conflict with the colorize gem
|
|
10
|
+
- Misc tweaks and bug fixes
|
|
11
|
+
|
|
12
|
+
1.0.2
|
|
13
|
+
- Added formatting of Mongoid documents (Adam Doppelt)
|
|
14
|
+
- ActiveRecord objects display attributes only. Use :raw => true to display the entire object
|
|
15
|
+
- ActiveSupport::Date objects get formatted as regular Date
|
|
16
|
+
- Rails.logger.ap colorizes output based on ActiveSupport::LogSubscriber.colorize_logging (default is true)
|
|
17
|
+
- Improved formatting of methods array
|
|
18
|
+
|
|
19
|
+
1.0.1
|
|
20
|
+
- Updated repo tags for Rubygems.org
|
|
21
|
+
|
|
1
22
|
1.0.0 Thanksgiving edition
|
|
2
23
|
- Added ability to format *arbitrary* Ruby object
|
|
3
24
|
- Added :limit option to limit large output for arrays and hashes (Andrew Horsman)
|
data/Gemfile.lock
CHANGED
data/LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -22,6 +22,7 @@ Default options:
|
|
|
22
22
|
:html => false, # Use ANSI color codes rather than HTML.
|
|
23
23
|
:multiline => true, # Display in multiple lines.
|
|
24
24
|
:plain => false, # Use colors.
|
|
25
|
+
:raw => false, # Do not recursively format object instance variables.
|
|
25
26
|
:sort_keys => false, # Do not sort hash keys.
|
|
26
27
|
:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer.
|
|
27
28
|
:color => {
|
|
@@ -37,6 +38,7 @@ Default options:
|
|
|
37
38
|
:keyword => :cyan,
|
|
38
39
|
:method => :purpleish,
|
|
39
40
|
:nilclass => :red,
|
|
41
|
+
:rational => :blue,
|
|
40
42
|
:string => :yellowish,
|
|
41
43
|
:struct => :pale,
|
|
42
44
|
:symbol => :cyanish,
|
|
@@ -229,24 +231,17 @@ Supported color names:
|
|
|
229
231
|
|
|
230
232
|
### IRB integration ###
|
|
231
233
|
To use awesome_print as default formatter in irb and Rails console add the following
|
|
232
|
-
|
|
234
|
+
code to your ~/.irbrc file:
|
|
233
235
|
|
|
234
|
-
require "rubygems"
|
|
235
236
|
require "awesome_print"
|
|
237
|
+
AwesomePrint.irb!
|
|
236
238
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
else # MacRuby
|
|
244
|
-
IRB.formatter = Class.new(IRB::Formatter) do
|
|
245
|
-
def inspect_object(object)
|
|
246
|
-
object.ai
|
|
247
|
-
end
|
|
248
|
-
end.new
|
|
249
|
-
end
|
|
239
|
+
### PRY integration ###
|
|
240
|
+
If you miss awesome_print's way of formatting output, here's how you can use it in place
|
|
241
|
+
of the formatting which comes with pry. Add the following code to your ~/.pryrc:
|
|
242
|
+
|
|
243
|
+
require "awesome_print"
|
|
244
|
+
AwesomePrint.pry!
|
|
250
245
|
|
|
251
246
|
### Logger Convenience Method ###
|
|
252
247
|
awesome_print adds the 'ap' method to the Logger and ActiveSupport::BufferedLogger classes
|
|
@@ -266,7 +261,8 @@ in the custom defaults (see below). You can also override on a per call basis wi
|
|
|
266
261
|
awesome_print adds the 'ap' method to the ActionView::Base class making it available
|
|
267
262
|
within Rails templates. For example:
|
|
268
263
|
|
|
269
|
-
<%= ap @accounts.first %>
|
|
264
|
+
<%= ap @accounts.first %> # ERB
|
|
265
|
+
!= ap @accounts.first # HAML
|
|
270
266
|
|
|
271
267
|
With other web frameworks (ex: in Sinatra templates) you can explicitly request HTML
|
|
272
268
|
formatting:
|
|
@@ -298,31 +294,38 @@ For example:
|
|
|
298
294
|
* Make your feature addition or bug fix.
|
|
299
295
|
* Add specs for it, making sure $ rake spec is all green.
|
|
300
296
|
* Commit, do not mess with rakefile, version, or history.
|
|
301
|
-
* Send me
|
|
297
|
+
* Send me commit URL (*do not* send me pull requests).
|
|
302
298
|
|
|
303
299
|
### Contributors ###
|
|
304
300
|
|
|
305
301
|
* Adam Doppelt -- https://github.com/gurgeous
|
|
306
302
|
* Andrew O'Brien -- https://github.com/AndrewO
|
|
307
303
|
* Andrew Horsman -- https://github.com/basicxman
|
|
304
|
+
* Barry Allard -- https://github.com/steakknife
|
|
308
305
|
* Benoit Daloze -- http://github.com/eregon
|
|
309
306
|
* Brandon Zylstra -- https://github.com/brandondrew
|
|
307
|
+
* Dan Lynn -- https://github.com/danlynn
|
|
310
308
|
* Daniel Johnson -- https://github.com/adhd360
|
|
311
309
|
* Daniel Bretoi -- http://github.com/danielb2
|
|
312
310
|
* Eloy Duran -- http://github.com/alloy
|
|
313
311
|
* Elpizo Choi -- https://github.com/fuJiin
|
|
312
|
+
* Evan Senter -- https://github.com/evansenter
|
|
313
|
+
* George . -- https://github.com/gardelea
|
|
314
314
|
* Greg Weber -- https://github.com/gregwebs
|
|
315
315
|
* Jeff Felchner -- https://github.com/jfelchner
|
|
316
|
+
* Nikolaj Nikolajsen -- https://github.com/nikolajsen
|
|
317
|
+
* Ryan Schlesinger -- https://github.com/ryansch
|
|
316
318
|
* Sean Gallagher -- http://github.com/torandu
|
|
317
319
|
* Stephan Hagemann -- https://github.com/shageman
|
|
318
320
|
* Tim Harper -- http://github.com/timcharper
|
|
319
321
|
* Tobias Crawley -- http://github.com/tobias
|
|
322
|
+
* Thibaut Barrère -- https://github.com/thbar
|
|
320
323
|
* Viktar Basharymau -- https://github.com/DNNX
|
|
321
324
|
|
|
322
325
|
### License ###
|
|
323
|
-
Copyright (c) 2010-
|
|
326
|
+
Copyright (c) 2010-2012 Michael Dvorkin
|
|
324
327
|
|
|
325
|
-
|
|
328
|
+
http://www.dvorkin.net
|
|
326
329
|
|
|
327
330
|
%w(mike dvorkin.net) * "@" || %w(mike fatfreecrm.com) * "@"
|
|
328
331
|
|
data/Rakefile
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
|
-
require
|
|
1
|
+
require "bundler"
|
|
2
2
|
Bundler::GemHelper.install_tasks
|
|
3
|
+
|
|
4
|
+
task :default => :spec
|
|
5
|
+
|
|
6
|
+
desc "Run all awesome_print gem specs"
|
|
7
|
+
task :spec do
|
|
8
|
+
# Run plain rspec command without RSpec::Core::RakeTask overrides.
|
|
9
|
+
exec "rspec -c spec"
|
|
10
|
+
end
|
|
11
|
+
|
data/lib/ap.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010-
|
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
|
2
2
|
#
|
|
3
3
|
# Awesome Print is freely distributable under the terms of MIT license.
|
|
4
4
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
|
@@ -58,7 +58,17 @@ class Array #:nodoc:
|
|
|
58
58
|
original_grep(pattern)
|
|
59
59
|
else
|
|
60
60
|
original_grep(pattern) do |match|
|
|
61
|
-
|
|
61
|
+
#
|
|
62
|
+
# The binding can only be used with Ruby-defined methods, therefore
|
|
63
|
+
# we must rescue potential "ArgumentError: Can't create Binding from
|
|
64
|
+
# C level Proc" error.
|
|
65
|
+
#
|
|
66
|
+
# For example, the following raises ArgumentError since #succ method
|
|
67
|
+
# is defined in C.
|
|
68
|
+
#
|
|
69
|
+
# [ 0, 1, 2, 3, 4 ].grep(1..2, &:succ)
|
|
70
|
+
#
|
|
71
|
+
eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding) rescue ArgumentError
|
|
62
72
|
yield match
|
|
63
73
|
end
|
|
64
74
|
end
|
|
@@ -1,17 +1,22 @@
|
|
|
1
|
-
# Copyright (c) 2010-
|
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
|
2
2
|
#
|
|
3
3
|
# Awesome Print is freely distributable under the terms of MIT license.
|
|
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)
|
|
14
|
-
methods
|
|
18
|
+
methods.instance_variable_set('@__awesome_methods__', self)
|
|
19
|
+
methods
|
|
15
20
|
end
|
|
16
21
|
end
|
|
17
22
|
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010-
|
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
|
2
2
|
#
|
|
3
3
|
# Awesome Print is freely distributable under the terms of MIT license.
|
|
4
4
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
|
@@ -13,7 +13,7 @@ module Kernel
|
|
|
13
13
|
|
|
14
14
|
def ap(object, options = {})
|
|
15
15
|
puts object.ai(options)
|
|
16
|
-
object
|
|
16
|
+
object unless AwesomePrint.console?
|
|
17
17
|
end
|
|
18
18
|
alias :awesome_print :ap
|
|
19
19
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010-
|
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
|
2
2
|
#
|
|
3
3
|
# Awesome Print is freely distributable under the terms of MIT license.
|
|
4
4
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
|
@@ -17,4 +17,4 @@ module AwesomePrint
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
Logger.send(:include, AwesomePrint::Logger)
|
|
20
|
-
ActiveSupport::BufferedLogger.send(:include, AwesomePrint::Logger) if defined?(
|
|
20
|
+
ActiveSupport::BufferedLogger.send(:include, AwesomePrint::Logger) if defined?(ActiveSupport::BufferedLogger)
|
|
@@ -1,17 +1,22 @@
|
|
|
1
|
-
# Copyright (c) 2010-
|
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
|
2
2
|
#
|
|
3
3
|
# Awesome Print is freely distributable under the terms of MIT license.
|
|
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)
|
|
14
|
-
methods
|
|
18
|
+
methods.instance_variable_set('@__awesome_methods__', self)
|
|
19
|
+
methods
|
|
15
20
|
end
|
|
16
21
|
end
|
|
17
22
|
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010-
|
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
|
2
2
|
#
|
|
3
3
|
# Awesome Print is freely distributable under the terms of MIT license.
|
|
4
4
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
|
@@ -15,4 +15,4 @@ module AwesomePrint
|
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
ActionView::Base.send(:include, AwesomePrint::ActionView)
|
|
18
|
+
ActionView::Base.send(:include, AwesomePrint::ActionView)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010-
|
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
|
2
2
|
#
|
|
3
3
|
# Awesome Print is freely distributable under the terms of MIT license.
|
|
4
4
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
|
@@ -15,14 +15,40 @@ module AwesomePrint
|
|
|
15
15
|
#------------------------------------------------------------------------------
|
|
16
16
|
def cast_with_active_record(object, type)
|
|
17
17
|
cast = cast_without_active_record(object, type)
|
|
18
|
-
if defined?(::ActiveRecord)
|
|
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)
|
|
19
23
|
cast = :active_record_class
|
|
24
|
+
elsif type == :activerecord_relation
|
|
25
|
+
cast = :array
|
|
20
26
|
end
|
|
21
27
|
cast
|
|
22
28
|
end
|
|
23
29
|
|
|
24
30
|
private
|
|
25
31
|
|
|
32
|
+
# Format ActiveRecord instance object.
|
|
33
|
+
#
|
|
34
|
+
# NOTE: by default only instance attributes (i.e. columns) are shown. To format
|
|
35
|
+
# ActiveRecord instance as regular object showing its instance variables and
|
|
36
|
+
# accessors use :raw => true option:
|
|
37
|
+
#
|
|
38
|
+
# ap record, :raw => true
|
|
39
|
+
#
|
|
40
|
+
#------------------------------------------------------------------------------
|
|
41
|
+
def awesome_active_record_instance(object)
|
|
42
|
+
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
|
|
43
|
+
return awesome_object(object) if @options[:raw]
|
|
44
|
+
|
|
45
|
+
data = object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
|
|
46
|
+
hash[name.to_sym] = object.send(name) if object.has_attribute?(name) || object.new_record?
|
|
47
|
+
hash
|
|
48
|
+
end
|
|
49
|
+
"#{object} " << awesome_hash(data)
|
|
50
|
+
end
|
|
51
|
+
|
|
26
52
|
# Format ActiveRecord class object.
|
|
27
53
|
#------------------------------------------------------------------------------
|
|
28
54
|
def awesome_active_record_class(object)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010-
|
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
|
2
2
|
#
|
|
3
3
|
# Awesome Print is freely distributable under the terms of MIT license.
|
|
4
4
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
|
@@ -14,7 +14,7 @@ module AwesomePrint
|
|
|
14
14
|
def cast_with_active_support(object, type)
|
|
15
15
|
cast = cast_without_active_support(object, type)
|
|
16
16
|
if defined?(::ActiveSupport) && defined?(::HashWithIndifferentAccess)
|
|
17
|
-
if object.is_a?(::ActiveSupport::TimeWithZone)
|
|
17
|
+
if (defined?(::ActiveSupport::TimeWithZone) && object.is_a?(::ActiveSupport::TimeWithZone)) || object.is_a?(::Date)
|
|
18
18
|
cast = :active_support_time
|
|
19
19
|
elsif object.is_a?(::HashWithIndifferentAccess)
|
|
20
20
|
cast = :hash_with_indifferent_access
|
|
@@ -38,3 +38,10 @@ module AwesomePrint
|
|
|
38
38
|
end
|
|
39
39
|
|
|
40
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
|
+
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010-
|
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
|
2
2
|
#
|
|
3
3
|
# Awesome Print is freely distributable under the terms of MIT license.
|
|
4
4
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
|
@@ -14,10 +14,21 @@ module AwesomePrint
|
|
|
14
14
|
# Add MongoMapper class names to the dispatcher pipeline.
|
|
15
15
|
#------------------------------------------------------------------------------
|
|
16
16
|
def cast_with_mongo_mapper(object, type)
|
|
17
|
+
apply_default_mongo_mapper_options
|
|
17
18
|
cast = cast_without_mongo_mapper(object, type)
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
|
|
20
|
+
if defined?(::MongoMapper::Document)
|
|
21
|
+
if object.is_a?(Class) && (object.ancestors & [ ::MongoMapper::Document, ::MongoMapper::EmbeddedDocument ]).size > 0
|
|
22
|
+
cast = :mongo_mapper_class
|
|
23
|
+
elsif object.is_a?(::MongoMapper::Document) || object.is_a?(::MongoMapper::EmbeddedDocument)
|
|
24
|
+
cast = :mongo_mapper_instance
|
|
25
|
+
elsif object.is_a?(::MongoMapper::Plugins::Associations::Base)
|
|
26
|
+
cast = :mongo_mapper_association
|
|
27
|
+
elsif object.is_a?(::BSON::ObjectId)
|
|
28
|
+
cast = :mongo_mapper_bson_id
|
|
29
|
+
end
|
|
20
30
|
end
|
|
31
|
+
|
|
21
32
|
cast
|
|
22
33
|
end
|
|
23
34
|
|
|
@@ -30,8 +41,80 @@ module AwesomePrint
|
|
|
30
41
|
hash[c.first] = (c.last.type || "undefined").to_s.underscore.intern
|
|
31
42
|
hash
|
|
32
43
|
end
|
|
44
|
+
|
|
45
|
+
# Add in associations
|
|
46
|
+
if @options[:mongo_mapper][:show_associations]
|
|
47
|
+
object.associations.each do |name, assoc|
|
|
48
|
+
data[name.to_s] = assoc
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
33
52
|
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
|
34
53
|
end
|
|
54
|
+
|
|
55
|
+
# Format MongoMapper instance object.
|
|
56
|
+
#
|
|
57
|
+
# NOTE: by default only instance attributes (i.e. keys) are shown. To format
|
|
58
|
+
# MongoMapper instance as regular object showing its instance variables and
|
|
59
|
+
# accessors use :raw => true option:
|
|
60
|
+
#
|
|
61
|
+
# ap record, :raw => true
|
|
62
|
+
#
|
|
63
|
+
#------------------------------------------------------------------------------
|
|
64
|
+
def awesome_mongo_mapper_instance(object)
|
|
65
|
+
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
|
|
66
|
+
return awesome_object(object) if @options[:raw]
|
|
67
|
+
|
|
68
|
+
data = object.keys.keys.sort_by{|k| k}.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
|
|
69
|
+
hash[name] = object[name]
|
|
70
|
+
hash
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Add in associations
|
|
74
|
+
if @options[:mongo_mapper][:show_associations]
|
|
75
|
+
object.associations.each do |name, assoc|
|
|
76
|
+
if @options[:mongo_mapper][:inline_embedded] and assoc.embeddable?
|
|
77
|
+
data[name.to_s] = object.send(name)
|
|
78
|
+
else
|
|
79
|
+
data[name.to_s] = assoc
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
label = object.to_s
|
|
85
|
+
label = "#{colorize('embedded', :assoc)} #{label}" if object.is_a?(::MongoMapper::EmbeddedDocument)
|
|
86
|
+
|
|
87
|
+
"#{label} " << awesome_hash(data)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Format MongoMapper association object.
|
|
91
|
+
#------------------------------------------------------------------------------
|
|
92
|
+
def awesome_mongo_mapper_association(object)
|
|
93
|
+
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
|
|
94
|
+
return awesome_object(object) if @options[:raw]
|
|
95
|
+
|
|
96
|
+
association = object.class.name.split('::').last.titleize.downcase.sub(/ association$/,'')
|
|
97
|
+
association = "embeds #{association}" if object.embeddable?
|
|
98
|
+
class_name = object.class_name
|
|
99
|
+
|
|
100
|
+
"#{colorize(association, :assoc)} #{colorize(class_name, :class)}"
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Format BSON::ObjectId
|
|
104
|
+
#------------------------------------------------------------------------------
|
|
105
|
+
def awesome_mongo_mapper_bson_id(object)
|
|
106
|
+
object.inspect
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
private
|
|
110
|
+
|
|
111
|
+
def apply_default_mongo_mapper_options
|
|
112
|
+
@options[:color][:assoc] ||= :greenish
|
|
113
|
+
@options[:mongo_mapper] ||= {
|
|
114
|
+
:show_associations => false, # Display association data for MongoMapper documents and classes.
|
|
115
|
+
:inline_embedded => false # Display embedded associations inline with MongoMapper documents.
|
|
116
|
+
}
|
|
117
|
+
end
|
|
35
118
|
end
|
|
36
119
|
end
|
|
37
120
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010-
|
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
|
2
2
|
#
|
|
3
3
|
# Awesome Print is freely distributable under the terms of MIT license.
|
|
4
4
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
|
@@ -15,8 +15,14 @@ module AwesomePrint
|
|
|
15
15
|
#------------------------------------------------------------------------------
|
|
16
16
|
def cast_with_mongoid(object, type)
|
|
17
17
|
cast = cast_without_mongoid(object, type)
|
|
18
|
-
if defined?(::Mongoid::Document)
|
|
19
|
-
|
|
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 (defined?(::BSON) && object.is_a?(::BSON::ObjectId)) || (defined?(::Moped) && object.is_a?(::Moped::BSON::ObjectId))
|
|
24
|
+
cast = :mongoid_bson_id
|
|
25
|
+
end
|
|
20
26
|
end
|
|
21
27
|
cast
|
|
22
28
|
end
|
|
@@ -26,13 +32,33 @@ module AwesomePrint
|
|
|
26
32
|
def awesome_mongoid_class(object)
|
|
27
33
|
return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields)
|
|
28
34
|
|
|
29
|
-
data = object.fields.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
|
|
30
|
-
hash[c[1].name] = (c[1].type || "undefined").to_s.underscore.intern
|
|
31
|
-
# hash[c[1].name] = (c[1].type || "undefined").to_s.underscore.intern rescue c[1].type
|
|
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
|
|
32
37
|
hash
|
|
33
38
|
end
|
|
34
39
|
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
|
35
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
|
|
36
62
|
end
|
|
37
63
|
end
|
|
38
64
|
|