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
def self.formattable?(object)
defined?(::ActiveRecord) && object.is_a?(ActiveRecord::AttributeSet)
defined?(::ActiveRecord) &&
defined?(::ActiveRecord::AttributeSet) &&
object.is_a?(ActiveRecord::AttributeSet)
end
def format(object)

View file

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

View file

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

View file

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

View file

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