awesome_print 1.9.2 → 2.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -1
  3. data/Appraisals +2 -33
  4. data/CHANGELOG.md +6 -15
  5. data/CONTRIBUTING.md +0 -1
  6. data/README.md +7 -49
  7. data/lib/ap.rb +1 -5
  8. data/lib/awesome_print.rb +7 -29
  9. data/lib/awesome_print/colorize.rb +2 -0
  10. data/lib/awesome_print/core_ext/active_support.rb +7 -0
  11. data/lib/awesome_print/core_ext/awesome_method_array.rb +0 -6
  12. data/lib/awesome_print/core_ext/class.rb +0 -5
  13. data/lib/awesome_print/core_ext/kernel.rb +0 -5
  14. data/lib/awesome_print/core_ext/logger.rb +0 -5
  15. data/lib/awesome_print/core_ext/object.rb +0 -5
  16. data/lib/awesome_print/core_ext/string.rb +0 -5
  17. data/lib/awesome_print/custom_defaults.rb +1 -1
  18. data/lib/awesome_print/formatter.rb +38 -106
  19. data/lib/awesome_print/formatters/array_formatter.rb +11 -8
  20. data/lib/awesome_print/formatters/base_formatter.rb +20 -53
  21. data/lib/awesome_print/formatters/bigdecimal_formatter.rb +20 -0
  22. data/lib/awesome_print/formatters/class_formatter.rb +5 -6
  23. data/lib/awesome_print/formatters/dir_formatter.rb +4 -6
  24. data/lib/awesome_print/formatters/ext/active_model_error_formatter.rb +44 -0
  25. data/lib/awesome_print/formatters/ext/active_record_attributeset_formatter.rb +27 -0
  26. data/lib/awesome_print/formatters/ext/active_record_class_formatter.rb +43 -0
  27. data/lib/awesome_print/formatters/ext/active_record_instance_formatter.rb +48 -0
  28. data/lib/awesome_print/formatters/ext/active_record_relation_formatter.rb +19 -0
  29. data/lib/awesome_print/formatters/ext/bson_objectid_formatter.rb +20 -0
  30. data/lib/awesome_print/formatters/ext/hash_with_indifferent_access_formatter.rb +20 -0
  31. data/lib/awesome_print/formatters/ext/mongoid_document_formatter.rb +59 -0
  32. data/lib/awesome_print/formatters/ext/nokogiri_document_formatter.rb +30 -0
  33. data/lib/awesome_print/formatters/ext/nokogiri_node_formatter.rb +29 -0
  34. data/lib/awesome_print/formatters/ext/nokogiri_nodeset_formatter.rb +32 -0
  35. data/lib/awesome_print/formatters/ext/sequel_dataset_formatter.rb +20 -0
  36. data/lib/awesome_print/formatters/ext/sequel_model_class_formatter.rb +25 -0
  37. data/lib/awesome_print/formatters/ext/sequel_model_formatter.rb +26 -0
  38. data/lib/awesome_print/formatters/ext/time_with_zone_formatter.rb +24 -0
  39. data/lib/awesome_print/formatters/fallback_formatter.rb +55 -0
  40. data/lib/awesome_print/formatters/false_class_formatter.rb +15 -0
  41. data/lib/awesome_print/formatters/file_formatter.rb +4 -6
  42. data/lib/awesome_print/formatters/hash_formatter.rb +12 -7
  43. data/lib/awesome_print/formatters/integer_formatter.rb +11 -0
  44. data/lib/awesome_print/formatters/method_formatter.rb +8 -6
  45. data/lib/awesome_print/formatters/module_formatter.rb +18 -0
  46. data/lib/awesome_print/formatters/nil_class_formatter.rb +19 -0
  47. data/lib/awesome_print/formatters/object_formatter.rb +19 -9
  48. data/lib/awesome_print/formatters/open_struct.rb +19 -0
  49. data/lib/awesome_print/formatters/range_formatter.rb +11 -0
  50. data/lib/awesome_print/formatters/rational_formatter.rb +15 -0
  51. data/lib/awesome_print/formatters/set_formatter.rb +19 -0
  52. data/lib/awesome_print/formatters/simple_formatter.rb +10 -8
  53. data/lib/awesome_print/formatters/string_formatter.rb +14 -0
  54. data/lib/awesome_print/formatters/struct_formatter.rb +8 -6
  55. data/lib/awesome_print/formatters/symbol_formatter.rb +18 -0
  56. data/lib/awesome_print/formatters/true_class_formatter.rb +15 -0
  57. data/lib/awesome_print/formatters/unbound_method_formatter.rb +15 -0
  58. data/lib/awesome_print/inspector.rb +18 -17
  59. data/lib/awesome_print/limiter.rb +63 -0
  60. data/lib/awesome_print/registrar.rb +20 -0
  61. data/lib/awesome_print/version.rb +6 -6
  62. data/spec/formatters/array_spec.rb +251 -0
  63. data/spec/formatters/big_decimal_spec.rb +28 -0
  64. data/spec/formatters/class_spec.rb +91 -0
  65. data/spec/formatters/dir_file_spec.rb +43 -0
  66. data/spec/{ext → formatters/ext}/active_record_spec.rb +14 -58
  67. data/spec/{ext → formatters/ext}/active_support_spec.rb +7 -11
  68. data/spec/{ext → formatters/ext}/mongoid_spec.rb +2 -2
  69. data/spec/{ext → formatters/ext}/nokogiri_spec.rb +0 -0
  70. data/spec/formatters/hash_spec.rb +284 -0
  71. data/spec/{methods_spec.rb → formatters/methods_spec.rb} +3 -5
  72. data/spec/{objects_spec.rb → formatters/objects_spec.rb} +0 -0
  73. data/spec/{ext → formatters}/ostruct_spec.rb +1 -0
  74. data/spec/formatters/set_spec.rb +49 -0
  75. data/spec/formatters/struct_spec.rb +61 -0
  76. data/spec/merge_options_spec.rb +13 -0
  77. data/spec/misc_spec.rb +3 -10
  78. data/spec/spec_helper.rb +2 -9
  79. data/spec/support/ext_verifier.rb +0 -15
  80. data/spec/support/mongoid_versions.rb +4 -8
  81. data/spec/support/rails_versions.rb +2 -32
  82. metadata +100 -78
  83. data/awesome_print.gemspec +0 -33
  84. data/init.rb +0 -1
  85. data/lib/awesome_print/core_ext/method.rb +0 -21
  86. data/lib/awesome_print/ext/action_view.rb +0 -22
  87. data/lib/awesome_print/ext/active_record.rb +0 -103
  88. data/lib/awesome_print/ext/active_support.rb +0 -47
  89. data/lib/awesome_print/ext/mongo_mapper.rb +0 -124
  90. data/lib/awesome_print/ext/mongoid.rb +0 -67
  91. data/lib/awesome_print/ext/nobrainer.rb +0 -52
  92. data/lib/awesome_print/ext/nokogiri.rb +0 -45
  93. data/lib/awesome_print/ext/ostruct.rb +0 -27
  94. data/lib/awesome_print/ext/ripple.rb +0 -71
  95. data/lib/awesome_print/ext/sequel.rb +0 -58
  96. data/spec/ext/action_view_spec.rb +0 -21
  97. data/spec/ext/mongo_mapper_spec.rb +0 -261
  98. data/spec/ext/nobrainer_spec.rb +0 -59
  99. data/spec/ext/ripple_spec.rb +0 -48
  100. data/spec/formats_spec.rb +0 -779
  101. data/spec/support/active_record_data/3_2_diana.txt +0 -24
  102. data/spec/support/active_record_data/3_2_diana_legacy.txt +0 -24
  103. data/spec/support/active_record_data/3_2_multi.txt +0 -50
  104. data/spec/support/active_record_data/3_2_multi_legacy.txt +0 -50
  105. data/spec/support/active_record_data/4_0_diana.txt +0 -98
  106. data/spec/support/active_record_data/4_0_multi.txt +0 -198
  107. data/spec/support/active_record_data/4_1_diana.txt +0 -97
  108. data/spec/support/active_record_data/4_1_multi.txt +0 -196
  109. data/spec/support/active_record_data/4_2_diana.txt +0 -109
  110. data/spec/support/active_record_data/4_2_diana_legacy.txt +0 -109
  111. data/spec/support/active_record_data/4_2_multi.txt +0 -220
  112. data/spec/support/active_record_data/4_2_multi_legacy.txt +0 -220
  113. data/spec/support/active_record_data/6_0_diana.txt +0 -104
  114. data/spec/support/active_record_data/6_0_multi.txt +0 -210
  115. data/spec/support/active_record_data/6_1_diana.txt +0 -109
  116. data/spec/support/active_record_data/6_1_multi.txt +0 -220
