From e8e02b6e09dbb47e9f4bc5413ab05032ed2540ae Mon Sep 17 00:00:00 2001 From: amd Date: Mon, 12 Dec 2011 21:41:31 -0800 Subject: [PATCH] mongoid instance printing and BSON::ObjectId --- lib/awesome_print/ext/mongoid.rb | 36 ++++++++++++++++++---- spec/ext/mongoid_spec.rb | 51 ++++++++------------------------ 2 files changed, 44 insertions(+), 43 deletions(-) diff --git a/lib/awesome_print/ext/mongoid.rb b/lib/awesome_print/ext/mongoid.rb index 722d661..0c289a2 100644 --- a/lib/awesome_print/ext/mongoid.rb +++ b/lib/awesome_print/ext/mongoid.rb @@ -15,8 +15,14 @@ module AwesomePrint #------------------------------------------------------------------------------ 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 + if defined?(::Mongoid::Document) + if object.is_a?(Class) && object.ancestors.include?(::Mongoid::Document) + cast = :mongoid_class + elsif object.class.ancestors.include?(::Mongoid::Document) + cast = :mongoid_document + elsif object.is_a?(::BSON::ObjectId) + cast = :mongoid_bson_id + end end cast end @@ -26,13 +32,33 @@ module AwesomePrint 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 || "undefined").to_s.underscore.intern - # hash[c[1].name] = (c[1].type || "undefined").to_s.underscore.intern rescue c[1].type + data = object.fields.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c| + hash[c[1].name.to_sym] = (c[1].type || "undefined").to_s.underscore.intern hash end "class #{object} < #{object.superclass} " << awesome_hash(data) end + + # Format Mongoid Document object. + #------------------------------------------------------------------------------ + def awesome_mongoid_document(object) + return object.inspect if !defined?(::ActiveSupport::OrderedHash) + + data = object.attributes.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c| + hash[c[0].to_sym] = c[1] + hash + end + if !object.errors.empty? + data = {:errors => object.errors, :attributes => data} + end + "#{object} #{awesome_hash(data)}" + end + + # Format BSON::ObjectId + #------------------------------------------------------------------------------ + def awesome_mongoid_bson_id(object) + object.inspect + end end end diff --git a/spec/ext/mongoid_spec.rb b/spec/ext/mongoid_spec.rb index 3331dac..c37ee12 100644 --- a/spec/ext/mongoid_spec.rb +++ b/spec/ext/mongoid_spec.rb @@ -23,49 +23,24 @@ begin 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 = {} -> +# { + :_id => BSON::ObjectId('424242424242424242424242'), + :first_name => "Al", + :last_name => "Capone" +} EOS out.gsub!(/0x([a-f\d]+)/, "0x01234567") - out.gsub!(/(\[\s?\d+\])\s\d+/, "\\1 42") + out.gsub!(/Id\('[^']+/, "Id('424242424242424242424242") out.should == str end it "should print the class" do @ap.send(:awesome, MongoUser).should == <<-EOS.strip class MongoUser < Object { - "_id" => :"bson/object_id", - "_type" => :string, - "first_name" => :string, - "last_name" => :string + :_id => :"bson/object_id", + :_type => :string, + :first_name => :string, + :last_name => :string } EOS end @@ -78,9 +53,9 @@ EOS @ap.send(:awesome, Chamelion).should == <<-EOS.strip class Chamelion < Object { - "_id" => :"bson/object_id", - "_type" => :string, - "last_attribute" => :"mongoid/fields/serializable/object" + :_id => :"bson/object_id", + :_type => :string, + :last_attribute => :"mongoid/fields/serializable/object" } EOS end