From 43f856ea2e5bdcc3e1abbb517c6f22c59d1e6105 Mon Sep 17 00:00:00 2001 From: Alexey Zapparov Date: Fri, 12 Feb 2016 00:50:31 +0100 Subject: [PATCH] Colorize ORM class names --- CHANGELOG.md | 4 +++- lib/awesome_print/ext/active_record.rb | 6 +++++- lib/awesome_print/ext/mongo_mapper.rb | 5 ++++- lib/awesome_print/ext/mongoid.rb | 6 +++++- lib/awesome_print/ext/nobrainer.rb | 7 ++++++- lib/awesome_print/ext/ripple.rb | 9 ++++----- lib/awesome_print/ext/sequel.rb | 7 +++++-- lib/awesome_print/formatter.rb | 2 +- 8 files changed, 33 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd5710f..dad8677 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ ## master (unreleased) - ActiveRecord: #joins now show the columns #select'ed [@adrianomitre] - [#211] - Handles NoMethodError for IRB implicit `ai` [@jtnegrotto] - [#212] - - Replaced Fixnum reference with Integer + - Replaced Fixnum reference with Integer + - Colorize ORM class names [@ixti] ## 1.7.0 - Refactoring by extracting formatters into their own classes [@waldyr] - [#237] @@ -148,6 +149,7 @@ [@clonezone]: https://github.com/clonezone [@cyberdelia]: https://github.com/cyberdelia [@gerrywastaken]: https://github.com/gerrywastaken + [@ixti]: https://github.com/ixti [@jtnegrotto]: https://github.com/jtnegrotto [@kemmason]: https://github.com/kemmason [@maurogeorge]: https://github.com/maurogeorge diff --git a/lib/awesome_print/ext/active_record.rb b/lib/awesome_print/ext/active_record.rb index a00aa11..1516c0b 100644 --- a/lib/awesome_print/ext/active_record.rb +++ b/lib/awesome_print/ext/active_record.rb @@ -66,7 +66,11 @@ module AwesomePrint hash[c.name.to_sym] = c.type hash end - "class #{object} < #{object.superclass} " << awesome_hash(data) + + name = "class #{awesome_simple(object.to_s, :class)}" + base = "< #{awesome_simple(object.superclass.to_s, :class)}" + + [name, base, awesome_hash(data)].join(' ') end end end diff --git a/lib/awesome_print/ext/mongo_mapper.rb b/lib/awesome_print/ext/mongo_mapper.rb index 840e3d7..fd6e801 100644 --- a/lib/awesome_print/ext/mongo_mapper.rb +++ b/lib/awesome_print/ext/mongo_mapper.rb @@ -49,7 +49,10 @@ module AwesomePrint end end - "class #{object} < #{object.superclass} " << awesome_hash(data) + name = "class #{awesome_simple(object.to_s, :class)}" + base = "< #{awesome_simple(object.superclass.to_s, :class)}" + + [name, base, awesome_hash(data)].join(' ') end # Format MongoMapper instance object. diff --git a/lib/awesome_print/ext/mongoid.rb b/lib/awesome_print/ext/mongoid.rb index 229c943..5a427eb 100644 --- a/lib/awesome_print/ext/mongoid.rb +++ b/lib/awesome_print/ext/mongoid.rb @@ -36,7 +36,11 @@ module AwesomePrint hash[c[1].name.to_sym] = (c[1].type || 'undefined').to_s.underscore.intern hash end - "class #{object} < #{object.superclass} " << awesome_hash(data) + + name = "class #{awesome_simple(object.to_s, :class)}" + base = "< #{awesome_simple(object.superclass.to_s, :class)}" + + [name, base, awesome_hash(data)].join(' ') end # Format Mongoid Document object. diff --git a/lib/awesome_print/ext/nobrainer.rb b/lib/awesome_print/ext/nobrainer.rb index 9ad37b9..109a656 100644 --- a/lib/awesome_print/ext/nobrainer.rb +++ b/lib/awesome_print/ext/nobrainer.rb @@ -28,10 +28,15 @@ module AwesomePrint # Format NoBrainer class object. #------------------------------------------------------------------------------ def awesome_nobrainer_class(object) + name = "#{awesome_simple(object, :class)} < #{awesome_simple(object.superclass, :class)}" data = Hash[object.fields.map do |field, options| [field, (options[:type] || Object).to_s.underscore.to_sym] end] - "class #{object} < #{object.superclass} " << awesome_hash(data) + + name = "class #{awesome_simple(object.to_s, :class)}" + base = "< #{awesome_simple(object.superclass.to_s, :class)}" + + [name, base, awesome_hash(data)].join(' ') end # Format NoBrainer Document object. diff --git a/lib/awesome_print/ext/ripple.rb b/lib/awesome_print/ext/ripple.rb index 68ebd5b..e2d243f 100644 --- a/lib/awesome_print/ext/ripple.rb +++ b/lib/awesome_print/ext/ripple.rb @@ -60,11 +60,10 @@ module AwesomePrint def awesome_ripple_document_class(object) return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:properties) - data = object.properties.inject(::ActiveSupport::OrderedHash.new) do |hash, (name, defn)| - hash[name.to_sym] = defn.type.to_s.downcase.to_sym - hash - end - "class #{object} < #{object.superclass} " << awesome_hash(data) + name = "class #{awesome_simple(object.to_s, :class)}" + base = "< #{awesome_simple(object.superclass.to_s, :class)}" + + [name, base, awesome_hash(data)].join(' ') end end end diff --git a/lib/awesome_print/ext/sequel.rb b/lib/awesome_print/ext/sequel.rb index 5ee5acf..d45129e 100644 --- a/lib/awesome_print/ext/sequel.rb +++ b/lib/awesome_print/ext/sequel.rb @@ -45,8 +45,11 @@ module AwesomePrint # Format Sequel Model class. #------------------------------------------------------------------------------ def awesome_sequel_model_class(object) - result = object.db_schema.inject({}) { |h, (name, data)| h.merge(name => data[:db_type]) } - "class #{object} < #{object.superclass} " << awesome_hash(result) + data = object.db_schema.inject({}) { |h, (prop, defn)| h.merge(prop => defn[:db_type]) } + name = "class #{awesome_simple(object.to_s, :class)}" + base = "< #{awesome_simple(object.superclass.to_s, :class)}" + + [name, base, awesome_hash(data)].join(' ') end end diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 0d59304..98dace1 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -63,7 +63,7 @@ module AwesomePrint awesome_simple(o, type, @inspector) end - def awesome_simple(o, type, inspector) + def awesome_simple(o, type, inspector = @inspector) AwesomePrint::Formatters::SimpleFormatter.new(o, type, inspector).format end