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

Manually merged expanded mongo_mapper support provided by gardelea

This commit is contained in:
Mike Dvorkin 2012-09-06 18:11:13 -07:00
parent da0defa87a
commit 5463de12e9
2 changed files with 216 additions and 18 deletions

View file

@ -14,10 +14,21 @@ module AwesomePrint
# Add MongoMapper class names to the dispatcher pipeline.
#------------------------------------------------------------------------------
def cast_with_mongo_mapper(object, type)
apply_default_mongo_mapper_options
cast = cast_without_mongo_mapper(object, type)
if defined?(::MongoMapper::Document) && object.is_a?(Class) && (object.ancestors & [ ::MongoMapper::Document, ::MongoMapper::EmbeddedDocument ]).size > 0
if defined?(::MongoMapper::Document)
if object.is_a?(Class) && (object.ancestors & [ ::MongoMapper::Document, ::MongoMapper::EmbeddedDocument ]).size > 0
cast = :mongo_mapper_class
elsif object.is_a?(::MongoMapper::Document) || object.is_a?(::MongoMapper::EmbeddedDocument)
cast = :mongo_mapper_instance
elsif object.is_a?(::MongoMapper::Plugins::Associations::Base)
cast = :mongo_mapper_association
elsif object.is_a?(::BSON::ObjectId)
cast = :mongo_mapper_bson_id
end
end
cast
end
@ -30,8 +41,80 @@ module AwesomePrint
hash[c.first] = (c.last.type || "undefined").to_s.underscore.intern
hash
end
# Add in associations
if @options[:mongo_mapper][:show_associations]
object.associations.each do |name, assoc|
data[name.to_s] = assoc
end
end
"class #{object} < #{object.superclass} " << awesome_hash(data)
end
# Format MongoMapper instance object.
#
# NOTE: by default only instance attributes (i.e. keys) are shown. To format
# MongoMapper instance as regular object showing its instance variables and
# accessors use :raw => true option:
#
# ap record, :raw => true
#
#------------------------------------------------------------------------------
def awesome_mongo_mapper_instance(object)
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
return awesome_object(object) if @options[:raw]
data = object.keys.keys.sort_by{|k| k}.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
hash[name] = object[name]
hash
end
# Add in associations
if @options[:mongo_mapper][:show_associations]
object.associations.each do |name, assoc|
if @options[:mongo_mapper][:inline_embedded] and assoc.embeddable?
data[name.to_s] = object.send(name)
else
data[name.to_s] = assoc
end
end
end
label = object.to_s
label = "#{colorize('embedded', :assoc)} #{label}" if object.is_a?(::MongoMapper::EmbeddedDocument)
"#{label} " << awesome_hash(data)
end
# Format MongoMapper association object.
#------------------------------------------------------------------------------
def awesome_mongo_mapper_association(object)
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
return awesome_object(object) if @options[:raw]
association = object.class.name.split('::').last.titleize.downcase.sub(/ association$/,'')
association = "embeds #{association}" if object.embeddable?
class_name = object.class_name
"#{colorize(association, :assoc)} #{colorize(class_name, :class)}"
end
# Format BSON::ObjectId
#------------------------------------------------------------------------------
def awesome_mongo_mapper_bson_id(object)
object.inspect
end
private
def apply_default_mongo_mapper_options
@options[:color][:assoc] ||= :greenish
@options[:mongo_mapper] ||= {
:show_associations => false, # Display association data for MongoMapper documents and classes.
:inline_embedded => false # Display embedded associations inline with MongoMapper documents.
}
end
end
end

View file

@ -24,6 +24,10 @@ begin
@ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true)
end
describe "with the raw option set to true" do
# before { @ap.options[:raw] = true }
before { @ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true, :raw => true) }
it "should print class instance" do
user = MongoUser.new(:first_name => "Al", :last_name => "Capone")
out = @ap.send(:awesome, user)
@ -69,6 +73,117 @@ EOS
end
end
describe "with associations" do
before :all do
class Child
include MongoMapper::EmbeddedDocument
key :data
end
class Sibling
include MongoMapper::Document
key :title
end
class Parent
include MongoMapper::Document
key :name
one :child
one :sibling
end
end
describe "with show associations turned off (default)" do
it "should render the class as normal" do
@ap.send(:awesome, Parent).should == <<-EOS.strip
class Parent < Object {
"_id" => :object_id,
"name" => :undefined
}
EOS
end
it "should render an instance as normal" do
parent = Parent.new(:name => 'test')
out = @ap.send(:awesome, parent)
str = <<-EOS.strip
#<Parent:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
"name" => "test"
}
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
out.should == str
end
end
describe "with show associations turned on and inline embedded turned off" do
before :each do
@ap = AwesomePrint::Inspector.new(:plain => true, :mongo_mapper => { :show_associations => true })
end
it "should render the class with associations shown" do
@ap.send(:awesome, Parent).should == <<-EOS.strip
class Parent < Object {
"_id" => :object_id,
"name" => :undefined,
"child" => embeds one Child,
"sibling" => one Sibling
}
EOS
end
it "should render an instance with associations shown" do
parent = Parent.new(:name => 'test')
out = @ap.send(:awesome, parent)
str = <<-EOS.strip
#<Parent:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
"name" => "test",
"child" => embeds one Child,
"sibling" => one Sibling
}
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
out.should == str
end
end
describe "with show associations turned on and inline embedded turned on" do
before :each do
@ap = AwesomePrint::Inspector.new(:plain => true,
:mongo_mapper => {
:show_associations => true,
:inline_embedded => true
}
)
end
it "should render an instance with associations shown and embeds there" do
parent = Parent.new(:name => 'test', :child => Child.new(:data => 5))
out = @ap.send(:awesome, parent)
str = <<-EOS.strip
#<Parent:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
"name" => "test",
"child" => embedded #<Child:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
"data" => 5
},
"sibling" => one Sibling
}
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
out.should == str
end
end
end
end
rescue LoadError => error
puts "Skipping MongoMapper specs: #{error}"
end