1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Format ActiveRecord and MongoDocument instances as any other arbitrary objects

This commit is contained in:
Mike Dvorkin 2011-05-13 17:36:12 -07:00
parent 17f6113aac
commit 5d76ded7ed
5 changed files with 30 additions and 62 deletions

View file

@ -13,30 +13,14 @@ module AwesomePrint
def cast_with_active_record(object, type)
cast = cast_without_active_record(object, type)
if defined?(::ActiveRecord)
if object.is_a?(::ActiveRecord::Base)
cast = :active_record_instance
elsif object.is_a?(Class) and object.ancestors.include?(::ActiveRecord::Base)
cast = :active_record_class
end
if defined?(::ActiveRecord) && object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base)
cast = :active_record_class
end
cast
end
private
# Format ActiveRecord instance object.
#------------------------------------------------------------------------------
def awesome_active_record_instance(object)
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
data = object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
hash[name.to_sym] = object.send(name) if object.has_attribute?(name) || object.new_record?
hash
end
"#{object} " + awesome_hash(data)
end
# Format ActiveRecord class object.
#------------------------------------------------------------------------------
def awesome_active_record_class(object)

View file

@ -3,52 +3,36 @@
# Awesome Print is freely distributable under the terms of MIT license.
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
module AwesomePrintMongoMapper
module AwesomePrint
module MongoMapper
def self.included(base)
base.send :alias_method, :printable_without_mongo_mapper, :printable
base.send :alias_method, :printable, :printable_with_mongo_mapper
end
def self.included(base)
base.send :alias_method, :cast_without_mongo_mapper, :cast
base.send :alias_method, :cast, :cast_with_mongo_mapper
end
# Add MongoMapper class names to the dispatcher pipeline.
#------------------------------------------------------------------------------
def printable_with_mongo_mapper(object)
printable = printable_without_mongo_mapper(object)
return printable if !defined?(MongoMapper::Document)
if printable == :self
if object.is_a?(MongoMapper::Document) || object.is_a?(MongoMapper::EmbeddedDocument)
printable = :mongo_mapper_instance
# Add MongoMapper class names to the dispatcher pipeline.
#------------------------------------------------------------------------------
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
cast = :mongo_mapper_class
end
elsif printable == :class && (object.ancestors & [MongoMapper::Document, MongoMapper::EmbeddedDocument]).size > 0
printable = :mongo_mapper_class
cast
end
printable
end
# Format MongoMapper instance object.
#------------------------------------------------------------------------------
def awesome_mongo_mapper_instance(object)
return object.inspect if !defined?(ActiveSupport::OrderedHash)
# Format MongoMapper class object.
#------------------------------------------------------------------------------
def awesome_mongo_mapper_class(object)
return object.inspect if !defined?(ActiveSupport::OrderedHash) || !object.respond_to?(:keys)
data = object.keys.keys.sort_by{|k| k}.inject(ActiveSupport::OrderedHash.new) do |hash, name|
hash[name] = object[name]
hash
data = object.keys.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
"#{object} " + awesome_hash(data)
end
# Format MongoMapper class object.
#------------------------------------------------------------------------------
def awesome_mongo_mapper_class(object)
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|
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, AwesomePrintMongoMapper)
AwesomePrint::Formatter.send(:include, AwesomePrint::MongoMapper)

View file

@ -43,7 +43,7 @@ begin
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")
@ap = AwesomePrint.new(:plain => true)
@ap = AwesomePrint::Inspector.new(:plain => true)
end
it "display single record" do
@ -101,7 +101,7 @@ EOS
#------------------------------------------------------------------------------
describe "ActiveRecord class" do
it "should print the class" do
@ap = AwesomePrint.new(:plain => true)
@ap = AwesomePrint::Inspector.new(:plain => true)
@ap.send(:awesome, User).should == <<-EOS.strip
class User < ActiveRecord::Base {
:id => :integer,
@ -114,7 +114,7 @@ EOS
end
it "should print the class for non-direct subclasses of AR::Base" do
@ap = AwesomePrint.new(:plain => true)
@ap = AwesomePrint::Inspector.new(:plain => true)
@ap.send(:awesome, SubUser).should == <<-EOS.strip
class SubUser < User {
:id => :integer,

View file

@ -7,7 +7,7 @@ begin
describe "AwesomePrint::ActiveSupport" do
before do
stub_dotfile!
@ap = AwesomePrint::Inspector.new()
@ap = AwesomePrint::Inspector.new
end
it "should format ActiveSupport::TimeWithZone as regular Time" do

View file

@ -15,7 +15,7 @@ begin
end
before :each do
@ap = AwesomePrint.new(:plain => true)
@ap = AwesomePrint::Inspector.new(:plain => true)
end
it "should print for a class instance" do