awesome_print 1.8.0 → 2.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/Appraisals +16 -48
  4. data/CHANGELOG.md +10 -1
  5. data/LICENSE +16 -19
  6. data/README.md +3 -10
  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/formatter.rb +38 -98
  18. data/lib/awesome_print/formatters/array_formatter.rb +10 -7
  19. data/lib/awesome_print/formatters/base_formatter.rb +17 -50
  20. data/lib/awesome_print/formatters/bigdecimal_formatter.rb +20 -0
  21. data/lib/awesome_print/formatters/class_formatter.rb +5 -6
  22. data/lib/awesome_print/formatters/dir_formatter.rb +4 -6
  23. data/lib/awesome_print/formatters/ext/active_model_error_formatter.rb +44 -0
  24. data/lib/awesome_print/formatters/ext/active_record_attributeset_formatter.rb +27 -0
  25. data/lib/awesome_print/formatters/ext/active_record_class_formatter.rb +43 -0
  26. data/lib/awesome_print/formatters/ext/active_record_instance_formatter.rb +48 -0
  27. data/lib/awesome_print/formatters/ext/active_record_relation_formatter.rb +19 -0
  28. data/lib/awesome_print/formatters/ext/bson_objectid_formatter.rb +20 -0
  29. data/lib/awesome_print/formatters/ext/hash_with_indifferent_access_formatter.rb +20 -0
  30. data/lib/awesome_print/formatters/ext/mongoid_document_formatter.rb +59 -0
  31. data/lib/awesome_print/formatters/ext/nokogiri_document_formatter.rb +30 -0
  32. data/lib/awesome_print/formatters/ext/nokogiri_node_formatter.rb +29 -0
  33. data/lib/awesome_print/formatters/ext/nokogiri_nodeset_formatter.rb +32 -0
  34. data/lib/awesome_print/formatters/ext/sequel_dataset_formatter.rb +20 -0
  35. data/lib/awesome_print/formatters/ext/sequel_model_class_formatter.rb +25 -0
  36. data/lib/awesome_print/formatters/ext/sequel_model_formatter.rb +26 -0
  37. data/lib/awesome_print/formatters/ext/time_with_zone_formatter.rb +24 -0
  38. data/lib/awesome_print/formatters/fallback_formatter.rb +55 -0
  39. data/lib/awesome_print/formatters/false_class_formatter.rb +15 -0
  40. data/lib/awesome_print/formatters/file_formatter.rb +4 -6
  41. data/lib/awesome_print/formatters/hash_formatter.rb +12 -7
  42. data/lib/awesome_print/formatters/integer_formatter.rb +11 -0
  43. data/lib/awesome_print/formatters/method_formatter.rb +8 -6
  44. data/lib/awesome_print/formatters/module_formatter.rb +18 -0
  45. data/lib/awesome_print/formatters/nil_class_formatter.rb +19 -0
  46. data/lib/awesome_print/formatters/object_formatter.rb +20 -7
  47. data/lib/awesome_print/formatters/open_struct.rb +19 -0
  48. data/lib/awesome_print/formatters/range_formatter.rb +11 -0
  49. data/lib/awesome_print/formatters/rational_formatter.rb +15 -0
  50. data/lib/awesome_print/formatters/set_formatter.rb +19 -0
  51. data/lib/awesome_print/formatters/simple_formatter.rb +10 -8
  52. data/lib/awesome_print/formatters/string_formatter.rb +14 -0
  53. data/lib/awesome_print/formatters/struct_formatter.rb +8 -6
  54. data/lib/awesome_print/formatters/symbol_formatter.rb +18 -0
  55. data/lib/awesome_print/formatters/true_class_formatter.rb +15 -0
  56. data/lib/awesome_print/formatters/unbound_method_formatter.rb +15 -0
  57. data/lib/awesome_print/inspector.rb +20 -16
  58. data/lib/awesome_print/limiter.rb +63 -0
  59. data/lib/awesome_print/registrar.rb +20 -0
  60. data/lib/awesome_print/version.rb +6 -6
  61. data/spec/formatters/array_spec.rb +251 -0
  62. data/spec/formatters/big_decimal_spec.rb +28 -0
  63. data/spec/formatters/class_spec.rb +91 -0
  64. data/spec/formatters/dir_file_spec.rb +43 -0
  65. data/spec/{ext → formatters/ext}/active_record_spec.rb +27 -63
  66. data/spec/{ext → formatters/ext}/active_support_spec.rb +6 -6
  67. data/spec/{ext → formatters/ext}/mongoid_spec.rb +4 -41
  68. data/spec/{ext → formatters/ext}/nokogiri_spec.rb +0 -0
  69. data/spec/formatters/hash_spec.rb +284 -0
  70. data/spec/{methods_spec.rb → formatters/methods_spec.rb} +13 -7
  71. data/spec/{objects_spec.rb → formatters/objects_spec.rb} +49 -0
  72. data/spec/{ext → formatters}/ostruct_spec.rb +1 -0
  73. data/spec/formatters/set_spec.rb +49 -0
  74. data/spec/formatters/struct_spec.rb +61 -0
  75. data/spec/merge_options_spec.rb +13 -0
  76. data/spec/misc_spec.rb +21 -21
  77. data/spec/spec_helper.rb +5 -10
  78. data/spec/support/active_record_data/5_1_diana.txt +104 -0
  79. data/spec/support/active_record_data/5_1_multi.txt +210 -0
  80. data/spec/support/active_record_data/5_2_diana.txt +104 -0
  81. data/spec/support/active_record_data/5_2_multi.txt +210 -0
  82. data/spec/support/ext_verifier.rb +0 -15
  83. data/spec/support/mongoid_versions.rb +4 -4
  84. data/spec/support/rails_versions.rb +10 -20
  85. metadata +98 -87
  86. data/lib/awesome_print/core_ext/method.rb +0 -21
  87. data/lib/awesome_print/ext/action_view.rb +0 -22
  88. data/lib/awesome_print/ext/active_record.rb +0 -78
  89. data/lib/awesome_print/ext/active_support.rb +0 -47
  90. data/lib/awesome_print/ext/mongo_mapper.rb +0 -124
  91. data/lib/awesome_print/ext/mongoid.rb +0 -67
  92. data/lib/awesome_print/ext/nobrainer.rb +0 -52
  93. data/lib/awesome_print/ext/nokogiri.rb +0 -45
  94. data/lib/awesome_print/ext/ostruct.rb +0 -27
  95. data/lib/awesome_print/ext/ripple.rb +0 -71
  96. data/lib/awesome_print/ext/sequel.rb +0 -58
  97. data/spec/ext/action_view_spec.rb +0 -18
  98. data/spec/ext/mongo_mapper_spec.rb +0 -261
  99. data/spec/ext/nobrainer_spec.rb +0 -59
  100. data/spec/ext/ripple_spec.rb +0 -48
  101. data/spec/formats_spec.rb +0 -779
  102. data/spec/support/active_record_data/3_2_diana.txt +0 -24
  103. data/spec/support/active_record_data/3_2_diana_legacy.txt +0 -24
  104. data/spec/support/active_record_data/3_2_multi.txt +0 -50
  105. data/spec/support/active_record_data/3_2_multi_legacy.txt +0 -50
  106. data/spec/support/active_record_data/4_0_diana.txt +0 -98
  107. data/spec/support/active_record_data/4_0_multi.txt +0 -198
  108. data/spec/support/active_record_data/4_1_diana.txt +0 -97
  109. data/spec/support/active_record_data/4_1_multi.txt +0 -196
  110. data/spec/support/active_record_data/4_2_diana.txt +0 -109
  111. data/spec/support/active_record_data/4_2_diana_legacy.txt +0 -109
  112. data/spec/support/active_record_data/4_2_multi.txt +0 -220
  113. data/spec/support/active_record_data/4_2_multi_legacy.txt +0 -220
