amazing_print 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +35 -0
  3. data/Appraisals +60 -0
  4. data/CHANGELOG.md +2 -0
  5. data/CONTRIBUTING.md +81 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE +21 -0
  8. data/README.md +356 -0
  9. data/Rakefile +23 -0
  10. data/lib/amazing_print.rb +46 -0
  11. data/lib/amazing_print/colorize.rb +25 -0
  12. data/lib/amazing_print/core_ext/awesome_method_array.rb +82 -0
  13. data/lib/amazing_print/core_ext/class.rb +23 -0
  14. data/lib/amazing_print/core_ext/kernel.rb +25 -0
  15. data/lib/amazing_print/core_ext/logger.rb +21 -0
  16. data/lib/amazing_print/core_ext/method.rb +21 -0
  17. data/lib/amazing_print/core_ext/object.rb +23 -0
  18. data/lib/amazing_print/core_ext/string.rb +42 -0
  19. data/lib/amazing_print/custom_defaults.rb +57 -0
  20. data/lib/amazing_print/ext/action_view.rb +22 -0
  21. data/lib/amazing_print/ext/active_record.rb +103 -0
  22. data/lib/amazing_print/ext/active_support.rb +45 -0
  23. data/lib/amazing_print/ext/mongo_mapper.rb +125 -0
  24. data/lib/amazing_print/ext/mongoid.rb +68 -0
  25. data/lib/amazing_print/ext/nobrainer.rb +53 -0
  26. data/lib/amazing_print/ext/nokogiri.rb +45 -0
  27. data/lib/amazing_print/ext/ostruct.rb +27 -0
  28. data/lib/amazing_print/ext/ripple.rb +71 -0
  29. data/lib/amazing_print/ext/sequel.rb +55 -0
  30. data/lib/amazing_print/formatter.rb +120 -0
  31. data/lib/amazing_print/formatters.rb +14 -0
  32. data/lib/amazing_print/formatters/array_formatter.rb +139 -0
  33. data/lib/amazing_print/formatters/base_formatter.rb +148 -0
  34. data/lib/amazing_print/formatters/class_formatter.rb +24 -0
  35. data/lib/amazing_print/formatters/dir_formatter.rb +21 -0
  36. data/lib/amazing_print/formatters/file_formatter.rb +21 -0
  37. data/lib/amazing_print/formatters/hash_formatter.rb +106 -0
  38. data/lib/amazing_print/formatters/method_formatter.rb +21 -0
  39. data/lib/amazing_print/formatters/object_formatter.rb +82 -0
  40. data/lib/amazing_print/formatters/simple_formatter.rb +20 -0
  41. data/lib/amazing_print/formatters/struct_formatter.rb +74 -0
  42. data/lib/amazing_print/indentator.rb +17 -0
  43. data/lib/amazing_print/inspector.rb +175 -0
  44. data/lib/amazing_print/version.rb +10 -0
  45. data/lib/ap.rb +10 -0
  46. data/spec/active_record_helper.rb +30 -0
  47. data/spec/colors_spec.rb +114 -0
  48. data/spec/core_ext/logger_spec.rb +44 -0
  49. data/spec/core_ext/string_spec.rb +20 -0
  50. data/spec/ext/action_view_spec.rb +17 -0
  51. data/spec/ext/active_record_spec.rb +297 -0
  52. data/spec/ext/active_support_spec.rb +26 -0
  53. data/spec/ext/mongo_mapper_spec.rb +259 -0
  54. data/spec/ext/mongoid_spec.rb +66 -0
  55. data/spec/ext/nobrainer_spec.rb +58 -0
  56. data/spec/ext/nokogiri_spec.rb +50 -0
  57. data/spec/ext/ostruct_spec.rb +22 -0
  58. data/spec/ext/ripple_spec.rb +47 -0
  59. data/spec/formats_spec.rb +779 -0
  60. data/spec/methods_spec.rb +478 -0
  61. data/spec/misc_spec.rb +245 -0
  62. data/spec/objects_spec.rb +219 -0
  63. data/spec/spec_helper.rb +106 -0
  64. data/spec/support/active_record_data.rb +20 -0
  65. data/spec/support/active_record_data/3_2_diana.txt +24 -0
  66. data/spec/support/active_record_data/3_2_diana_legacy.txt +24 -0
  67. data/spec/support/active_record_data/3_2_multi.txt +50 -0
  68. data/spec/support/active_record_data/3_2_multi_legacy.txt +50 -0
  69. data/spec/support/active_record_data/4_0_diana.txt +98 -0
  70. data/spec/support/active_record_data/4_0_multi.txt +198 -0
  71. data/spec/support/active_record_data/4_1_diana.txt +97 -0
  72. data/spec/support/active_record_data/4_1_multi.txt +196 -0
  73. data/spec/support/active_record_data/4_2_diana.txt +109 -0
  74. data/spec/support/active_record_data/4_2_diana_legacy.txt +109 -0
  75. data/spec/support/active_record_data/4_2_multi.txt +220 -0
  76. data/spec/support/active_record_data/4_2_multi_legacy.txt +220 -0
  77. data/spec/support/active_record_data/5_0_diana.txt +105 -0
  78. data/spec/support/active_record_data/5_0_multi.txt +212 -0
  79. data/spec/support/active_record_data/5_1_diana.txt +104 -0
  80. data/spec/support/active_record_data/5_1_multi.txt +210 -0
  81. data/spec/support/active_record_data/5_2_diana.txt +104 -0
  82. data/spec/support/active_record_data/5_2_multi.txt +210 -0
  83. data/spec/support/active_record_data/6_0_diana.txt +104 -0
  84. data/spec/support/active_record_data/6_0_multi.txt +210 -0
  85. data/spec/support/ext_verifier.rb +41 -0
  86. data/spec/support/mongoid_versions.rb +22 -0
  87. data/spec/support/rails_versions.rb +50 -0
  88. metadata +243 -0
