mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Refactored MongoMapper extension and its specs
This commit is contained in:
parent
5de4abf359
commit
b8822574ec
3 changed files with 54 additions and 16 deletions
|
@ -15,7 +15,7 @@ module AwesomePrint
|
|||
#------------------------------------------------------------------------------
|
||||
def cast_with_mongo_mapper(object, type)
|
||||
cast = cast_without_mongo_mapper(object, type)
|
||||
if defined?(MongoMapper::Document) && object.is_a?(Class) && (object.ancestors & [ MongoMapper::Document, MongoMapper::EmbeddedDocument ]).size > 0
|
||||
if defined?(::MongoMapper::Document) && object.is_a?(Class) && (object.ancestors & [ ::MongoMapper::Document, ::MongoMapper::EmbeddedDocument ]).size > 0
|
||||
cast = :mongo_mapper_class
|
||||
end
|
||||
cast
|
||||
|
@ -24,9 +24,9 @@ module AwesomePrint
|
|||
# Format MongoMapper class object.
|
||||
#------------------------------------------------------------------------------
|
||||
def awesome_mongo_mapper_class(object)
|
||||
return object.inspect if !defined?(ActiveSupport::OrderedHash) || !object.respond_to?(:keys)
|
||||
return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:keys)
|
||||
|
||||
data = object.keys.sort_by{|k| k}.inject(ActiveSupport::OrderedHash.new) do |hash, c|
|
||||
data = object.keys.sort.inject(::ActiveSupport::OrderedHash.new) do |hash, c|
|
||||
hash[c.first] = (c.last.type || "undefined").to_s.underscore.intern
|
||||
hash
|
||||
end
|
||||
|
|
|
@ -33,13 +33,13 @@ begin
|
|||
end
|
||||
|
||||
describe "AwesomePrint/ActiveRecord" do
|
||||
before(:each) do
|
||||
before do
|
||||
stub_dotfile!
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
describe "ActiveRecord instance" do
|
||||
before(:each) do
|
||||
before do
|
||||
ActiveRecord::Base.default_timezone = :utc
|
||||
@diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00")
|
||||
@laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00")
|
||||
|
|
|
@ -14,26 +14,64 @@ begin
|
|||
end
|
||||
end
|
||||
|
||||
before :each do
|
||||
@ap = AwesomePrint::Inspector.new(:plain => true)
|
||||
before do
|
||||
@ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true)
|
||||
end
|
||||
|
||||
it "should print for a class instance" do
|
||||
it "should print class instance" do
|
||||
user = MongoUser.new(:first_name => "Al", :last_name => "Capone")
|
||||
out = @ap.send(:awesome, user)
|
||||
str = <<-EOS.strip
|
||||
#<MongoUser:0x01234567> {
|
||||
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
|
||||
"first_name" => "Al",
|
||||
"last_name" => "Capone"
|
||||
}
|
||||
#<MongoUser:0x01234567
|
||||
@_new = true,
|
||||
attr_accessor :_id = #<BSON::ObjectId:0x01234567
|
||||
attr_accessor :data = [
|
||||
[ 0] 42,
|
||||
[ 1] 42,
|
||||
[ 2] 42,
|
||||
[ 3] 42,
|
||||
[ 4] 42,
|
||||
[ 5] 42,
|
||||
[ 6] 42,
|
||||
[ 7] 42,
|
||||
[ 8] 42,
|
||||
[ 9] 42,
|
||||
[10] 42,
|
||||
[11] 42
|
||||
]
|
||||
>,
|
||||
attr_accessor :first_name = "Al",
|
||||
attr_accessor :last_name = "Capone",
|
||||
attr_reader :_id_before_type_cast = #<BSON::ObjectId:0x01234567
|
||||
attr_accessor :data = [
|
||||
[ 0] 42,
|
||||
[ 1] 42,
|
||||
[ 2] 42,
|
||||
[ 3] 42,
|
||||
[ 4] 42,
|
||||
[ 5] 42,
|
||||
[ 6] 42,
|
||||
[ 7] 42,
|
||||
[ 8] 42,
|
||||
[ 9] 42,
|
||||
[10] 42,
|
||||
[11] 42
|
||||
]
|
||||
>,
|
||||
attr_reader :changed_attributes = {
|
||||
"first_name" => nil,
|
||||
"last_name" => nil
|
||||
},
|
||||
attr_reader :first_name_before_type_cast = "Al",
|
||||
attr_reader :last_name_before_type_cast = "Capone"
|
||||
>
|
||||
EOS
|
||||
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
|
||||
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
|
||||
out.gsub!(/(\[\s?\d+\])\s\d+/, "\\1 42")
|
||||
out.should == str
|
||||
end
|
||||
|
||||
it "should print for a class" do
|
||||
it "should print the class" do
|
||||
@ap.send(:awesome, MongoUser).should == <<-EOS.strip
|
||||
class MongoUser < Object {
|
||||
"_id" => :object_id,
|
||||
|
@ -43,7 +81,7 @@ class MongoUser < Object {
|
|||
EOS
|
||||
end
|
||||
|
||||
it "should print for a class when type is undefined" do
|
||||
it "should print the class when type is undefined" do
|
||||
class Chamelion
|
||||
include MongoMapper::Document
|
||||
key :last_attribute
|
||||
|
|
Loading…
Add table
Reference in a new issue