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

mongoid instance printing and BSON::ObjectId

This commit is contained in:
amd 2011-12-12 21:41:31 -08:00
parent 71044fdd20
commit e8e02b6e09
2 changed files with 44 additions and 43 deletions

View file

@ -15,8 +15,14 @@ module AwesomePrint
#------------------------------------------------------------------------------
def cast_with_mongoid(object, type)
cast = cast_without_mongoid(object, type)
if defined?(::Mongoid::Document) && object.is_a?(Class) && object.ancestors.include?(::Mongoid::Document)
cast = :mongoid_class
if defined?(::Mongoid::Document)
if object.is_a?(Class) && object.ancestors.include?(::Mongoid::Document)
cast = :mongoid_class
elsif object.class.ancestors.include?(::Mongoid::Document)
cast = :mongoid_document
elsif object.is_a?(::BSON::ObjectId)
cast = :mongoid_bson_id
end
end
cast
end
@ -26,13 +32,33 @@ module AwesomePrint
def awesome_mongoid_class(object)
return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields)
data = object.fields.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
hash[c[1].name] = (c[1].type || "undefined").to_s.underscore.intern
# hash[c[1].name] = (c[1].type || "undefined").to_s.underscore.intern rescue c[1].type
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.intern
hash
end
"class #{object} < #{object.superclass} " << awesome_hash(data)
end
# Format Mongoid Document object.
#------------------------------------------------------------------------------
def awesome_mongoid_document(object)
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
data = object.attributes.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
hash[c[0].to_sym] = c[1]
hash
end
if !object.errors.empty?
data = {:errors => object.errors, :attributes => data}
end
"#{object} #{awesome_hash(data)}"
end
# Format BSON::ObjectId
#------------------------------------------------------------------------------
def awesome_mongoid_bson_id(object)
object.inspect
end
end
end

View file

@ -23,49 +23,24 @@ begin
out = @ap.send :awesome, user
str = <<-EOS.strip
#<MongoUser:0x01234567
attr_accessor :attributes = {
"_id" => #<BSON::ObjectId:0x01234567
attr_accessor :data = [
[ 0] 42,
[ 1] 42,
[ 2] 42,
[ 3] 42,
[ 4] 42,
[ 5] 42,
[ 6] 42,
[ 7] 42,
[ 8] 42,
[ 9] 42,
[10] 42,
[11] 42
]
>,
"first_name" => "Al",
"last_name" => "Capone"
},
attr_accessor :new_record = true,
attr_reader :changed_attributes = {
"_id" => nil,
"first_name" => nil,
"last_name" => nil
},
attr_reader :pending_nested = {},
attr_reader :pending_relations = {}
>
#<MongoUser:0x01234567> {
:_id => BSON::ObjectId('424242424242424242424242'),
:first_name => "Al",
:last_name => "Capone"
}
EOS
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
out.gsub!(/(\[\s?\d+\])\s\d+/, "\\1 42")
out.gsub!(/Id\('[^']+/, "Id('424242424242424242424242")
out.should == str
end
it "should print the class" do
@ap.send(:awesome, MongoUser).should == <<-EOS.strip
class MongoUser < Object {
"_id" => :"bson/object_id",
"_type" => :string,
"first_name" => :string,
"last_name" => :string
:_id => :"bson/object_id",
:_type => :string,
:first_name => :string,
:last_name => :string
}
EOS
end
@ -78,9 +53,9 @@ EOS
@ap.send(:awesome, Chamelion).should == <<-EOS.strip
class Chamelion < Object {
"_id" => :"bson/object_id",
"_type" => :string,
"last_attribute" => :"mongoid/fields/serializable/object"
:_id => :"bson/object_id",
:_type => :string,
:last_attribute => :"mongoid/fields/serializable/object"
}
EOS
end