@@ -0,0 +1,45 @@
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
+ #
3
+ # AmazingPrint 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 AmazingPrint
7
+ module ActiveSupport
8
+ def self.included(base)
9
+ base.send :alias_method, :cast_without_active_support, :cast
10
+ base.send :alias_method, :cast, :cast_with_active_support
11
+ end
12
+
13
+ def cast_with_active_support(object, type)
14
+ cast = cast_without_active_support(object, type)
15
+ if defined?(::ActiveSupport) && defined?(::HashWithIndifferentAccess)
16
+ if (defined?(::ActiveSupport::TimeWithZone) && object.is_a?(::ActiveSupport::TimeWithZone)) || object.is_a?(::Date)
17
+ cast = :active_support_time
18
+ elsif object.is_a?(::HashWithIndifferentAccess)
19
+ cast = :hash_with_indifferent_access
20
+ end
21
+ end
22
+ cast
23
+ end
24
+
25
+ # Format ActiveSupport::TimeWithZone as standard Time.
26
+ #------------------------------------------------------------------------------
27
+ def awesome_active_support_time(object)
28
+ colorize(object.inspect, :time)
29
+ end
30
+
31
+ # Format HashWithIndifferentAccess as standard Hash.
32
+ #------------------------------------------------------------------------------
33
+ def awesome_hash_with_indifferent_access(object)
34
+ awesome_hash(object)
35
+ end
36
+ end
37
+ end
38
+
39
+ AmazingPrint::Formatter.include AmazingPrint::ActiveSupport
40
+ #
41
+ # Colorize Rails logs.
42
+ #
43
+ if defined?(ActiveSupport::LogSubscriber)
44
+ AmazingPrint.force_colors! ActiveSupport::LogSubscriber.colorize_logging
45
+ end
@@ -0,0 +1,125 @@
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
+ #
3
+ # AmazingPrint 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 AmazingPrint
7
+ module MongoMapper
8
+ def self.included(base)
9
+ base.send :alias_method, :cast_without_mongo_mapper, :cast
10
+ base.send :alias_method, :cast, :cast_with_mongo_mapper
11
+ end
12
+
13
+ # Add MongoMapper class names to the dispatcher pipeline.
14
+ #------------------------------------------------------------------------------
15
+ def cast_with_mongo_mapper(object, type)
16
+ apply_default_mongo_mapper_options
17
+ cast = cast_without_mongo_mapper(object, type)
18
+
19
+ if defined?(::MongoMapper::Document)
20
+ if object.is_a?(Class) && (object.ancestors & [::MongoMapper::Document, ::MongoMapper::EmbeddedDocument]).size > 0
21
+ cast = :mongo_mapper_class
22
+ elsif object.is_a?(::MongoMapper::Document) || object.is_a?(::MongoMapper::EmbeddedDocument)
23
+ cast = :mongo_mapper_instance
24
+ elsif object.is_a?(::MongoMapper::Plugins::Associations::Base)
25
+ cast = :mongo_mapper_association
26
+ elsif object.is_a?(::BSON::ObjectId)
27
+ cast = :mongo_mapper_bson_id
28
+ end
29
+ end
30
+
31
+ cast
32
+ end
33
+
34
+ # Format MongoMapper class object.
35
+ #------------------------------------------------------------------------------
36
+ def awesome_mongo_mapper_class(object)
37
+ if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:keys)
38
+ return object.inspect
39
+ end
40
+
41
+ data = object.keys.sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
42
+ hash[c.first] = (c.last.type || 'undefined').to_s.underscore.intern
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 unless defined?(::ActiveSupport::OrderedHash)
69
+ return awesome_object(object) if @options[:raw]
70
+
71
+ data = object.keys.keys.sort.each_with_object(::ActiveSupport::OrderedHash.new) do |name, hash|
72
+ hash[name] = object[name]
73
+ end
74
+
75
+ # Add in associations
76
+ if @options[:mongo_mapper][:show_associations]
77
+ object.associations.each do |name, assoc|
78
+ data[name.to_s] = if @options[:mongo_mapper][:inline_embedded] && assoc.embeddable?
79
+ object.send(name)
80
+ else
81
+ assoc
82
+ end
83
+ end
84
+ end
85
+
86
+ label = object.to_s
87
+ if object.is_a?(::MongoMapper::EmbeddedDocument)
88
+ label = "#{colorize('embedded', :assoc)} #{label}"
89
+ end
90
+
91
+ "#{label} " << awesome_hash(data)
92
+ end
93
+
94
+ # Format MongoMapper association object.
95
+ #------------------------------------------------------------------------------
96
+ def awesome_mongo_mapper_association(object)
97
+ return object.inspect unless defined?(::ActiveSupport::OrderedHash)
98
+ return awesome_object(object) if @options[:raw]
99
+
100
+ association = object.class.name.split('::').last.titleize.downcase.sub(/ association$/, '')
101
+ association = "embeds #{association}" if object.embeddable?
102
+ class_name = object.class_name
103
+
104
+ "#{colorize(association, :assoc)} #{colorize(class_name, :class)}"
105
+ end
106
+
107
+ # Format BSON::ObjectId
108
+ #------------------------------------------------------------------------------
109
+ def awesome_mongo_mapper_bson_id(object)
110
+ object.inspect
111
+ end
112
+
113
+ private
114
+
115
+ def apply_default_mongo_mapper_options
116
+ @options[:color][:assoc] ||= :greenish
117
+ @options[:mongo_mapper] ||= {
118
+ show_associations: false, # Display association data for MongoMapper documents and classes.
119
+ inline_embedded: false # Display embedded associations inline with MongoMapper documents.
120
+ }
121
+ end
122
+ end
123
+ end
124
+
125
+ AmazingPrint::Formatter.include AmazingPrint::MongoMapper
@@ -0,0 +1,68 @@
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
+ #
3
+ # AmazingPrint 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 AmazingPrint
7
+ module Mongoid
8
+ def self.included(base)
9
+ base.send :alias_method, :cast_without_mongoid, :cast
10
+ base.send :alias_method, :cast, :cast_with_mongoid
11
+ end
12
+
13
+ # Add Mongoid class names to the dispatcher pipeline.
14
+ #------------------------------------------------------------------------------
15
+ def cast_with_mongoid(object, type)
16
+ cast = cast_without_mongoid(object, type)
17
+ if defined?(::Mongoid::Document)
18
+ if object.is_a?(Class) && object.ancestors.include?(::Mongoid::Document)
19
+ cast = :mongoid_class
20
+ elsif object.class.ancestors.include?(::Mongoid::Document)
21
+ cast = :mongoid_document
22
+ elsif (defined?(::BSON) && object.is_a?(::BSON::ObjectId)) || (defined?(::Moped::BSON) && object.is_a?(::Moped::BSON::ObjectId))
23
+ cast = :mongoid_bson_id
24
+ end
25
+ end
26
+ cast
27
+ end
28
+
29
+ # Format Mongoid class object.
30
+ #------------------------------------------------------------------------------
31
+ def awesome_mongoid_class(object)
32
+ if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields)
33
+ return object.inspect
34
+ end
35
+
36
+ data = object.fields.sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
37
+ hash[c[1].name.to_sym] = (c[1].type || 'undefined').to_s.underscore.intern
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 unless defined?(::ActiveSupport::OrderedHash)
50
+
51
+ data = (object.attributes || {}).sort.each_with_object(::ActiveSupport::OrderedHash.new) do |c, hash|
52
+ hash[c[0].to_sym] = c[1]
53
+ end
54
+ unless object.errors.empty?
55
+ data = { errors: object.errors, attributes: data }
56
+ end
57
+ "#{object} #{awesome_hash(data)}"
58
+ end
59
+
60
+ # Format BSON::ObjectId
61
+ #------------------------------------------------------------------------------
62
+ def awesome_mongoid_bson_id(object)
63
+ object.inspect
64
+ end
65
+ end
66
+ end
67
+
68
+ AmazingPrint::Formatter.include AmazingPrint::Mongoid
@@ -0,0 +1,53 @@
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
+ #
3
+ # AmazingPrint 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 AmazingPrint
7
+ module NoBrainer
8
+ def self.included(base)
9
+ base.send :alias_method, :cast_without_nobrainer, :cast
10
+ base.send :alias_method, :cast, :cast_with_nobrainer
11
+ end
12
+
13
+ # Add NoBrainer class names to the dispatcher pipeline.
14
+ #------------------------------------------------------------------------------
15
+ def cast_with_nobrainer(object, type)
16
+ cast = cast_without_nobrainer(object, type)
17
+ if defined?(::NoBrainer::Document)
18
+ if object.is_a?(Class) && object < ::NoBrainer::Document
19
+ cast = :nobrainer_class
20
+ elsif object.is_a?(::NoBrainer::Document)
21
+ cast = :nobrainer_document
22
+ end
23
+ end
24
+ cast
25
+ end
26
+
27
+ # Format NoBrainer class object.
28
+ #------------------------------------------------------------------------------
29
+ def awesome_nobrainer_class(object)
30
+ name = "#{awesome_simple(object, :class)} < #{awesome_simple(object.superclass, :class)}"
31
+ data = Hash[object.fields.map do |field, options|
32
+ [field, (options[:type] || Object).to_s.underscore.to_sym]
33
+ end]
34
+
35
+ name = "class #{awesome_simple(object.to_s, :class)}"
36
+ base = "< #{awesome_simple(object.superclass.to_s, :class)}"
37
+
38
+ [name, base, awesome_hash(data)].join(' ')
39
+ end
40
+
41
+ # Format NoBrainer Document object.
42
+ #------------------------------------------------------------------------------
43
+ def awesome_nobrainer_document(object)
44
+ data = object.inspectable_attributes.symbolize_keys
45
+ if object.errors.present?
46
+ data = { errors: object.errors, attributes: data }
47
+ end
48
+ "#{object} #{awesome_hash(data)}"
49
+ end
50
+ end
51
+ end
52
+
53
+ AmazingPrint::Formatter.include AmazingPrint::NoBrainer
@@ -0,0 +1,45 @@
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
+ #
3
+ # AmazingPrint 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 AmazingPrint
7
+ module Nokogiri
8
+ def self.included(base)
9
+ base.send :alias_method, :cast_without_nokogiri, :cast
10
+ base.send :alias_method, :cast, :cast_with_nokogiri
11
+ end
12
+
13
+ # Add Nokogiri XML Node and NodeSet names to the dispatcher pipeline.
14
+ #------------------------------------------------------------------------------
15
+ def cast_with_nokogiri(object, type)
16
+ cast = cast_without_nokogiri(object, type)
17
+ if (defined?(::Nokogiri::XML::Node) && object.is_a?(::Nokogiri::XML::Node)) ||
18
+ (defined?(::Nokogiri::XML::NodeSet) && object.is_a?(::Nokogiri::XML::NodeSet))
19
+ cast = :nokogiri_xml_node
20
+ end
21
+ cast
22
+ end
23
+
24
+ #------------------------------------------------------------------------------
25
+ def awesome_nokogiri_xml_node(object)
26
+ return '[]' if object.is_a?(::Nokogiri::XML::NodeSet) && object.empty?
27
+
28
+ xml = object.to_xml(indent: 2)
29
+ #
30
+ # Colorize tag, id/class name, and contents.
31
+ #
32
+ xml.gsub!(%r{(<)(/?[A-Za-z1-9]+)}) { |_tag| "#{Regexp.last_match(1)}#{colorize(Regexp.last_match(2), :keyword)}" }
33
+ xml.gsub!(/(id|class)="[^"]+"/i) { |id| colorize(id, :class) }
34
+ xml.gsub!(/>([^<]+)</) do |contents|
35
+ if contents && !contents.empty?
36
+ contents = colorize(Regexp.last_match(1), :trueclass)
37
+ end
38
+ ">#{contents}<"
39
+ end
40
+ xml
41
+ end
42
+ end
43
+ end
44
+
45
+ AmazingPrint::Formatter.include AmazingPrint::Nokogiri
@@ -0,0 +1,27 @@
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
+ #
3
+ # AmazingPrint 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 AmazingPrint
7
+ module OpenStruct
8
+ def self.included(base)
9
+ base.send :alias_method, :cast_without_ostruct, :cast
10
+ base.send :alias_method, :cast, :cast_with_ostruct
11
+ end
12
+
13
+ def cast_with_ostruct(object, type)
14
+ cast = cast_without_ostruct(object, type)
15
+ if defined?(::OpenStruct) && object.is_a?(::OpenStruct)
16
+ cast = :open_struct_instance
17
+ end
18
+ cast
19
+ end
20
+
21
+ def awesome_open_struct_instance(object)
22
+ "#{object.class} #{awesome_hash(object.marshal_dump)}"
23
+ end
24
+ end
25
+ end
26
+
27
+ AmazingPrint::Formatter.include AmazingPrint::OpenStruct
@@ -0,0 +1,71 @@
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
+ #
3
+ # AmazingPrint 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 AmazingPrint
7
+ module Ripple
8
+ def self.included(base)
9
+ base.send :alias_method, :cast_without_ripple, :cast
10
+ base.send :alias_method, :cast, :cast_with_ripple
11
+ end
12
+
13
+ # Add Ripple class names to the dispatcher pipeline.
14
+ #------------------------------------------------------------------------------
15
+ def cast_with_ripple(object, type)
16
+ cast = cast_without_ripple(object, type)
17
+ return cast unless defined?(::Ripple)
18
+
19
+ if object.is_a?(::Ripple::AttributeMethods) # Module used to access attributes across documents and embedded documents
20
+ cast = :ripple_document_instance
21
+ elsif object.is_a?(::Ripple::Properties) # Used to access property metadata on Ripple classes
22
+ cast = :ripple_document_class
23
+ end
24
+ cast
25
+ end
26
+
27
+ private
28
+
29
+ # Format Ripple instance object.
30
+ #
31
+ # NOTE: by default only instance attributes are shown. To format a Ripple document instance
32
+ # as a regular object showing its instance variables and accessors use :raw => true option:
33
+ #
34
+ # ap document, :raw => true
35
+ #
36
+ #------------------------------------------------------------------------------
37
+ def awesome_ripple_document_instance(object)
38
+ return object.inspect unless defined?(::ActiveSupport::OrderedHash)
39
+ return awesome_object(object) if @options[:raw]
40
+
41
+ (exclude_assoc = @options[:exclude_assoc]) || @options[:exclude_associations]
42
+
43
+ data = object.attributes.each_with_object(::ActiveSupport::OrderedHash.new) do |(name, _value), hash|
44
+ hash[name.to_sym] = object.send(name)
45
+ end
46
+
47
+ unless exclude_assoc
48
+ data = object.class.embedded_associations.each_with_object(data) do |assoc, hash|
49
+ hash[assoc.name] = object.get_proxy(assoc) # Should always be array or Ripple::EmbeddedDocument for embedded associations
50
+ end
51
+ end
52
+
53
+ "#{object} " << awesome_hash(data)
54
+ end
55
+
56
+ # Format Ripple class object.
57
+ #------------------------------------------------------------------------------
58
+ def awesome_ripple_document_class(object)
59
+ if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:properties)
60
+ return object.inspect
61
+ end
62
+
63
+ name = "class #{awesome_simple(object.to_s, :class)}"
64
+ base = "< #{awesome_simple(object.superclass.to_s, :class)}"
65
+
66
+ [name, base, awesome_hash(data)].join(' ')
67
+ end
68
+ end
69
+ end
70
+
71
+ AmazingPrint::Formatter.include AmazingPrint::Ripple
@@ -0,0 +1,55 @@
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
+ #
3
+ # AmazingPrint 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 AmazingPrint
7
+ module Sequel
8
+ def self.included(base)
9
+ base.send :alias_method, :cast_without_sequel, :cast
10
+ base.send :alias_method, :cast, :cast_with_sequel
11
+ end
12
+
13
+ # Add Sequel class names to the dispatcher pipeline.
14
+ #------------------------------------------------------------------------------
15
+ def cast_with_sequel(object, type)
16
+ cast = cast_without_sequel(object, type)
17
+ if defined?(::Sequel::Model) && object.is_a?(::Sequel::Model)
18
+ cast = :sequel_document
19
+ elsif defined?(::Sequel::Model) && object.is_a?(Class) && object.ancestors.include?(::Sequel::Model)
20
+ cast = :sequel_model_class
21
+ elsif defined?(::Sequel::Mysql2::Dataset) && object.class.ancestors.include?(::Sequel::Mysql2::Dataset)
22
+ cast = :sequel_dataset
23
+ end
24
+ cast
25
+ end
26
+
27
+ # Format Sequel Document object.
28
+ #------------------------------------------------------------------------------
29
+ def awesome_sequel_document(object)
30
+ data = object.values.sort_by { |key| key.to_s }.each_with_object({}) do |c, hash|
31
+ hash[c[0].to_sym] = c[1]
32
+ end
33
+ data = { errors: object.errors, values: data } unless object.errors.empty?
34
+ "#{object} #{awesome_hash(data)}"
35
+ end
36
+
37
+ # Format Sequel Dataset object.
38
+ #------------------------------------------------------------------------------
39
+ def awesome_sequel_dataset(dataset)
40
+ [awesome_array(dataset.to_a), amazing_print(dataset.sql)].join("\n")
41
+ end
42
+
43
+ # Format Sequel Model class.
44
+ #------------------------------------------------------------------------------
45
+ def awesome_sequel_model_class(object)
46
+ data = object.db_schema.inject({}) { |h, (prop, defn)| h.merge(prop => defn[:db_type]) }
47
+ name = "class #{awesome_simple(object.to_s, :class)}"
48
+ base = "< #{awesome_simple(object.superclass.to_s, :class)}"
49
+
50
+ [name, base, awesome_hash(data)].join(' ')
51
+ end
52
+ end
53
+ end
54
+
55
+ AmazingPrint::Formatter.include AmazingPrint::Sequel