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:
parent
17f6113aac
commit
5d76ded7ed
5 changed files with 30 additions and 62 deletions
|
@ -13,30 +13,14 @@ module AwesomePrint
|
||||||
|
|
||||||
def cast_with_active_record(object, type)
|
def cast_with_active_record(object, type)
|
||||||
cast = cast_without_active_record(object, type)
|
cast = cast_without_active_record(object, type)
|
||||||
if defined?(::ActiveRecord)
|
if defined?(::ActiveRecord) && object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base)
|
||||||
if object.is_a?(::ActiveRecord::Base)
|
cast = :active_record_class
|
||||||
cast = :active_record_instance
|
|
||||||
elsif object.is_a?(Class) and object.ancestors.include?(::ActiveRecord::Base)
|
|
||||||
cast = :active_record_class
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
cast
|
cast
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
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.
|
# Format ActiveRecord class object.
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
def awesome_active_record_class(object)
|
def awesome_active_record_class(object)
|
||||||
|
|
|
@ -3,52 +3,36 @@
|
||||||
# Awesome Print is freely distributable under the terms of MIT license.
|
# Awesome Print is freely distributable under the terms of MIT license.
|
||||||
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
module AwesomePrintMongoMapper
|
module AwesomePrint
|
||||||
|
module MongoMapper
|
||||||
|
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
base.send :alias_method, :printable_without_mongo_mapper, :printable
|
base.send :alias_method, :cast_without_mongo_mapper, :cast
|
||||||
base.send :alias_method, :printable, :printable_with_mongo_mapper
|
base.send :alias_method, :cast, :cast_with_mongo_mapper
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add MongoMapper class names to the dispatcher pipeline.
|
# Add MongoMapper class names to the dispatcher pipeline.
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
def printable_with_mongo_mapper(object)
|
def cast_with_mongo_mapper(object, type)
|
||||||
printable = printable_without_mongo_mapper(object)
|
cast = cast_without_mongo_mapper(object, type)
|
||||||
return printable if !defined?(MongoMapper::Document)
|
if defined?(MongoMapper::Document) && object.is_a?(Class) && (object.ancestors & [ MongoMapper::Document, MongoMapper::EmbeddedDocument ]).size > 0
|
||||||
|
cast = :mongo_mapper_class
|
||||||
if printable == :self
|
|
||||||
if object.is_a?(MongoMapper::Document) || object.is_a?(MongoMapper::EmbeddedDocument)
|
|
||||||
printable = :mongo_mapper_instance
|
|
||||||
end
|
end
|
||||||
elsif printable == :class && (object.ancestors & [MongoMapper::Document, MongoMapper::EmbeddedDocument]).size > 0
|
cast
|
||||||
printable = :mongo_mapper_class
|
|
||||||
end
|
end
|
||||||
printable
|
|
||||||
end
|
|
||||||
|
|
||||||
# Format MongoMapper instance object.
|
# Format MongoMapper class object.
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
def awesome_mongo_mapper_instance(object)
|
def awesome_mongo_mapper_class(object)
|
||||||
return object.inspect if !defined?(ActiveSupport::OrderedHash)
|
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|
|
data = object.keys.sort_by{|k| k}.inject(ActiveSupport::OrderedHash.new) do |hash, c|
|
||||||
hash[name] = object[name]
|
hash[c.first] = (c.last.type || "undefined").to_s.underscore.intern
|
||||||
hash
|
hash
|
||||||
|
end
|
||||||
|
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
AwesomePrint.send(:include, AwesomePrintMongoMapper)
|
AwesomePrint::Formatter.send(:include, AwesomePrint::MongoMapper)
|
||||||
|
|
|
@ -43,7 +43,7 @@ begin
|
||||||
ActiveRecord::Base.default_timezone = :utc
|
ActiveRecord::Base.default_timezone = :utc
|
||||||
@diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00")
|
@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")
|
@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
|
end
|
||||||
|
|
||||||
it "display single record" do
|
it "display single record" do
|
||||||
|
@ -101,7 +101,7 @@ EOS
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
describe "ActiveRecord class" do
|
describe "ActiveRecord class" do
|
||||||
it "should print the 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
|
@ap.send(:awesome, User).should == <<-EOS.strip
|
||||||
class User < ActiveRecord::Base {
|
class User < ActiveRecord::Base {
|
||||||
:id => :integer,
|
:id => :integer,
|
||||||
|
@ -114,7 +114,7 @@ EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should print the class for non-direct subclasses of AR::Base" do
|
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
|
@ap.send(:awesome, SubUser).should == <<-EOS.strip
|
||||||
class SubUser < User {
|
class SubUser < User {
|
||||||
:id => :integer,
|
:id => :integer,
|
||||||
|
|
|
@ -7,7 +7,7 @@ begin
|
||||||
describe "AwesomePrint::ActiveSupport" do
|
describe "AwesomePrint::ActiveSupport" do
|
||||||
before do
|
before do
|
||||||
stub_dotfile!
|
stub_dotfile!
|
||||||
@ap = AwesomePrint::Inspector.new()
|
@ap = AwesomePrint::Inspector.new
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should format ActiveSupport::TimeWithZone as regular Time" do
|
it "should format ActiveSupport::TimeWithZone as regular Time" do
|
||||||
|
|
|
@ -15,7 +15,7 @@ begin
|
||||||
end
|
end
|
||||||
|
|
||||||
before :each do
|
before :each do
|
||||||
@ap = AwesomePrint.new(:plain => true)
|
@ap = AwesomePrint::Inspector.new(:plain => true)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should print for a class instance" do
|
it "should print for a class instance" do
|
||||||
|
|
Loading…
Reference in a new issue