@@ -1,33 +0,0 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
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
-
7
- $:.push File.expand_path('../lib', __FILE__)
8
- require 'awesome_print/version'
9
-
10
- Gem::Specification.new do |s|
11
- s.name = 'awesome_print'
12
- s.version = AwesomePrint.version
13
- s.authors = 'Michael Dvorkin'
14
- s.date = Time.now.strftime('%Y-%m-%d')
15
- s.email = 'mike@dvorkin.net'
16
- s.homepage = 'https://github.com/awesome-print/awesome_print'
17
- s.summary = 'Pretty print Ruby objects with proper indentation and colors'
18
- s.description = 'Great Ruby debugging companion: pretty print Ruby objects to visualize their structure. Supports custom object formatting via plugins'
19
- s.license = 'MIT'
20
-
21
- s.files = Dir['[A-Z]*[^~]'] + Dir['lib/**/*.rb'] + Dir['spec/**/*'] + ['.gitignore']
22
- s.test_files = Dir['spec/**/*']
23
- s.executables = []
24
- s.require_paths = ['lib']
25
-
26
- s.add_development_dependency 'rspec', '>= 3.0.0'
27
- s.add_development_dependency 'appraisal'
28
- s.add_development_dependency 'fakefs', '>= 0.2.1'
29
- s.add_development_dependency 'sqlite3'
30
- s.add_development_dependency 'nokogiri', '>= 1.11.0'
31
- # s.add_development_dependency 'simplecov'
32
- # s.add_development_dependency 'codeclimate-test-reporter'
33
- end
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'lib', 'awesome_print')
@@ -1,21 +0,0 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
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
- #
7
- # Method#name was intorduced in Ruby 1.8.7 so we define it here as necessary.
8
- #
9
- unless nil.method(:class).respond_to?(:name)
10
- class Method
11
- def name
12
- inspect.split(/[#.>]/)[-1]
13
- end
14
- end
15
-
16
- class UnboundMethod
17
- def name
18
- inspect.split(/[#.>]/)[-1]
19
- end
20
- end
21
- end
@@ -1,22 +0,0 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
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
- # Use HTML colors and add default "debug_dump" class to the resulting HTML.
9
- def ap_debug(object, options = {})
10
- object.ai(
11
- options.merge(html: true)
12
- ).sub(
13
- /^<pre([\s>])/,
14
- '<pre class="debug_dump"\\1'
15
- )
16
- end
17
-
18
- alias ap ap_debug
19
- end
20
- end
21
-
22
- ActionView::Base.send(:include, AwesomePrint::ActionView)
@@ -1,103 +0,0 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
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::Base)
19
-
20
- if object.is_a?(::ActiveRecord::Base)
21
- cast = :active_record_instance
22
- elsif object.is_a?(::ActiveModel::Errors)
23
- cast = :active_model_error
24
- elsif object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base)
25
- cast = :active_record_class
26
- elsif type == :activerecord_relation || object.class.ancestors.include?(::ActiveRecord::Relation)
27
- cast = :array
28
- end
29
- cast
30
- end
31
-
32
- private
33
-
34
- # Format ActiveRecord instance object.
35
- #
36
- # NOTE: by default only instance attributes (i.e. columns) are shown. To format
37
- # ActiveRecord instance as regular object showing its instance variables and
38
- # accessors use :raw => true option:
39
- #
40
- # ap record, :raw => true
41
- #
42
- #------------------------------------------------------------------------------
43
- def awesome_active_record_instance(object)
44
- return object.inspect if !defined?(::ActiveSupport::OrderedHash)
45
- return awesome_object(object) if @options[:raw]
46
-
47
- data = if object.class.column_names != object.attributes.keys
48
- object.attributes
49
- else
50
- object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
51
- if object.has_attribute?(name) || object.new_record?
52
- value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name)
53
- hash[name.to_sym] = value
54
- end
55
- hash
56
- end
57
- end
58
- "#{object} #{awesome_hash(data)}"
59
- end
60
-
61
- # Format ActiveRecord class object.
62
- #------------------------------------------------------------------------------
63
- def awesome_active_record_class(object)
64
- return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:columns) || object.to_s == 'ActiveRecord::Base'
65
- return awesome_class(object) if object.respond_to?(:abstract_class?) && object.abstract_class?
66
-
67
- data = object.columns.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
68
- hash[c.name.to_sym] = c.type
69
- hash
70
- end
71
-
72
- name = "class #{awesome_simple(object.to_s, :class)}"
73
- base = "< #{awesome_simple(object.superclass.to_s, :class)}"
74
-
75
- [name, base, awesome_hash(data)].join(' ')
76
- end
77
-
78
- # Format ActiveModel error object.
79
- #------------------------------------------------------------------------------
80
- def awesome_active_model_error(object)
81
- return object.inspect if !defined?(::ActiveSupport::OrderedHash)
82
- return awesome_object(object) if @options[:raw]
83
-
84
- object_dump = object.marshal_dump.first
85
- data = if object_dump.class.column_names != object_dump.attributes.keys
86
- object_dump.attributes
87
- else
88
- object_dump.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
89
- if object_dump.has_attribute?(name) || object_dump.new_record?
90
- value = object_dump.respond_to?(name) ? object_dump.send(name) : object_dump.read_attribute(name)
91
- hash[name.to_sym] = value
92
- end
93
- hash
94
- end
95
- end
96
-
97
- data.merge!({details: object.details, messages: object.messages})
98
- "#{object} #{awesome_hash(data)}"
99
- end
100
- end
101
- end
102
-
103
- AwesomePrint::Formatter.send(:include, AwesomePrint::ActiveRecord)
@@ -1,47 +0,0 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
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 (defined?(::ActiveSupport::TimeWithZone) && 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
-
@@ -1,124 +0,0 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
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
- apply_default_mongo_mapper_options
18
- cast = cast_without_mongo_mapper(object, type)
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
30
- end
31
-
32
- cast
33
- end
34
-
35
- # Format MongoMapper class object.
36
- #------------------------------------------------------------------------------
37
- def awesome_mongo_mapper_class(object)
38
- return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:keys)
39
-
40
- data = object.keys.sort.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
41
- hash[c.first] = (c.last.type || 'undefined').to_s.underscore.intern
42
- hash
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
-
52
- name = "class #{awesome_simple(object.to_s, :class)}"
53
- base = "< #{awesome_simple(object.superclass.to_s, :class)}"
54
-
55
- [name, base, awesome_hash(data)].join(' ')
56
- end
57
-
58
- # Format MongoMapper instance object.
59
- #
60
- # NOTE: by default only instance attributes (i.e. keys) are shown. To format
61
- # MongoMapper instance as regular object showing its instance variables and
62
- # accessors use :raw => true option:
63
- #
64
- # ap record, :raw => true
65
- #
66
- #------------------------------------------------------------------------------
67
- def awesome_mongo_mapper_instance(object)
68
- return object.inspect if !defined?(::ActiveSupport::OrderedHash)
69
- return awesome_object(object) if @options[:raw]
70
-
71
- data = object.keys.keys.sort_by { |k| k }.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
72
- hash[name] = object[name]
73
- hash
74
- end
75
-
76
- # Add in associations
77
- if @options[:mongo_mapper][:show_associations]
78
- object.associations.each do |name, assoc|
79
- data[name.to_s] = if @options[:mongo_mapper][:inline_embedded] and assoc.embeddable?
80
- object.send(name)
81
- else
82
- assoc
83
- end
84
- end
85
- end
86
-
87
- label = object.to_s
88
- label = "#{colorize('embedded', :assoc)} #{label}" if object.is_a?(::MongoMapper::EmbeddedDocument)
89
-
90
- "#{label} " << awesome_hash(data)
91
- end
92
-
93
- # Format MongoMapper association object.
94
- #------------------------------------------------------------------------------
95
- def awesome_mongo_mapper_association(object)
96
- return object.inspect if !defined?(::ActiveSupport::OrderedHash)
97
- return awesome_object(object) if @options[:raw]
98
-
99
- association = object.class.name.split('::').last.titleize.downcase.sub(/ association$/, '')
100
- association = "embeds #{association}" if object.embeddable?
101
- class_name = object.class_name
102
-
103
- "#{colorize(association, :assoc)} #{colorize(class_name, :class)}"
104
- end
105
-
106
- # Format BSON::ObjectId
107
- #------------------------------------------------------------------------------
108
- def awesome_mongo_mapper_bson_id(object)
109
- object.inspect
110
- end
111
-
112
- private
113
-
114
- def apply_default_mongo_mapper_options
115
- @options[:color][:assoc] ||= :greenish
116
- @options[:mongo_mapper] ||= {
117
- show_associations: false, # Display association data for MongoMapper documents and classes.
118
- inline_embedded: false # Display embedded associations inline with MongoMapper documents.
119
- }
120
- end
121
- end
122
- end
123
-
124
- AwesomePrint::Formatter.send(:include, AwesomePrint::MongoMapper)
@@ -1,67 +0,0 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
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 (defined?(::BSON) && object.is_a?(::BSON::ObjectId)) || (defined?(::Moped::BSON) && object.is_a?(::Moped::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
-
40
- name = "class #{awesome_simple(object.to_s, :class)}"
41
- base = "< #{awesome_simple(object.superclass.to_s, :class)}"
42
-
43
- [name, base, awesome_hash(data)].join(' ')
44
- end
45
-
46
- # Format Mongoid Document object.
47
- #------------------------------------------------------------------------------
48
- def awesome_mongoid_document(object)
49
- return object.inspect if !defined?(::ActiveSupport::OrderedHash)
50
-
51
- data = (object.attributes || {}).sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
52
- hash[c[0].to_sym] = c[1]
53
- hash
54
- end
55
- data = { errors: object.errors, attributes: data } if !object.errors.empty?
56
- "#{object} #{awesome_hash(data)}"
57
- end
58
-
59
- # Format BSON::ObjectId
60
- #------------------------------------------------------------------------------
61
- def awesome_mongoid_bson_id(object)
62
- object.inspect
63
- end
64
- end
65
- end
66
-
67
- AwesomePrint::Formatter.send(:include, AwesomePrint::Mongoid)
@@ -1,52 +0,0 @@
1
- # Copyright (c) 2010-2016 Michael Dvorkin and contributors
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 NoBrainer
8
-
9
- def self.included(base)
10
- base.send :alias_method, :cast_without_nobrainer, :cast
11
- base.send :alias_method, :cast, :cast_with_nobrainer
12
- end
13
-
14
- # Add NoBrainer class names to the dispatcher pipeline.
15
- #------------------------------------------------------------------------------
16
- def cast_with_nobrainer(object, type)
17
- cast = cast_without_nobrainer(object, type)
18
- if defined?(::NoBrainer::Document)
19
- if object.is_a?(Class) && object < ::NoBrainer::Document
20
- cast = :nobrainer_class
21
- elsif object.is_a?(::NoBrainer::Document)
22
- cast = :nobrainer_document
23
- end
24
- end
25
- cast
26
- end
27
-
28
- # Format NoBrainer class object.
29
- #------------------------------------------------------------------------------
30
- def awesome_nobrainer_class(object)
31
- name = "#{awesome_simple(object, :class)} < #{awesome_simple(object.superclass, :class)}"
32
- data = Hash[object.fields.map do |field, options|
33
- [field, (options[:type] || Object).to_s.underscore.to_sym]
34
- end]
35
-
36
- name = "class #{awesome_simple(object.to_s, :class)}"
37
- base = "< #{awesome_simple(object.superclass.to_s, :class)}"
38
-
39
- [name, base, awesome_hash(data)].join(' ')
40
- end
41
-
42
- # Format NoBrainer Document object.
43
- #------------------------------------------------------------------------------
44
- def awesome_nobrainer_document(object)
45
- data = object.inspectable_attributes.symbolize_keys
46
- data = { errors: object.errors, attributes: data } if object.errors.present?
47
- "#{object} #{awesome_hash(data)}"
48
- end
49
- end
50
- end
51
-
52
- AwesomePrint::Formatter.send(:include, AwesomePrint::NoBrainer)