awesome_print 1.0.2 → 1.7.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.
- checksums.yaml +7 -0
- data/.gitignore +11 -1
- data/Appraisals +52 -0
- data/CHANGELOG.md +152 -0
- data/CONTRIBUTING.md +81 -0
- data/Gemfile +3 -2
- data/Gemfile.lock +44 -13
- data/LICENSE +3 -3
- data/README.md +272 -264
- data/Rakefile +23 -1
- data/lib/ap.rb +1 -1
- data/lib/awesome_print/colorize.rb +24 -0
- data/lib/awesome_print/core_ext/array.rb +12 -2
- data/lib/awesome_print/core_ext/class.rb +1 -1
- data/lib/awesome_print/core_ext/kernel.rb +8 -3
- data/lib/awesome_print/core_ext/logger.rb +1 -1
- data/lib/awesome_print/core_ext/method.rb +1 -1
- data/lib/awesome_print/core_ext/object.rb +1 -1
- data/lib/awesome_print/core_ext/string.rb +1 -1
- data/lib/awesome_print/ext/action_view.rb +1 -1
- data/lib/awesome_print/ext/active_record.rb +8 -2
- data/lib/awesome_print/ext/active_support.rb +2 -2
- data/lib/awesome_print/ext/mongo_mapper.rb +86 -3
- data/lib/awesome_print/ext/mongoid.rb +3 -3
- data/lib/awesome_print/ext/nobrainer.rb +49 -0
- data/lib/awesome_print/ext/nokogiri.rb +1 -1
- data/lib/awesome_print/ext/ostruct.rb +27 -0
- data/lib/awesome_print/ext/ripple.rb +72 -0
- data/lib/awesome_print/ext/sequel.rb +57 -0
- data/lib/awesome_print/formatter.rb +59 -309
- data/lib/awesome_print/formatters/array_formatter.rb +73 -0
- data/lib/awesome_print/formatters/base_formatter.rb +138 -0
- data/lib/awesome_print/formatters/class_formatter.rb +24 -0
- data/lib/awesome_print/formatters/dir_formatter.rb +22 -0
- data/lib/awesome_print/formatters/file_formatter.rb +22 -0
- data/lib/awesome_print/formatters/hash_formatter.rb +54 -0
- data/lib/awesome_print/formatters/method_formatter.rb +22 -0
- data/lib/awesome_print/formatters/object_formatter.rb +80 -0
- data/lib/awesome_print/formatters/simple_formatter.rb +21 -0
- data/lib/awesome_print/indentator.rb +18 -0
- data/lib/awesome_print/inspector.rb +48 -6
- data/lib/awesome_print/version.rb +2 -2
- data/lib/awesome_print.rb +20 -10
- data/spec/active_record_helper.rb +24 -0
- data/spec/colors_spec.rb +10 -10
- data/spec/formats_spec.rb +191 -170
- data/spec/methods_spec.rb +69 -68
- data/spec/misc_spec.rb +250 -0
- data/spec/objects_spec.rb +62 -12
- data/spec/spec_helper.rb +66 -34
- metadata +125 -49
- data/CHANGELOG +0 -77
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010-
|
|
1
|
+
# Copyright (c) 2010-2013 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
|
|
@@ -7,13 +7,18 @@ module Kernel
|
|
|
7
7
|
|
|
8
8
|
def ai(options = {})
|
|
9
9
|
ap = AwesomePrint::Inspector.new(options)
|
|
10
|
-
ap.awesome self
|
|
10
|
+
awesome = ap.awesome self
|
|
11
|
+
if options[:html]
|
|
12
|
+
awesome = "<pre>#{awesome}</pre>"
|
|
13
|
+
awesome = awesome.html_safe if defined? ActiveSupport
|
|
14
|
+
end
|
|
15
|
+
awesome
|
|
11
16
|
end
|
|
12
17
|
alias :awesome_inspect :ai
|
|
13
18
|
|
|
14
19
|
def ap(object, options = {})
|
|
15
20
|
puts object.ai(options)
|
|
16
|
-
object
|
|
21
|
+
object unless AwesomePrint.console?
|
|
17
22
|
end
|
|
18
23
|
alias :awesome_print :ap
|
|
19
24
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010-
|
|
1
|
+
# Copyright (c) 2010-2013 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
|
|
@@ -21,6 +21,8 @@ module AwesomePrint
|
|
|
21
21
|
cast = :active_record_instance
|
|
22
22
|
elsif object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base)
|
|
23
23
|
cast = :active_record_class
|
|
24
|
+
elsif type == :activerecord_relation || object.class.ancestors.include?(::ActiveRecord::Relation)
|
|
25
|
+
cast = :array
|
|
24
26
|
end
|
|
25
27
|
cast
|
|
26
28
|
end
|
|
@@ -41,7 +43,10 @@ module AwesomePrint
|
|
|
41
43
|
return awesome_object(object) if @options[:raw]
|
|
42
44
|
|
|
43
45
|
data = object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
|
|
44
|
-
|
|
46
|
+
if object.has_attribute?(name) || object.new_record?
|
|
47
|
+
value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name)
|
|
48
|
+
hash[name.to_sym] = value
|
|
49
|
+
end
|
|
45
50
|
hash
|
|
46
51
|
end
|
|
47
52
|
"#{object} " << awesome_hash(data)
|
|
@@ -51,6 +56,7 @@ module AwesomePrint
|
|
|
51
56
|
#------------------------------------------------------------------------------
|
|
52
57
|
def awesome_active_record_class(object)
|
|
53
58
|
return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:columns) || object.to_s == "ActiveRecord::Base"
|
|
59
|
+
return awesome_class(object) if object.respond_to?(:abstract_class?) && object.abstract_class?
|
|
54
60
|
|
|
55
61
|
data = object.columns.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
|
|
56
62
|
hash[c.name.to_sym] = c.type
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010-
|
|
1
|
+
# Copyright (c) 2010-2013 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) || object.is_a?(::Date)
|
|
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
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Copyright (c) 2010-
|
|
1
|
+
# Copyright (c) 2010-2013 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-2013 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
|
|
@@ -20,7 +20,7 @@ module AwesomePrint
|
|
|
20
20
|
cast = :mongoid_class
|
|
21
21
|
elsif object.class.ancestors.include?(::Mongoid::Document)
|
|
22
22
|
cast = :mongoid_document
|
|
23
|
-
elsif object.is_a?(::BSON::ObjectId)
|
|
23
|
+
elsif (defined?(::BSON) && object.is_a?(::BSON::ObjectId)) || (defined?(::Moped::BSON) && object.is_a?(::Moped::BSON::ObjectId))
|
|
24
24
|
cast = :mongoid_bson_id
|
|
25
25
|
end
|
|
26
26
|
end
|
|
@@ -44,7 +44,7 @@ module AwesomePrint
|
|
|
44
44
|
def awesome_mongoid_document(object)
|
|
45
45
|
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
|
|
46
46
|
|
|
47
|
-
data = object.attributes.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
|
|
47
|
+
data = (object.attributes || {}).sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
|
|
48
48
|
hash[c[0].to_sym] = c[1]
|
|
49
49
|
hash
|
|
50
50
|
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Copyright (c) 2010-2013 Michael Dvorkin
|
|
2
|
+
#
|
|
3
|
+
# Awesome Print is freely distributable under the terms of MIT license.
|
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
|
5
|
+
#------------------------------------------------------------------------------
|
|
6
|
+
module AwesomePrint
|
|
7
|
+
module 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
|
+
data = Hash[object.fields.map do |field, options|
|
|
32
|
+
[field, (options[:type] || Object).to_s.underscore.to_sym]
|
|
33
|
+
end]
|
|
34
|
+
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Format NoBrainer Document object.
|
|
38
|
+
#------------------------------------------------------------------------------
|
|
39
|
+
def awesome_nobrainer_document(object)
|
|
40
|
+
data = object.inspectable_attributes.symbolize_keys
|
|
41
|
+
if object.errors.present?
|
|
42
|
+
data = {:errors => object.errors, :attributes => data}
|
|
43
|
+
end
|
|
44
|
+
"#{object} #{awesome_hash(data)}"
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
AwesomePrint::Formatter.send(:include, AwesomePrint::NoBrainer)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Copyright (c) 2010-2013 Michael Dvorkin
|
|
2
|
+
#
|
|
3
|
+
# Awesome Print is freely distributable under the terms of MIT license.
|
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
|
5
|
+
#------------------------------------------------------------------------------
|
|
6
|
+
module AwesomePrint
|
|
7
|
+
module 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)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Copyright (c) 2010-2013 Michael Dvorkin
|
|
2
|
+
#
|
|
3
|
+
# Awesome Print is freely distributable under the terms of MIT license.
|
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
|
5
|
+
#------------------------------------------------------------------------------
|
|
6
|
+
module AwesomePrint
|
|
7
|
+
module 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
|
+
data = object.properties.inject(::ActiveSupport::OrderedHash.new) do |hash, (name, defn)|
|
|
64
|
+
hash[name.to_sym] = defn.type.to_s.downcase.to_sym
|
|
65
|
+
hash
|
|
66
|
+
end
|
|
67
|
+
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
AwesomePrint::Formatter.send(:include, AwesomePrint::Ripple)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Copyright (c) 2010-2013 Michael Dvorkin
|
|
2
|
+
#
|
|
3
|
+
# Awesome Print is freely distributable under the terms of MIT license.
|
|
4
|
+
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
|
5
|
+
#------------------------------------------------------------------------------
|
|
6
|
+
module AwesomePrint
|
|
7
|
+
module 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
|
+
if !object.errors.empty?
|
|
36
|
+
data = {:errors => object.errors, :values => data}
|
|
37
|
+
end
|
|
38
|
+
"#{object} #{awesome_hash(data)}"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Format Sequel Dataset object.
|
|
42
|
+
#------------------------------------------------------------------------------
|
|
43
|
+
def awesome_sequel_dataset(dataset)
|
|
44
|
+
[awesome_array(dataset.to_a), awesome_print(dataset.sql)].join("\n")
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Format Sequel Model class.
|
|
48
|
+
#------------------------------------------------------------------------------
|
|
49
|
+
def awesome_sequel_model_class(object)
|
|
50
|
+
data = object.db_schema.inject({}) {|h, (name,data)| h.merge(name => data[:db_type])}
|
|
51
|
+
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
AwesomePrint::Formatter.send(:include, AwesomePrint::Sequel)
|