Created
May 18, 2011 09:35
-
-
Save yohfee/978285 to your computer and use it in GitHub Desktop.
Draft of awesome_print/lib/ap/mixin/mongoid.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module AwesomePrintMongoid | |
| def self.included(base) | |
| base.send :alias_method, :printable_without_mongoid, :printable | |
| base.send :alias_method, :printable, :printable_with_mongoid | |
| end | |
| # Add Mongoid class names to the dispatcher pipeline. | |
| #------------------------------------------------------------------------------ | |
| def printable_with_mongoid(object) | |
| printable = printable_without_mongoid(object) | |
| return printable if !defined?(Mongoid::Document) | |
| if printable == :self | |
| if object.is_a?(Mongoid::Document) | |
| printable = :mongoid_instance | |
| end | |
| elsif printable == :class && object.ancestors.include?(Mongoid::Document) | |
| printable = :mongoid_class | |
| end | |
| printable | |
| end | |
| # Format Mongoid instance object. | |
| #------------------------------------------------------------------------------ | |
| def awesome_mongoid_instance(object) | |
| return object.inspect if !defined?(ActiveSupport::OrderedHash) | |
| data = object.fields.keys.sort_by{|k| k}.inject(ActiveSupport::OrderedHash.new) do |hash, name| | |
| hash[name] = object[name] | |
| hash | |
| end | |
| "#{object} " + awesome_hash(data) | |
| end | |
| # Format Mongoid class object. | |
| #------------------------------------------------------------------------------ | |
| def awesome_mongoid_class(object) | |
| return object.inspect if !defined?(ActiveSupport::OrderedHash) | |
| data = object.fields.sort_by{|k| k}.inject(ActiveSupport::OrderedHash.new) do |hash, c| | |
| hash[c.first] = (c.last.type || "undefined").to_s.underscore.intern | |
| hash | |
| end | |
| "class #{object} < #{object.superclass} " << awesome_hash(data) | |
| end | |
| end | |
| AwesomePrint.send(:include, AwesomePrintMongoid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment