mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
fix printing for class, add tests
This commit is contained in:
parent
c5551ac9c7
commit
cf2839c0d7
2 changed files with 48 additions and 7 deletions
|
@ -15,15 +15,12 @@ module AwesomePrintMongoMapper
|
|||
def printable_with_mongo_mapper(object)
|
||||
printable = printable_without_mongo_mapper(object)
|
||||
return printable if !defined?(MongoMapper::Document)
|
||||
|
||||
is_mongo = object.class.include?(MongoMapper::Document) ||
|
||||
object.class.include?(MongoMapper::EmbeddedDocument)
|
||||
|
||||
if printable == :self
|
||||
if is_mongo
|
||||
if object.is_a?(MongoMapper::Document) || object.is_a?(MongoMapper::EmbeddedDocument)
|
||||
printable = :mongo_mapper_instance
|
||||
end
|
||||
elsif printable == :class and is_mongo
|
||||
elsif printable == :class && (object.ancestors & [MongoMapper::Document, MongoMapper::EmbeddedDocument]).size > 0
|
||||
printable = :mongo_mapper_class
|
||||
end
|
||||
printable
|
||||
|
@ -44,10 +41,10 @@ module AwesomePrintMongoMapper
|
|||
# Format MongoMapper class object.
|
||||
#------------------------------------------------------------------------------
|
||||
def awesome_mongo_mapper_class(object)
|
||||
return object.inspect if !defined?(ActiveSupport::OrderedHash) || !object.respond_to?(:columns)
|
||||
return object.inspect if !defined?(ActiveSupport::OrderedHash) || !object.respond_to?(:keys)
|
||||
|
||||
data = object.keys.inject(ActiveSupport::OrderedHash.new) do |hash, c|
|
||||
hash[c.first] = c.last.type.to_s
|
||||
hash[c.first] = c.last.type.to_s.underscore.intern
|
||||
hash
|
||||
end
|
||||
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
||||
|
|
44
spec/mongo_mapper_spec.rb
Normal file
44
spec/mongo_mapper_spec.rb
Normal file
|
@ -0,0 +1,44 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||
|
||||
require "mongo_mapper"
|
||||
require "ap/mixin/mongo_mapper"
|
||||
|
||||
if defined?(MongoMapper)
|
||||
class User
|
||||
include MongoMapper::Document
|
||||
|
||||
key :first_name, String
|
||||
key :last_name, String
|
||||
end
|
||||
|
||||
describe "AwesomePrint/MongoMapper" do
|
||||
before :each do
|
||||
@ap = AwesomePrint.new(:plain => true)
|
||||
end
|
||||
|
||||
it "should print for a class instance" do
|
||||
user = User.new(:first_name => "Al", :last_name => "Capone")
|
||||
out = @ap.send(:awesome, user)
|
||||
str = <<-EOS.strip
|
||||
#<User:0x01234567> {
|
||||
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
|
||||
"last_name" => "Capone",
|
||||
"first_name" => "Al"
|
||||
}
|
||||
EOS
|
||||
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
|
||||
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
|
||||
out.should == str
|
||||
end
|
||||
|
||||
it "should print for a class" do
|
||||
@ap.send(:awesome, User).should == <<-EOS.strip
|
||||
class User < Object {
|
||||
"_id" => :object_id,
|
||||
"last_name" => :string,
|
||||
"first_name" => :string
|
||||
}
|
||||
EOS
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue