From 3d820373255665ed8c397154074f6f330de5052c Mon Sep 17 00:00:00 2001 From: Jonathan Davies Date: Fri, 12 Oct 2012 11:39:30 +0100 Subject: [PATCH 1/7] added basic sequel formatter only does models so far --- lib/awesome_print.rb | 1 + lib/awesome_print/ext/sequel.rb | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 lib/awesome_print/ext/sequel.rb diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index 0d04e3b..04ca88e 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/sequel" if defined?(Sequel) end diff --git a/lib/awesome_print/ext/sequel.rb b/lib/awesome_print/ext/sequel.rb new file mode 100644 index 0000000..7142703 --- /dev/null +++ b/lib/awesome_print/ext/sequel.rb @@ -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) From b76b11dea9cbc2cfcecd3397e1c6b5ca1c56a0f4 Mon Sep 17 00:00:00 2001 From: Jonathan Davies Date: Wed, 17 Oct 2012 11:17:00 +0100 Subject: [PATCH 2/7] added printing of sequel datasets --- lib/awesome_print/ext/sequel.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/awesome_print/ext/sequel.rb b/lib/awesome_print/ext/sequel.rb index 7142703..56cc609 100644 --- a/lib/awesome_print/ext/sequel.rb +++ b/lib/awesome_print/ext/sequel.rb @@ -17,6 +17,8 @@ module AwesomePrint cast = cast_without_sequel(object, type) if defined?(::Sequel::Model) && object.class.ancestors.include?(::Sequel::Model) cast = :sequel_document + elsif defined?(::Sequel::Mysql2::Dataset) && object.class.ancestors.include?(::Sequel::Mysql2::Dataset) + cast = :sequel_dataset end cast end @@ -33,7 +35,14 @@ module AwesomePrint end "#{object} #{awesome_hash(data)}" end + + # Format Sequel Dataset object. + #------------------------------------------------------------------------------ + def awesome_sequel_dataset(dataset) + awesome_array(dataset.to_a) + end end + end AwesomePrint::Formatter.send(:include, AwesomePrint::Sequel) From 521ac53368ccc38a7440022e43d4def4bda560f3 Mon Sep 17 00:00:00 2001 From: Jonathan Davies Date: Thu, 3 Jan 2013 17:10:20 +0000 Subject: [PATCH 3/7] cast all keys to strings to fix sorting of fields --- lib/awesome_print/ext/sequel.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/awesome_print/ext/sequel.rb b/lib/awesome_print/ext/sequel.rb index 56cc609..d0afd00 100644 --- a/lib/awesome_print/ext/sequel.rb +++ b/lib/awesome_print/ext/sequel.rb @@ -26,7 +26,7 @@ module AwesomePrint # Format Sequel Document object. #------------------------------------------------------------------------------ def awesome_sequel_document(object) - data = object.values.sort_by { |key| key }.inject({}) do |hash, c| + data = object.values.sort_by { |key| key.to_s }.inject({}) do |hash, c| hash[c[0].to_sym] = c[1] hash end From 09992b676dc5547992797959576dc12d81b887c7 Mon Sep 17 00:00:00 2001 From: Jonathan Davies Date: Tue, 8 Jan 2013 16:29:32 +0000 Subject: [PATCH 4/7] print sequel dataset sql --- lib/awesome_print/ext/sequel.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/awesome_print/ext/sequel.rb b/lib/awesome_print/ext/sequel.rb index d0afd00..f055aa5 100644 --- a/lib/awesome_print/ext/sequel.rb +++ b/lib/awesome_print/ext/sequel.rb @@ -39,7 +39,7 @@ module AwesomePrint # Format Sequel Dataset object. #------------------------------------------------------------------------------ def awesome_sequel_dataset(dataset) - awesome_array(dataset.to_a) + [awesome_array(dataset.to_a), awesome_print(dataset.sql)].join("\n") end end From 6fc92236eff04a3efca0747fcb45054e03a42d2c Mon Sep 17 00:00:00 2001 From: Jonathan Davies Date: Fri, 5 Jul 2013 11:36:52 +0100 Subject: [PATCH 5/7] add support for sequel model classes --- Gemfile.lock | 2 +- lib/awesome_print/ext/sequel.rb | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 8acbb80..64f5b43 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - awesome_print (1.0.2) + awesome_print (1.1.0) GEM remote: http://rubygems.org/ diff --git a/lib/awesome_print/ext/sequel.rb b/lib/awesome_print/ext/sequel.rb index f055aa5..bbd209d 100644 --- a/lib/awesome_print/ext/sequel.rb +++ b/lib/awesome_print/ext/sequel.rb @@ -15,8 +15,10 @@ module AwesomePrint #------------------------------------------------------------------------------ def cast_with_sequel(object, type) cast = cast_without_sequel(object, type) - if defined?(::Sequel::Model) && object.class.ancestors.include?(::Sequel::Model) + if defined?(::Sequel::Model) && object.is_a?(::Sequel::Model) cast = :sequel_document + elsif defined?(::Sequel::Model) && object.is_a?(Class) && object.ancestors.include?(::Sequel::Model) + cast = :sequel_model_class elsif defined?(::Sequel::Mysql2::Dataset) && object.class.ancestors.include?(::Sequel::Mysql2::Dataset) cast = :sequel_dataset end @@ -41,6 +43,12 @@ module AwesomePrint def awesome_sequel_dataset(dataset) [awesome_array(dataset.to_a), awesome_print(dataset.sql)].join("\n") end + + # Format Sequel Model class. + #------------------------------------------------------------------------------ + def awesome_sequel_model_class + "class #{object} < #{object.superclass} " << awesome_hash(object.db_schema) + end end end From d8d50857fcef847502674747a2a631f140f055c6 Mon Sep 17 00:00:00 2001 From: Jonathan Davies Date: Fri, 5 Jul 2013 11:40:52 +0100 Subject: [PATCH 6/7] fix --- lib/awesome_print/ext/sequel.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/awesome_print/ext/sequel.rb b/lib/awesome_print/ext/sequel.rb index bbd209d..8ee8b90 100644 --- a/lib/awesome_print/ext/sequel.rb +++ b/lib/awesome_print/ext/sequel.rb @@ -47,7 +47,8 @@ module AwesomePrint # Format Sequel Model class. #------------------------------------------------------------------------------ def awesome_sequel_model_class - "class #{object} < #{object.superclass} " << awesome_hash(object.db_schema) + data = object.db_schema.inject({}) {|h, (name,data)| h.merge(name => data[:db_type])} + "class #{object} < #{object.superclass} " << awesome_hash(data) end end From e27e0255099735b206789a21b3d076ec41c96463 Mon Sep 17 00:00:00 2001 From: Jonathan Davies Date: Fri, 5 Jul 2013 11:44:22 +0100 Subject: [PATCH 7/7] fix --- lib/awesome_print/ext/sequel.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/awesome_print/ext/sequel.rb b/lib/awesome_print/ext/sequel.rb index 8ee8b90..b402471 100644 --- a/lib/awesome_print/ext/sequel.rb +++ b/lib/awesome_print/ext/sequel.rb @@ -46,7 +46,7 @@ module AwesomePrint # Format Sequel Model class. #------------------------------------------------------------------------------ - def awesome_sequel_model_class + def awesome_sequel_model_class(object) data = object.db_schema.inject({}) {|h, (name,data)| h.merge(name => data[:db_type])} "class #{object} < #{object.superclass} " << awesome_hash(data) end