From e410508b2a8b3f7b37330728572dd87927d757a3 Mon Sep 17 00:00:00 2001 From: Gudleik Rasch Date: Wed, 26 Oct 2011 23:47:15 +0200 Subject: [PATCH] mongoid adapter --- lib/ap.rb | 3 +- lib/awesome_print/ext/mongoid.rb | 34 ++++++++++++ spec/ext/mongoid_spec.rb | 92 ++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 lib/awesome_print/ext/mongoid.rb create mode 100644 spec/ext/mongoid_spec.rb diff --git a/lib/ap.rb b/lib/ap.rb index 5cebf35..7c689a2 100755 --- a/lib/ap.rb +++ b/lib/ap.rb @@ -17,9 +17,10 @@ unless defined?(AwesomePrint) require File.dirname(__FILE__) + "/awesome_print/core_ext/logger" if defined?(Logger) require File.dirname(__FILE__) + "/awesome_print/ext/action_view" if defined?(ActionView) - # Load the following under normal circumstatnces as well as in Rails + # Load the following under normal circumstances as well as in Rails # console when required from ~/.irbrc. require File.dirname(__FILE__) + "/awesome_print/ext/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV']) require File.dirname(__FILE__) + "/awesome_print/ext/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV']) require File.dirname(__FILE__) + "/awesome_print/ext/mongo_mapper" if defined?(MongoMapper) + require File.dirname(__FILE__) + "/awesome_print/ext/mongoid" if defined?(Mongoid) end diff --git a/lib/awesome_print/ext/mongoid.rb b/lib/awesome_print/ext/mongoid.rb new file mode 100644 index 0000000..a1404db --- /dev/null +++ b/lib/awesome_print/ext/mongoid.rb @@ -0,0 +1,34 @@ +module AwesomePrint + module Mongoid + + def self.included(base) + base.send :alias_method, :cast_without_mongoid, :cast + base.send :alias_method, :cast, :cast_with_mongoid + end + + # Add Mongoid class names to the dispatcher pipeline. + #------------------------------------------------------------------------------ + 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 + end + cast + end + + # Format Mongoid class object. + #------------------------------------------------------------------------------ + 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 + hash + end + + "class #{object} < #{object.superclass} " << awesome_hash(data) + end + end +end + +AwesomePrint::Formatter.send(:include, AwesomePrint::Mongoid) diff --git a/spec/ext/mongoid_spec.rb b/spec/ext/mongoid_spec.rb new file mode 100644 index 0000000..9c08fa6 --- /dev/null +++ b/spec/ext/mongoid_spec.rb @@ -0,0 +1,92 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +begin + require "mongoid" + require "awesome_print/ext/mongoid" + + describe "AwesomePrint/Mongoid" do + before :all do + class MongoUser + include Mongoid::Document + + field :first_name, :type => String + field :last_name, :type => String + end + end + + before do + @ap = AwesomePrint::Inspector.new :plain => true, :sort_keys => true + end + + it "should print class instance" do + user = MongoUser.new :first_name => "Al", :last_name => "Capone" + out = @ap.send :awesome, user + + str = <<-EOS.strip +# #, + "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 = {} +> +EOS + out.gsub!(/0x([a-f\d]+)/, "0x01234567") + out.gsub!(/(\[\s?\d+\])\s\d+/, "\\1 42") + out.should eq str + end + + it "should print the class" do + @ap.send(:awesome, MongoUser).should == <<-EOS.strip +class MongoUser < Object { + "_id" => BSON::ObjectId < Object, + "_type" => String < Object, + "first_name" => String < Object, + "last_name" => String < Object +} +EOS + end + + it "should print the class when type is undefined" do + class Chamelion + include Mongoid::Document + field :last_attribute + end + + @ap.send(:awesome, Chamelion).should == <<-EOS.strip +class Chamelion < Object { + "_id" => BSON::ObjectId < Object, + "_type" => String < Object, + "last_attribute" => Mongoid::Fields::Serializable::Object < Object +} +EOS + end + end + +rescue LoadError => error + puts "Missing mongoid gem, skipping mongoid specs" + puts "Skipping mongoid specs: #{error}" +end