From 2779249a4bd0bb0436eab1034e559211cbd19b1c Mon Sep 17 00:00:00 2001 From: Nikolaj Nikolajsen Date: Wed, 8 Aug 2012 13:22:15 +0200 Subject: [PATCH] Adding support for Mongoid 3 and Moped --- lib/awesome_print/ext/mongoid.rb | 2 +- spec/ext/mongoid_spec.rb | 27 ++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/awesome_print/ext/mongoid.rb b/lib/awesome_print/ext/mongoid.rb index 0c289a2..701c05a 100644 --- a/lib/awesome_print/ext/mongoid.rb +++ b/lib/awesome_print/ext/mongoid.rb @@ -20,7 +20,7 @@ module AwesomePrint cast = :mongoid_class elsif object.class.ancestors.include?(::Mongoid::Document) cast = :mongoid_document - elsif object.is_a?(::BSON::ObjectId) + elsif defined?(::BSON) && object.is_a?(::BSON::ObjectId) || defined?(::Moped) && object.is_a?(::Moped::BSON::ObjectId) cast = :mongoid_bson_id end end diff --git a/spec/ext/mongoid_spec.rb b/spec/ext/mongoid_spec.rb index c37ee12..148496f 100644 --- a/spec/ext/mongoid_spec.rb +++ b/spec/ext/mongoid_spec.rb @@ -22,22 +22,37 @@ begin user = MongoUser.new :first_name => "Al", :last_name => "Capone" out = @ap.send :awesome, user - str = <<-EOS.strip + if defined?(::Moped) + str = <<-EOS.strip +# { + :_id => "424242424242424242424242", + :first_name => "Al", + :last_name => "Capone" +} +EOS + else + str = <<-EOS.strip # { :_id => BSON::ObjectId('424242424242424242424242'), :first_name => "Al", :last_name => "Capone" } EOS + end out.gsub!(/0x([a-f\d]+)/, "0x01234567") - out.gsub!(/Id\('[^']+/, "Id('424242424242424242424242") + if defined?(::Moped) + out.gsub!(/:_id => \"[^"]+/, ":_id => \"424242424242424242424242") + else + out.gsub!(/Id\('[^']+/, "Id('424242424242424242424242") + end out.should == str end it "should print the class" do + moped_or_not = defined?(::Moped) ? 'moped/' : '' @ap.send(:awesome, MongoUser).should == <<-EOS.strip class MongoUser < Object { - :_id => :"bson/object_id", + :_id => :"#{moped_or_not}bson/object_id", :_type => :string, :first_name => :string, :last_name => :string @@ -51,11 +66,13 @@ EOS field :last_attribute end + moped_or_not = defined?(::Moped) ? 'moped/' : '' + last_attribute = defined?(::BSON) ? '"mongoid/fields/serializable/object"' : 'object' @ap.send(:awesome, Chamelion).should == <<-EOS.strip class Chamelion < Object { - :_id => :"bson/object_id", + :_id => :"#{moped_or_not}bson/object_id", :_type => :string, - :last_attribute => :"mongoid/fields/serializable/object" + :last_attribute => :#{last_attribute} } EOS end