From 037a1e938c300060badba59f8e6de8775c4b9808 Mon Sep 17 00:00:00 2001 From: Nicolas Viennot Date: Thu, 27 Dec 2012 23:41:19 -0500 Subject: [PATCH] Preliminary support for NoBrainer --- lib/awesome_print.rb | 1 + lib/awesome_print/ext/no_brainer.rb | 58 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 lib/awesome_print/ext/no_brainer.rb diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index 0d04e3b..c75ab20 100755 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -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/mongoid" if defined?(Mongoid) require File.dirname(__FILE__) + "/awesome_print/ext/nokogiri" if defined?(Nokogiri) + require File.dirname(__FILE__) + "/awesome_print/ext/no_brainer" if defined?(NoBrainer) end diff --git a/lib/awesome_print/ext/no_brainer.rb b/lib/awesome_print/ext/no_brainer.rb new file mode 100644 index 0000000..78408a7 --- /dev/null +++ b/lib/awesome_print/ext/no_brainer.rb @@ -0,0 +1,58 @@ +# 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 NoBrainer + + def self.included(base) + base.send :alias_method, :cast_without_no_brainer, :cast + base.send :alias_method, :cast, :cast_with_no_brainer + end + + # Add NoBrainer class names to the dispatcher pipeline. + #------------------------------------------------------------------------------ + def cast_with_no_brainer(object, type) + cast = cast_without_no_brainer(object, type) + if defined?(::NoBrainer::Document) + if object.is_a?(Class) && object.ancestors.include?(::NoBrainer::Document) + cast = :no_brainer_class + elsif object.class.ancestors.include?(::NoBrainer::Document) + cast = :no_brainer_document + end + end + cast + end + + # Format NoBrainer class object. + #------------------------------------------------------------------------------ + def awesome_no_brainer_class(object) + return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields) + + # We want id first + data = object.fields.sort_by { |key| key[0] == :id ? '_id' : key[0].to_s }.inject(::ActiveSupport::OrderedHash.new) do |hash, c| + hash[c[0]] = :object + hash + end + "class #{object} < #{object.superclass} " << awesome_hash(data) + end + + # Format NoBrainer Document object. + #------------------------------------------------------------------------------ + def awesome_no_brainer_document(object) + return object.inspect if !defined?(::ActiveSupport::OrderedHash) + + data = object.attributes.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c| + hash[c[0].to_sym] = c[1] + hash + end + if !object.errors.empty? + data = {:errors => object.errors, :attributes => data} + end + "#{object} #{awesome_hash(data)}" + end + end +end + +AwesomePrint::Formatter.send(:include, AwesomePrint::NoBrainer)