@@ -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)
@@ -1,45 +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 Nokogiri
8
-
9
- def self.included(base)
10
- base.send :alias_method, :cast_without_nokogiri, :cast
11
- base.send :alias_method, :cast, :cast_with_nokogiri
12
- end
13
-
14
- # Add Nokogiri XML Node and NodeSet names to the dispatcher pipeline.
15
- #------------------------------------------------------------------------------
16
- def cast_with_nokogiri(object, type)
17
- cast = cast_without_nokogiri(object, type)
18
- if (defined?(::Nokogiri::XML::Node) && object.is_a?(::Nokogiri::XML::Node)) ||
19
- (defined?(::Nokogiri::XML::NodeSet) && object.is_a?(::Nokogiri::XML::NodeSet))
20
- cast = :nokogiri_xml_node
21
- end
22
- cast
23
- end
24
-
25
- #------------------------------------------------------------------------------
26
- def awesome_nokogiri_xml_node(object)
27
- if object.is_a?(::Nokogiri::XML::NodeSet) && object.empty?
28
- return '[]'
29
- end
30
- xml = object.to_xml(indent: 2)
31
- #
32
- # Colorize tag, id/class name, and contents.
33
- #
34
- xml.gsub!(/(<)(\/?[A-Za-z1-9]+)/) { |tag| "#{$1}#{colorize($2, :keyword)}" }
35
- xml.gsub!(/(id|class)="[^"]+"/i) { |id| colorize(id, :class) }
36
- xml.gsub!(/>([^<]+)</) do |contents|
37
- contents = colorize($1, :trueclass) if contents && !contents.empty?
38
- ">#{contents}<"
39
- end
40
- xml
41
- end
42
- end
43
- end
44
-
45
- AwesomePrint::Formatter.send(:include, AwesomePrint::Nokogiri)
@@ -1,27 +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 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
- AwesomePrint::Formatter.send(:include, AwesomePrint::OpenStruct)
@@ -1,71 +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 Ripple
8
-
9
- def self.included(base)
10
- base.send :alias_method, :cast_without_ripple, :cast
11
- base.send :alias_method, :cast, :cast_with_ripple
12
- end
13
-
14
- # Add Ripple class names to the dispatcher pipeline.
15
- #------------------------------------------------------------------------------
16
- def cast_with_ripple(object, type)
17
- cast = cast_without_ripple(object, type)
18
- return cast if !defined?(::Ripple)
19
-
20
- if object.is_a?(::Ripple::AttributeMethods) # Module used to access attributes across documents and embedded documents
21
- cast = :ripple_document_instance
22
- elsif object.is_a?(::Ripple::Properties) # Used to access property metadata on Ripple classes
23
- cast = :ripple_document_class
24
- end
25
- cast
26
- end
27
-
28
- private
29
-
30
- # Format Ripple instance object.
31
- #
32
- # NOTE: by default only instance attributes are shown. To format a Ripple document instance
33
- # as a regular object showing its instance variables and accessors use :raw => true option:
34
- #
35
- # ap document, :raw => true
36
- #
37
- #------------------------------------------------------------------------------
38
- def awesome_ripple_document_instance(object)
39
- return object.inspect if !defined?(::ActiveSupport::OrderedHash)
40
- return awesome_object(object) if @options[:raw]
41
- exclude_assoc = @options[:exclude_assoc] or @options[:exclude_associations]
42
-
43
- data = object.attributes.inject(::ActiveSupport::OrderedHash.new) do |hash, (name, value)|
44
- hash[name.to_sym] = object.send(name)
45
- hash
46
- end
47
-
48
- unless exclude_assoc
49
- data = object.class.embedded_associations.inject(data) do |hash, assoc|
50
- hash[assoc.name] = object.get_proxy(assoc) # Should always be array or Ripple::EmbeddedDocument for embedded associations
51
- hash
52
- end
53
- end
54
-
55
- "#{object} " << awesome_hash(data)
56
- end
57
-
58
- # Format Ripple class object.
59
- #------------------------------------------------------------------------------
60
- def awesome_ripple_document_class(object)
61
- return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:properties)
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
- AwesomePrint::Formatter.send(:include, AwesomePrint::Ripple)
@@ -1,58 +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 Sequel
8
-
9
- def self.included(base)
10
- base.send :alias_method, :cast_without_sequel, :cast
11
- base.send :alias_method, :cast, :cast_with_sequel
12
- end
13
-
14
- # Add Sequel class names to the dispatcher pipeline.
15
- #------------------------------------------------------------------------------
16
- def cast_with_sequel(object, type)
17
- cast = cast_without_sequel(object, type)
18
- if defined?(::Sequel::Model) && object.is_a?(::Sequel::Model)
19
- cast = :sequel_document
20
- elsif defined?(::Sequel::Model) && object.is_a?(Class) && object.ancestors.include?(::Sequel::Model)
21
- cast = :sequel_model_class
22
- elsif defined?(::Sequel::Mysql2::Dataset) && object.class.ancestors.include?(::Sequel::Mysql2::Dataset)
23
- cast = :sequel_dataset
24
- end
25
- cast
26
- end
27
-
28
- # Format Sequel Document object.
29
- #------------------------------------------------------------------------------
30
- def awesome_sequel_document(object)
31
- data = object.values.sort_by { |key| key.to_s }.inject({}) do |hash, c|
32
- hash[c[0].to_sym] = c[1]
33
- hash
34
- end
35
- data = { errors: object.errors, values: data } if !object.errors.empty?
36
- "#{object} #{awesome_hash(data)}"
37
- end
38
-
39
- # Format Sequel Dataset object.
40
- #------------------------------------------------------------------------------
41
- def awesome_sequel_dataset(dataset)
42
- [awesome_array(dataset.to_a), awesome_print(dataset.sql)].join("\n")
43
- end
44
-
45
- # Format Sequel Model class.
46
- #------------------------------------------------------------------------------
47
- def awesome_sequel_model_class(object)
48
- data = object.db_schema.inject({}) { |h, (prop, defn)| h.merge(prop => defn[:db_type]) }
49
- name = "class #{awesome_simple(object.to_s, :class)}"
50
- base = "< #{awesome_simple(object.superclass.to_s, :class)}"
51
-
52
- [name, base, awesome_hash(data)].join(' ')
53
- end
54
- end
55
-
56
- end
57
-
58
- AwesomePrint::Formatter.send(:include, AwesomePrint::Sequel)