1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Fix mongoid concerns

This commit is contained in:
James Cox 2019-01-23 14:58:51 -05:00
parent 52933a1c74
commit 1ff12067cb
5 changed files with 32 additions and 25 deletions

View file

@ -11,7 +11,9 @@ module AwesomePrint
end end
def self.formattable?(object) def self.formattable?(object)
defined?(::ActiveRecord) && object.is_a?(ActiveRecord::AttributeSet) defined?(::ActiveRecord) &&
defined?(::ActiveRecord::AttributeSet) &&
object.is_a?(ActiveRecord::AttributeSet)
end end
def format(object) def format(object)

View file

@ -8,7 +8,8 @@ module AwesomePrint
def self.formattable?(object) def self.formattable?(object)
defined?(::Mongoid::Document) && defined?(::Mongoid::Document) &&
object.class.ancestors.include?(::Mongoid::Document) (object.class.ancestors.include?(::Mongoid::Document) ||
(object.respond_to?(:ancestors) && object.ancestors.include?(::Mongoid::Document)))
end end
def format(object) def format(object)
@ -16,40 +17,42 @@ module AwesomePrint
return Formatters::SimpleFormatter.new(@inspector).format(object.inspect) return Formatters::SimpleFormatter.new(@inspector).format(object.inspect)
end end
if object.respond_to?(:fields) if object.is_a?(Class)
puts "[MGD] formatting #{object} as class..." if AwesomePrint.debug
# mongoid class # mongoid class
format_as_class(object) format_as_class(object)
else else
puts "[MGD] formatting #{object} as instance..." if AwesomePrint.debug
format_as_instance(object) format_as_instance(object)
end end
end end
end private
private def format_as_class(object)
data = object.fields.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
hash[c[1].name.to_sym] = (c[1].type || 'undefined').to_s.underscore.to_sym
hash
end
def format_as_class(object) [
data = object.fields.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c| "class #{colorize(object.to_s, :class)}",
hash[c[1].name.to_sym] = (c[1].type || 'undefined').to_s.underscore.to_sym "< #{colorize(object.superclass.to_s, :class)}",
hash Formatters::HashFormatter.new(@inspector).format(data)
].join(' ')
end end
[ def format_as_instance(object)
"class #{colorize(object.to_s, :class)}", data = (object.attributes || {}).sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
"< #{colorize(object.superclass.to_s, :class)}", hash[c[0].to_sym] = c[1]
Formatters::HashFormatter.new(@inspector).format(data) hash
].join(' ') end
end
def format_as_instance(object) data = { errors: object.errors, attributes: data } if !object.errors.empty?
data = (object.attributes || {}).sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
hash[c[0].to_sym] = c[1] "#{object} #{Formatters::HashFormatter.new(@inspector).format(data)}"
hash
end end
data = { errors: object.errors, attributes: data } if !object.errors.empty?
"#{object} #{Formatters::HashFormatter.new(@inspector).format(data)}"
end end
end end
end end

View file

@ -127,6 +127,8 @@ module AwesomePrint
# awesome_print-rails binding lib or something. # awesome_print-rails binding lib or something.
if defined?(::ActiveRecord) && object.respond_to?(:ancestors) && object.ancestors.to_a.include?(::ActiveRecord::Base) if defined?(::ActiveRecord) && object.respond_to?(:ancestors) && object.ancestors.to_a.include?(::ActiveRecord::Base)
:activerecord_base_class :activerecord_base_class
elsif defined?(::Mongoid) && object.respond_to?(:ancestors) && object.ancestors.to_a.include?(::Mongoid::Document)
:mongoid_document
else else
object.class.to_s.gsub(/:+/, '_').downcase.to_sym object.class.to_s.gsub(/:+/, '_').downcase.to_sym
end end

View file

@ -1,7 +1,7 @@
module AwesomePrint module AwesomePrint
def self.debug def self.debug
false false #true
end end
def self.version def self.version

View file

@ -40,7 +40,7 @@ RSpec.describe 'AwesomePrint/Mongoid', skip: -> { !ExtVerifier.has_mongoid? }.ca
it 'should print the class' do it 'should print the class' do
class_spec = <<-EOS.strip class_spec = <<-EOS.strip
class MongoUser < Object { class MongoUser < Object {
:_id => :"bson/object_id", :_id => :bson/object_id,
:first_name => :string, :first_name => :string,
:last_name => :string :last_name => :string
} }
@ -57,7 +57,7 @@ class MongoUser < Object {
class_spec = <<-EOS.strip class_spec = <<-EOS.strip
class Chamelion < Object { class Chamelion < Object {
:_id => :"bson/object_id", :_id => :bson/object_id,
:last_attribute => :object :last_attribute => :object
} }
EOS EOS