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

added basic sequel formatter

only does models so far
This commit is contained in:
Jonathan Davies 2012-10-12 11:39:30 +01:00
parent ee16be40bc
commit 3d82037325
2 changed files with 40 additions and 0 deletions

View file

@ -29,4 +29,5 @@ unless defined?(AwesomePrint::Inspector)
require File.dirname(__FILE__) + "/awesome_print/ext/mongo_mapper" if defined?(MongoMapper) require File.dirname(__FILE__) + "/awesome_print/ext/mongo_mapper" if defined?(MongoMapper)
require File.dirname(__FILE__) + "/awesome_print/ext/mongoid" if defined?(Mongoid) require File.dirname(__FILE__) + "/awesome_print/ext/mongoid" if defined?(Mongoid)
require File.dirname(__FILE__) + "/awesome_print/ext/nokogiri" if defined?(Nokogiri) require File.dirname(__FILE__) + "/awesome_print/ext/nokogiri" if defined?(Nokogiri)
require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel)
end end

View file

@ -0,0 +1,39 @@
# Copyright (c) 2010-2012 Michael Dvorkin
#
# Awesome Print is freely distributable under the terms of MIT license.
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
module AwesomePrint
module Sequel
def self.included(base)
base.send :alias_method, :cast_without_sequel, :cast
base.send :alias_method, :cast, :cast_with_sequel
end
# Add Sequel class names to the dispatcher pipeline.
#------------------------------------------------------------------------------
def cast_with_sequel(object, type)
cast = cast_without_sequel(object, type)
if defined?(::Sequel::Model) && object.class.ancestors.include?(::Sequel::Model)
cast = :sequel_document
end
cast
end
# Format Sequel Document object.
#------------------------------------------------------------------------------
def awesome_sequel_document(object)
data = object.values.sort_by { |key| key }.inject({}) do |hash, c|
hash[c[0].to_sym] = c[1]
hash
end
if !object.errors.empty?
data = {:errors => object.errors, :values => data}
end
"#{object} #{awesome_hash(data)}"
end
end
end
AwesomePrint::Formatter.send(:include, AwesomePrint::Sequel)