From dbd2bcd3606b5bbc715d98f5d11e974365ac3f49 Mon Sep 17 00:00:00 2001 From: Elpizo Choi Date: Mon, 28 Mar 2011 17:56:06 -0700 Subject: [PATCH] add support for mongo_mapper --- lib/ap/mixin/mongo_mapper.rb | 57 ++++++++++++++++++++++++++++++++++++ lib/awesome_print.rb | 1 + 2 files changed, 58 insertions(+) create mode 100644 lib/ap/mixin/mongo_mapper.rb diff --git a/lib/ap/mixin/mongo_mapper.rb b/lib/ap/mixin/mongo_mapper.rb new file mode 100644 index 0000000..3db0f22 --- /dev/null +++ b/lib/ap/mixin/mongo_mapper.rb @@ -0,0 +1,57 @@ +# Copyright (c) 2010-2011 Michael Dvorkin +# +# Awesome Print is freely distributable under the terms of MIT license. +# See LICENSE file or http://www.opensource.org/licenses/mit-license.php +#------------------------------------------------------------------------------ +module AwesomePrintMongoMapper + + def self.included(base) + base.send :alias_method, :printable_without_mongo_mapper, :printable + base.send :alias_method, :printable, :printable_with_mongo_mapper + end + + # Add MongoMapper class names to the dispatcher pipeline. + #------------------------------------------------------------------------------ + 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 + printable = :mongo_mapper_instance + end + elsif printable == :class and is_mongo + printable = :mongo_mapper_class + end + printable + end + + # Format MongoMapper instance object. + #------------------------------------------------------------------------------ + def awesome_mongo_mapper_instance(object) + return object.inspect if !defined?(ActiveSupport::OrderedHash) + + data = object.keys.keys.inject(ActiveSupport::OrderedHash.new) do |hash, name| + hash[name] = object[name] + hash + end + "#{object} " + awesome_hash(data) + end + + # Format MongoMapper class object. + #------------------------------------------------------------------------------ + def awesome_mongo_mapper_class(object) + return object.inspect if !defined?(ActiveSupport::OrderedHash) || !object.respond_to?(:columns) + + data = object.keys.inject(ActiveSupport::OrderedHash.new) do |hash, c| + hash[c.first] = c.last.class + hash + end + "class #{object} < #{object.superclass} " << awesome_hash(data) + end +end + +AwesomePrint.send(:include, AwesomePrintMongoMapper) diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index fc1cc15..268239a 100755 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -22,3 +22,4 @@ require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(ActionView) # console when required from ~/.irbrc. require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV']) require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV']) +require File.dirname(__FILE__) + "/ap/mixin/mongo_mapper" if defined?(MongoMapper) \ No newline at end of file