From 7e6e7c01aa049170d46187fdacdb41944e714522 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 12 Feb 2015 19:57:41 -0200 Subject: [PATCH 01/59] Remove conditional check against old Ruby versions AwesomePrint supports Ruby 1.9.3 or later. This remove lot of conditional check against Ruby prior to 1.9.3. --- spec/ext/active_record_spec.rb | 30 ++------ spec/formats_spec.rb | 61 ++++------------ spec/methods_spec.rb | 128 ++++++++++++--------------------- spec/misc_spec.rb | 17 ++--- 4 files changed, 69 insertions(+), 167 deletions(-) diff --git a/spec/ext/active_record_spec.rb b/spec/ext/active_record_spec.rb index 6b67ac38..860a5db2 100644 --- a/spec/ext/active_record_spec.rb +++ b/spec/ext/active_record_spec.rb @@ -27,11 +27,7 @@ :rank => 1 } EOS - if RUBY_VERSION < '1.9' - str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992') - else - str.sub!('?', '1992-10-10 12:30:00 UTC') - end + str.sub!('?', '1992-10-10 12:30:00 UTC') expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str) end @@ -55,13 +51,8 @@ } ] EOS - if RUBY_VERSION < '1.9' - str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992') - str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003') - else - str.sub!('??', '1992-10-10 12:30:00 UTC') - str.sub!('?!', '2003-05-26 14:15:00 UTC') - end + str.sub!('??', '1992-10-10 12:30:00 UTC') + str.sub!('?!', '2003-05-26 14:15:00 UTC') expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str) end @@ -87,13 +78,8 @@ } ] EOS - if RUBY_VERSION < '1.9' - str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992') - str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003') - else - str.sub!('??', '1992-10-10 12:30:00 UTC') - str.sub!('?!', '2003-05-26 14:15:00 UTC') - end + str.sub!('??', '1992-10-10 12:30:00 UTC') + str.sub!('?!', '2003-05-26 14:15:00 UTC') expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str) end end @@ -1695,11 +1681,7 @@ class SubUser < User { out = @ap.send(:awesome, User.methods.grep(/first/)) if ActiveRecord::VERSION::STRING >= "3.2" - if RUBY_VERSION >= "1.9" - expect(out).to match(/\sfirst\(\*args,\s&block\)\s+Class \(ActiveRecord::Querying\)/) - else - expect(out).to match(/\sfirst\(\*arg1\)\s+Class \(ActiveRecord::Querying\)/) - end + expect(out).to match(/\sfirst\(\*args,\s&block\)\s+Class \(ActiveRecord::Querying\)/) else expect(out).to match(/\sfirst\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/) end diff --git a/spec/formats_spec.rb b/spec/formats_spec.rb index 4c1c8ace..91c8b0bd 100644 --- a/spec/formats_spec.rb +++ b/spec/formats_spec.rb @@ -361,14 +361,7 @@ it "plain multiline" do out = @hash.ai(:plain => true) - if RUBY_VERSION.to_f < 1.9 # Order of @hash keys is not guaranteed. - expect(out).to match(/^\{[^\}]+\}/m) - expect(out).to match(/ "b" => "b",?/) - expect(out).to match(/ :a => "a",?/) - expect(out).to match(/ :z => "z",?/) - expect(out).to match(/ "alpha" => "alpha",?$/) - else - expect(out).to eq <<-EOS.strip + expect(out).to eq <<-EOS.strip { "b" => "b", :a => "a", @@ -376,7 +369,6 @@ "alpha" => "alpha" } EOS - end end it "plain multiline with sorted keys" do @@ -488,16 +480,7 @@ it "should present Rational object with arbitrary precision" do rat = Rational(201020102010201020102010201020102010, 2) out = rat.ai(:plain => true) - # - # Ruby 1.9 slightly changed the format of Rational#to_s, see - # http://techtime.getharvest.com/blog/harvest-is-now-on-ruby-1-dot-9-3 and - # http://www.ruby-forum.com/topic/189397 - # - if RUBY_VERSION < "1.9" - expect(out).to eq("100510051005100510051005100510051005") - else - expect(out).to eq("100510051005100510051005100510051005/1") - end + expect(out).to eq("100510051005100510051005100510051005/1") end end @@ -523,38 +506,20 @@ expect(Set.new.ai).to eq([].ai) end - if RUBY_VERSION > "1.9" - it "plain multiline" do - expect(@set.ai(:plain => true)).to eq(@arr.ai(:plain => true)) - end - - it "plain multiline indented" do - expect(@set.ai(:plain => true, :indent => 1)).to eq(@arr.ai(:plain => true, :indent => 1)) - end - - it "plain single line" do - expect(@set.ai(:plain => true, :multiline => false)).to eq(@arr.ai(:plain => true, :multiline => false)) - end - - it "colored multiline (default)" do - expect(@set.ai).to eq(@arr.ai) - end - else # Prior to Ruby 1.9 the order of set values is unpredicatble. - it "plain multiline" do - expect(@set.sort_by{ |x| x.to_s }.ai(:plain => true)).to eq(@arr.sort_by{ |x| x.to_s }.ai(:plain => true)) - end + it "plain multiline" do + expect(@set.ai(:plain => true)).to eq(@arr.ai(:plain => true)) + end - it "plain multiline indented" do - expect(@set.sort_by{ |x| x.to_s }.ai(:plain => true, :indent => 1)).to eq(@arr.sort_by{ |x| x.to_s }.ai(:plain => true, :indent => 1)) - end + it "plain multiline indented" do + expect(@set.ai(:plain => true, :indent => 1)).to eq(@arr.ai(:plain => true, :indent => 1)) + end - it "plain single line" do - expect(@set.sort_by{ |x| x.to_s }.ai(:plain => true, :multiline => false)).to eq(@arr.sort_by{ |x| x.to_s }.ai(:plain => true, :multiline => false)) - end + it "plain single line" do + expect(@set.ai(:plain => true, :multiline => false)).to eq(@arr.ai(:plain => true, :multiline => false)) + end - it "colored multiline (default)" do - expect(@set.sort_by{ |x| x.to_s }.ai).to eq(@arr.sort_by{ |x| x.to_s }.ai) - end + it "colored multiline (default)" do + expect(@set.ai).to eq(@arr.ai) end end diff --git a/spec/methods_spec.rb b/spec/methods_spec.rb index 0ad25ab5..8aa55243 100644 --- a/spec/methods_spec.rb +++ b/spec/methods_spec.rb @@ -72,11 +72,7 @@ class Hello def world(a,b); end end method = Hello.instance_method(:world) - if RUBY_VERSION < '1.9.2' - expect(method.ai).to eq("\e[1;33mHello (unbound)\e[0m#\e[0;35mworld\e[0m\e[0;37m(arg1, arg2)\e[0m") - else - expect(method.ai).to eq("\e[1;33mHello (unbound)\e[0m#\e[0;35mworld\e[0m\e[0;37m(a, b)\e[0m") - end + expect(method.ai).to eq("\e[1;33mHello (unbound)\e[0m#\e[0;35mworld\e[0m\e[0;37m(a, b)\e[0m") end end @@ -140,11 +136,7 @@ class Hello protected def m3(a,b); end end - if RUBY_VERSION < '1.9.2' - expect(Hello.new.protected_methods.ai(:plain => true, :index => false)).to eq("[\n m3(arg1, arg2) Hello\n]") - else - expect(Hello.new.protected_methods.ai(:plain => true, :index => false)).to eq("[\n m3(a, b) Hello\n]") - end + expect(Hello.new.protected_methods.ai(:plain => true, :index => false)).to eq("[\n m3(a, b) Hello\n]") end end @@ -167,11 +159,7 @@ class Hello def m3(a,b); end end out = Hello.new.private_methods.ai(:plain => true).split("\n").grep(/m\d/) - if RUBY_VERSION < '1.9.2' - expect(out.first).to match(/^\s+\[\s*\d+\]\s+m3\(arg1, arg2\)\s+Hello$/) - else - expect(out.first).to match(/^\s+\[\s*\d+\]\s+m3\(a, b\)\s+Hello$/) - end + expect(out.first).to match(/^\s+\[\s*\d+\]\s+m3\(a, b\)\s+Hello$/) end end @@ -193,11 +181,7 @@ class Hello def self.m3(a,b); end end out = Hello.singleton_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/) - if RUBY_VERSION < '1.9.2' - expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello$/) - else - expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello$/) - end + expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello$/) end end end @@ -227,11 +211,7 @@ class Hello def m3(a,b); end end out = Hello.instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/) - if RUBY_VERSION < '1.9.2' - expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/) - else - expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/) - end + expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/) end end @@ -251,11 +231,7 @@ class Hello def m3(a,b); end end out = Hello.public_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/) - if RUBY_VERSION < '1.9.2' - expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/) - else - expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/) - end + expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/) end end @@ -277,11 +253,7 @@ class Hello def m3(a,b); end end out = Hello.protected_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/) - if RUBY_VERSION < '1.9.2' - expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/) - else - expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/) - end + expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/) end end @@ -303,64 +275,58 @@ class Hello def m3(a,b); end end out = Hello.private_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/) - if RUBY_VERSION < '1.9.2' - expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/) - else - expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/) - end + expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/) end end end -if RUBY_VERSION >= '1.9.2' - RSpec.describe "Ruby 1.9.2+ Method#parameters" do - before do - stub_dotfile! - end +RSpec.describe "Ruby 1.9.2+ Method#parameters" do + before do + stub_dotfile! + end - after do - Object.instance_eval{ remove_const :Hello } if defined?(Hello) - end + after do + Object.instance_eval{ remove_const :Hello } if defined?(Hello) + end - it "()" do - class Hello - def m1; end - end - out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/) - expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello$/) + it "()" do + class Hello + def m1; end end + out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/) + expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello$/) + end - it ":req" do - class Hello - def m1(a, b, c); end - end - out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/) - expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, b, c\)\s+Hello$/) + it ":req" do + class Hello + def m1(a, b, c); end end + out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/) + expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, b, c\)\s+Hello$/) + end - it ":opt" do - class Hello - def m1(a, b = 1, c = 2); end # m1(a, *b, *c) - end - out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/) - expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, \*b, \*c\)\s+Hello$/) + it ":opt" do + class Hello + def m1(a, b = 1, c = 2); end # m1(a, *b, *c) end + out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/) + expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, \*b, \*c\)\s+Hello$/) + end - it ":rest" do - class Hello - def m1(*a); end # m1(*a) - end - out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/) - expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\*a\)\s+Hello$/) + it ":rest" do + class Hello + def m1(*a); end # m1(*a) end + out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/) + expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\*a\)\s+Hello$/) + end - it ":block" do - class Hello - def m1(a, b = nil, &blk); end # m1(a, *b, &blk) - end - out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/) - expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, \*b, &blk\)\s+Hello$/) + it ":block" do + class Hello + def m1(a, b = nil, &blk); end # m1(a, *b, &blk) end + out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/) + expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, \*b, &blk\)\s+Hello$/) end end @@ -450,10 +416,6 @@ def her it "appending garbage to methods array should not raise error" do arr = 42.methods << [ :wtf ] expect { arr.ai(:plain => true) }.not_to raise_error - if RUBY_VERSION < '1.9.2' - expect(arr.ai(:plain => true)).to match(/\s+wtf\(\?\)\s+\?/) # [ :wtf ].to_s => "wtf" - else - expect(arr.ai(:plain => true)).to match(/\s+\[:wtf\]\(\?\)\s+\?/) # [ :wtf ].to_s => [:wtf] - end + expect(arr.ai(:plain => true)).to match(/\s+\[:wtf\]\(\?\)\s+\?/) # [ :wtf ].to_s => [:wtf] end end diff --git a/spec/misc_spec.rb b/spec/misc_spec.rb index 21b50ce6..d9db9889 100644 --- a/spec/misc_spec.rb +++ b/spec/misc_spec.rb @@ -32,13 +32,10 @@ def inspect expect(grepped.ai(:plain => true, :multiline => false)).to eq('[ "1", "2" ]') end - # See https://github.com/michaeldv/awesome_print/issues/85 - if RUBY_VERSION >= "1.8.7" - it "handle array grep when a method is defined in C and thus doesn't have a binding" do - arr = (0..6).to_a - grepped = arr.grep(1..4, &:succ) - expect(grepped.ai(:plain => true, :multiline => false)).to eq('[ 2, 3, 4, 5 ]') - end + it "handle array grep when a method is defined in C and thus doesn't have a binding" do + arr = (0..6).to_a + grepped = arr.grep(1..4, &:succ) + expect(grepped.ai(:plain => true, :multiline => false)).to eq('[ 2, 3, 4, 5 ]') end it "returns value passed as a parameter" do @@ -177,11 +174,7 @@ def ==(other) it "shoud not raise ArgumentError when formatting HTML" do out = "hello".ai(:color => { :string => :red }, :html => true) - if RUBY_VERSION >= "1.9" - expect(out).to eq(%Q|
[red]"hello"[/red]
|) - else - expect(out).to eq(%Q|
[red]"hello"[/red]
|) - end + expect(out).to eq(%Q|
[red]"hello"[/red]
|) end it "shoud not raise ArgumentError when formatting HTML (shade color)" do From f6fbe023b4cb155f06f40061e25cb769906591f7 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 19 Feb 2015 19:32:18 -0200 Subject: [PATCH 02/59] Remove init.rb and rails/init.rb Since we support only rails greater than 3.2 we don't need this init files. --- init.rb | 1 - rails/init.rb | 9 --------- 2 files changed, 10 deletions(-) delete mode 100644 init.rb delete mode 100644 rails/init.rb diff --git a/init.rb b/init.rb deleted file mode 100644 index b1861774..00000000 --- a/init.rb +++ /dev/null @@ -1 +0,0 @@ -require File.join(File.dirname(__FILE__), "lib", "awesome_print") diff --git a/rails/init.rb b/rails/init.rb deleted file mode 100644 index ef0b9190..00000000 --- a/rails/init.rb +++ /dev/null @@ -1,9 +0,0 @@ -# Copyright (c) 2010-2013 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 -#------------------------------------------------------------------------------ -# -# Load awesome_print when installed as Rails 2.3.x plugin. -# -require File.join(File.dirname(__FILE__), "..", "init") unless defined?(AwesomePrint) From c75c058ed627b23dd392de5792c5366cbab117c8 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 19 Feb 2015 19:36:33 -0200 Subject: [PATCH 03/59] Remove the lib/ap.rb This ways we need to require always awesome_print --- lib/ap.rb | 10 ---------- spec/misc_spec.rb | 3 +-- 2 files changed, 1 insertion(+), 12 deletions(-) delete mode 100644 lib/ap.rb diff --git a/lib/ap.rb b/lib/ap.rb deleted file mode 100644 index 793a9afa..00000000 --- a/lib/ap.rb +++ /dev/null @@ -1,10 +0,0 @@ -# Copyright (c) 2010-2013 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 -#------------------------------------------------------------------------------ -# -# Keeping this for backwards compatibility to allow -# require "ap" -# -require File.dirname(__FILE__) + "/awesome_print" diff --git a/spec/misc_spec.rb b/spec/misc_spec.rb index d9db9889..f6be128a 100644 --- a/spec/misc_spec.rb +++ b/spec/misc_spec.rb @@ -44,9 +44,8 @@ def inspect expect(ap object).to eq(object) end - # Require different file name this time (lib/ap.rb vs. lib/awesome_print). it "several require 'awesome_print' should do no harm" do - require File.expand_path(File.dirname(__FILE__) + '/../lib/ap') + require File.expand_path(File.dirname(__FILE__) + '/../lib/awesome_print') expect { rand.ai }.not_to raise_error end From cf91f4e864b2fe1907f3449ded37d226595b84ee Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 19 Feb 2015 19:50:27 -0200 Subject: [PATCH 04/59] Remove copyright from each file --- awesome_print.gemspec | 5 ----- lib/awesome_print.rb | 5 ----- lib/awesome_print/core_ext/array.rb | 5 ----- lib/awesome_print/core_ext/class.rb | 5 ----- lib/awesome_print/core_ext/kernel.rb | 5 ----- lib/awesome_print/core_ext/logger.rb | 5 ----- lib/awesome_print/core_ext/method.rb | 5 ----- lib/awesome_print/core_ext/object.rb | 5 ----- lib/awesome_print/core_ext/string.rb | 5 ----- lib/awesome_print/ext/action_view.rb | 5 ----- lib/awesome_print/ext/active_record.rb | 5 ----- lib/awesome_print/ext/active_support.rb | 5 ----- lib/awesome_print/ext/mongo_mapper.rb | 5 ----- lib/awesome_print/ext/mongoid.rb | 5 ----- lib/awesome_print/ext/nobrainer.rb | 5 ----- lib/awesome_print/ext/nokogiri.rb | 5 ----- lib/awesome_print/ext/ostruct.rb | 5 ----- lib/awesome_print/ext/ripple.rb | 5 ----- lib/awesome_print/ext/sequel.rb | 5 ----- lib/awesome_print/formatter.rb | 5 ----- lib/awesome_print/inspector.rb | 5 ----- lib/awesome_print/version.rb | 5 ----- spec/spec_helper.rb | 5 ----- 23 files changed, 115 deletions(-) diff --git a/awesome_print.gemspec b/awesome_print.gemspec index 4db950ed..a00b20dd 100644 --- a/awesome_print.gemspec +++ b/awesome_print.gemspec @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 -#------------------------------------------------------------------------------ require "rake" Gem::Specification.new do |s| diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index ab4a6699..d1796ba8 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 -#------------------------------------------------------------------------------ # # AwesomePrint might be loaded implicitly through ~/.irbrc or ~/.pryrc # so do nothing for subsequent requires. diff --git a/lib/awesome_print/core_ext/array.rb b/lib/awesome_print/core_ext/array.rb index ec1a1cda..2aa9b72d 100644 --- a/lib/awesome_print/core_ext/array.rb +++ b/lib/awesome_print/core_ext/array.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 -#------------------------------------------------------------------------------ # # The following makes it possible to invoke awesome_print while performing # operations on method arrays, ex: diff --git a/lib/awesome_print/core_ext/class.rb b/lib/awesome_print/core_ext/class.rb index 5faac7e8..48d09593 100644 --- a/lib/awesome_print/core_ext/class.rb +++ b/lib/awesome_print/core_ext/class.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 -#------------------------------------------------------------------------------ class Class #:nodoc: # # Intercept methods below to inject @__awesome_print__ instance variable diff --git a/lib/awesome_print/core_ext/kernel.rb b/lib/awesome_print/core_ext/kernel.rb index a87d19c8..bb87daa7 100644 --- a/lib/awesome_print/core_ext/kernel.rb +++ b/lib/awesome_print/core_ext/kernel.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 Kernel def ai(options = {}) diff --git a/lib/awesome_print/core_ext/logger.rb b/lib/awesome_print/core_ext/logger.rb index 516609c3..05809a1c 100644 --- a/lib/awesome_print/core_ext/logger.rb +++ b/lib/awesome_print/core_ext/logger.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 Logger diff --git a/lib/awesome_print/core_ext/method.rb b/lib/awesome_print/core_ext/method.rb index a30b1487..f997e107 100644 --- a/lib/awesome_print/core_ext/method.rb +++ b/lib/awesome_print/core_ext/method.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 -#------------------------------------------------------------------------------ # # Method#name was intorduced in Ruby 1.8.7 so we define it here as necessary. # diff --git a/lib/awesome_print/core_ext/object.rb b/lib/awesome_print/core_ext/object.rb index c58e2795..3f9dc5d0 100644 --- a/lib/awesome_print/core_ext/object.rb +++ b/lib/awesome_print/core_ext/object.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 -#------------------------------------------------------------------------------ class Object #:nodoc: # # Intercept methods below to inject @__awesome_print__ instance variable diff --git a/lib/awesome_print/core_ext/string.rb b/lib/awesome_print/core_ext/string.rb index 3fa1ed9d..ad727925 100644 --- a/lib/awesome_print/core_ext/string.rb +++ b/lib/awesome_print/core_ext/string.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 -#------------------------------------------------------------------------------ class String # # ANSI color codes: diff --git a/lib/awesome_print/ext/action_view.rb b/lib/awesome_print/ext/action_view.rb index c34e8d05..19a0a146 100644 --- a/lib/awesome_print/ext/action_view.rb +++ b/lib/awesome_print/ext/action_view.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 ActionView diff --git a/lib/awesome_print/ext/active_record.rb b/lib/awesome_print/ext/active_record.rb index e04f9fe5..6a3dbebb 100644 --- a/lib/awesome_print/ext/active_record.rb +++ b/lib/awesome_print/ext/active_record.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 ActiveRecord diff --git a/lib/awesome_print/ext/active_support.rb b/lib/awesome_print/ext/active_support.rb index 2774990d..ba2a730b 100644 --- a/lib/awesome_print/ext/active_support.rb +++ b/lib/awesome_print/ext/active_support.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 ActiveSupport diff --git a/lib/awesome_print/ext/mongo_mapper.rb b/lib/awesome_print/ext/mongo_mapper.rb index 4e7a81a9..32461341 100644 --- a/lib/awesome_print/ext/mongo_mapper.rb +++ b/lib/awesome_print/ext/mongo_mapper.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 MongoMapper diff --git a/lib/awesome_print/ext/mongoid.rb b/lib/awesome_print/ext/mongoid.rb index 6d4d1030..bd862027 100644 --- a/lib/awesome_print/ext/mongoid.rb +++ b/lib/awesome_print/ext/mongoid.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 Mongoid diff --git a/lib/awesome_print/ext/nobrainer.rb b/lib/awesome_print/ext/nobrainer.rb index d13b9068..177fe9c0 100644 --- a/lib/awesome_print/ext/nobrainer.rb +++ b/lib/awesome_print/ext/nobrainer.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 diff --git a/lib/awesome_print/ext/nokogiri.rb b/lib/awesome_print/ext/nokogiri.rb index 4d26c92d..36853002 100644 --- a/lib/awesome_print/ext/nokogiri.rb +++ b/lib/awesome_print/ext/nokogiri.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 Nokogiri diff --git a/lib/awesome_print/ext/ostruct.rb b/lib/awesome_print/ext/ostruct.rb index 199122f8..84b53eb9 100644 --- a/lib/awesome_print/ext/ostruct.rb +++ b/lib/awesome_print/ext/ostruct.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 OpenStruct def self.included(base) diff --git a/lib/awesome_print/ext/ripple.rb b/lib/awesome_print/ext/ripple.rb index 7bd6facf..1956dc79 100644 --- a/lib/awesome_print/ext/ripple.rb +++ b/lib/awesome_print/ext/ripple.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 Ripple diff --git a/lib/awesome_print/ext/sequel.rb b/lib/awesome_print/ext/sequel.rb index 75cfa23e..95c12e20 100644 --- a/lib/awesome_print/ext/sequel.rb +++ b/lib/awesome_print/ext/sequel.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 0e07ecbb..e4858aee 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 -#------------------------------------------------------------------------------ autoload :CGI, "cgi" require "shellwords" diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 0893a90a..34e47f78 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 class << self # Class accessors for custom defaults. diff --git a/lib/awesome_print/version.rb b/lib/awesome_print/version.rb index 0e1edabc..7bb69fc9 100644 --- a/lib/awesome_print/version.rb +++ b/lib/awesome_print/version.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 def self.version '1.6.2' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4d43a29f..804b5ff4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,8 +1,3 @@ -# Copyright (c) 2010-2013 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 -#------------------------------------------------------------------------------ # # Running specs from the command line: # $ rake spec # Entire spec suite. From f5f494bd98f12f69d554f9b6533b1debba09e6db Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 26 Feb 2015 19:39:56 -0300 Subject: [PATCH 05/59] Split Inspector in 2 files Just split in 2 responsabilities. Maybe configuration is not a good name. Just a begin to learn more about the code. --- lib/awesome_print.rb | 1 + lib/awesome_print/configuration.rb | 44 +++++++++++++++++++++++++++ lib/awesome_print/inspector.rb | 49 ++---------------------------- 3 files changed, 48 insertions(+), 46 deletions(-) create mode 100644 lib/awesome_print/configuration.rb diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index d1796ba8..0bdfb507 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -7,6 +7,7 @@ require File.dirname(__FILE__) + "/awesome_print/core_ext/#{file}" end + require File.dirname(__FILE__) + "/awesome_print/configuration" require File.dirname(__FILE__) + "/awesome_print/inspector" require File.dirname(__FILE__) + "/awesome_print/formatter" require File.dirname(__FILE__) + "/awesome_print/version" diff --git a/lib/awesome_print/configuration.rb b/lib/awesome_print/configuration.rb new file mode 100644 index 00000000..6c530aac --- /dev/null +++ b/lib/awesome_print/configuration.rb @@ -0,0 +1,44 @@ +module AwesomePrint + + class << self # Class accessors for custom defaults. + attr_accessor :defaults, :force_colors + + # Class accessor to force colorized output (ex. forked subprocess where TERM + # might be dumb). + #------------------------------------------------------------------------------ + def force_colors!(value = true) + @force_colors = value + end + + def console? + !!(defined?(IRB) || defined?(Pry)) + end + + def rails_console? + console? && !!(defined?(Rails::Console) || ENV["RAILS_ENV"]) + end + + def irb! + return unless defined?(IRB) + unless IRB.version.include?("DietRB") + IRB::Irb.class_eval do + def output_value + ap @context.last_value + end + end + else # MacRuby + IRB.formatter = Class.new(IRB::Formatter) do + def inspect_object(object) + object.ai + end + end.new + end + end + + def pry! + if defined?(Pry) + Pry.print = proc { |output, value| output.puts value.ai } + end + end + end +end diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 34e47f78..e887618a 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -1,54 +1,11 @@ module AwesomePrint - - class << self # Class accessors for custom defaults. - attr_accessor :defaults, :force_colors - - # Class accessor to force colorized output (ex. forked subprocess where TERM - # might be dumb). - #------------------------------------------------------------------------------ - def force_colors!(value = true) - @force_colors = value - end - - def console? - !!(defined?(IRB) || defined?(Pry)) - end - - def rails_console? - console? && !!(defined?(Rails::Console) || ENV["RAILS_ENV"]) - end - - def irb! - return unless defined?(IRB) - unless IRB.version.include?("DietRB") - IRB::Irb.class_eval do - def output_value - ap @context.last_value - end - end - else # MacRuby - IRB.formatter = Class.new(IRB::Formatter) do - def inspect_object(object) - object.ai - end - end.new - end - end - - def pry! - if defined?(Pry) - Pry.print = proc { |output, value| output.puts value.ai } - end - end - end - class Inspector attr_accessor :options AP = :__awesome_print__ def initialize(options = {}) - @options = { + @options = { :indent => 4, # Indent using 4 spaces. :index => true, # Display array indices. :html => false, # Use ANSI color codes rather than HTML. @@ -57,7 +14,7 @@ def initialize(options = {}) :raw => false, # Do not recursively format object instance variables. :sort_keys => false, # Do not sort hash keys. :limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. - :color => { + :color => { :args => :pale, :array => :white, :bigdecimal => :blue, @@ -87,7 +44,7 @@ def initialize(options = {}) @formatter = AwesomePrint::Formatter.new(self) Thread.current[AP] ||= [] end - + # Dispatcher that detects data nesting and invokes object-aware formatter. #------------------------------------------------------------------------------ def awesome(object) From b48989c261a85220cf7f880a3884d0e871527a19 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 26 Feb 2015 20:13:12 -0300 Subject: [PATCH 06/59] Extract Array Formatter Moved from the Formatter to a single class that do the Array formatation. --- lib/awesome_print/formatter.rb | 224 ++++++++++++-------------- lib/awesome_print/formatters/array.rb | 64 ++++++++ 2 files changed, 169 insertions(+), 119 deletions(-) create mode 100644 lib/awesome_print/formatters/array.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index e4858aee..ffde834a 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -1,5 +1,7 @@ -autoload :CGI, "cgi" -require "shellwords" +autoload :CGI, 'cgi' +require 'shellwords' + +require 'awesome_print/formatters/array' module AwesomePrint class Formatter @@ -7,6 +9,8 @@ class Formatter CORE = [ :array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod ] DEFAULT_LIMIT_SIZE = 7 + attr_reader :options, :inspector + def initialize(inspector) @inspector = inspector @options = inspector.options @@ -50,6 +54,102 @@ def colorize(str, type) end end + def indent + ' ' * @indentation + end + + def outdent + ' ' * (@indentation - @options[:indent].abs) + end + + def indented + @indentation += @options[:indent].abs + yield + ensure + @indentation -= @options[:indent].abs + end + + # To support limited output, for example: + # + # ap ('a'..'z').to_a, :limit => 3 + # [ + # [ 0] "a", + # [ 1] .. [24], + # [25] "z" + # ] + # + # ap (1..100).to_a, :limit => true # Default limit is 7. + # [ + # [ 0] 1, + # [ 1] 2, + # [ 2] 3, + # [ 3] .. [96], + # [97] 98, + # [98] 99, + # [99] 100 + # ] + #------------------------------------------------------------------------------ + def should_be_limited? + @options[:limit] == true or (@options[:limit].is_a?(Fixnum) and @options[:limit] > 0) + end + + def limited(data, width, is_hash = false) + limit = get_limit_size + if data.length <= limit + data + else + # Calculate how many elements to be displayed above and below the separator. + head = limit / 2 + tail = head - (limit - 1) % 2 + + # Add the proper elements to the temp array and format the separator. + temp = data[0, head] + [ nil ] + data[-tail, tail] + + if is_hash + temp[head] = "#{indent}#{data[head].strip} .. #{data[data.length - tail - 1].strip}" + else + temp[head] = "#{indent}[#{head.to_s.rjust(width)}] .. [#{data.length - tail - 1}]" + end + + temp + end + end + + # Format object.methods array. + #------------------------------------------------------------------------------ + def methods_array(a) + a.sort! { |x, y| x.to_s <=> y.to_s } # Can't simply a.sort! because of o.methods << [ :blah ] + object = a.instance_variable_get('@__awesome_methods__') + tuples = a.map do |name| + if name.is_a?(Symbol) || name.is_a?(String) # Ignore garbage, ex. 42.methods << [ :blah ] + tuple = if object.respond_to?(name, true) # Is this a regular method? + the_method = object.method(name) rescue nil # Avoid potential ArgumentError if object#method is overridden. + if the_method && the_method.respond_to?(:arity) # Is this original object#method? + method_tuple(the_method) # Yes, we are good. + end + elsif object.respond_to?(:instance_method) # Is this an unbound method? + method_tuple(object.instance_method(name)) rescue nil # Rescue to avoid NameError when the method is not + end # available (ex. File.lchmod on Ubuntu 12). + end + tuple || [ name.to_s, '(?)', '?' ] # Return WTF default if all the above fails. + end + + width = (tuples.size - 1).to_s.size + name_width = tuples.map { |item| item[0].size }.max || 0 + args_width = tuples.map { |item| item[1].size }.max || 0 + + data = tuples.inject([]) do |arr, item| + index = indent + index << "[#{arr.size.to_s.rjust(width)}]" if @options[:index] + indented do + arr << "#{index} #{colorize(item[0].rjust(name_width), :method)}#{colorize(item[1].ljust(args_width), :args)} #{colorize(item[2], :class)}" + end + end + + "[\n" << data.join("\n") << "\n#{outdent}]" + end + + private @@ -68,26 +168,7 @@ def awesome_self(object, type) # Format an array. #------------------------------------------------------------------------------ def awesome_array(a) - return "[]" if a == [] - - if a.instance_variable_defined?('@__awesome_methods__') - methods_array(a) - elsif @options[:multiline] - width = (a.size - 1).to_s.size - - data = a.inject([]) do |arr, item| - index = indent - index << colorize("[#{arr.size.to_s.rjust(width)}] ", :array) if @options[:index] - indented do - arr << (index << @inspector.awesome(item)) - end - end - - data = limited(data, width) if should_be_limited? - "[\n" << data.join(",\n") << "\n#{outdent}]" - else - "[ " << a.map{ |item| @inspector.awesome(item) }.join(", ") << " ]" - end + AwesomePrint::Formatters::Array.new(self, a).call end # Format a hash. If @options[:indent] if negative left align hash keys. @@ -101,10 +182,10 @@ def awesome_hash(h) [ @inspector.awesome(key), h[key] ] end end - + width = data.map { |key, | key.size }.max || 0 width += @indentation if @options[:indent] > 0 - + data = data.map do |key, value| indented do align(key, width) << colorize(" => ", :hash) << @inspector.awesome(value) @@ -228,40 +309,6 @@ def awesome_instance(o) "#{o.class}:0x%08x" % (o.__id__ * 2) end - # Format object.methods array. - #------------------------------------------------------------------------------ - def methods_array(a) - a.sort! { |x, y| x.to_s <=> y.to_s } # Can't simply a.sort! because of o.methods << [ :blah ] - object = a.instance_variable_get('@__awesome_methods__') - tuples = a.map do |name| - if name.is_a?(Symbol) || name.is_a?(String) # Ignore garbage, ex. 42.methods << [ :blah ] - tuple = if object.respond_to?(name, true) # Is this a regular method? - the_method = object.method(name) rescue nil # Avoid potential ArgumentError if object#method is overridden. - if the_method && the_method.respond_to?(:arity) # Is this original object#method? - method_tuple(the_method) # Yes, we are good. - end - elsif object.respond_to?(:instance_method) # Is this an unbound method? - method_tuple(object.instance_method(name)) rescue nil # Rescue to avoid NameError when the method is not - end # available (ex. File.lchmod on Ubuntu 12). - end - tuple || [ name.to_s, '(?)', '?' ] # Return WTF default if all the above fails. - end - - width = (tuples.size - 1).to_s.size - name_width = tuples.map { |item| item[0].size }.max || 0 - args_width = tuples.map { |item| item[1].size }.max || 0 - - data = tuples.inject([]) do |arr, item| - index = indent - index << "[#{arr.size.to_s.rjust(width)}]" if @options[:index] - indented do - arr << "#{index} #{colorize(item[0].rjust(name_width), :method)}#{colorize(item[1].ljust(args_width), :args)} #{colorize(item[2], :class)}" - end - end - - "[\n" << data.join("\n") << "\n#{outdent}]" - end - # Return [ name, arguments, owner ] tuple for a given method. #------------------------------------------------------------------------------ def method_tuple(method) @@ -343,13 +390,6 @@ def align(value, width) end end - def indented - @indentation += @options[:indent].abs - yield - ensure - @indentation -= @options[:indent].abs - end - def left_aligned current, @options[:indent] = @options[:indent], 0 yield @@ -357,62 +397,8 @@ def left_aligned @options[:indent] = current end - def indent - ' ' * @indentation - end - - def outdent - ' ' * (@indentation - @options[:indent].abs) - end - - # To support limited output, for example: - # - # ap ('a'..'z').to_a, :limit => 3 - # [ - # [ 0] "a", - # [ 1] .. [24], - # [25] "z" - # ] - # - # ap (1..100).to_a, :limit => true # Default limit is 7. - # [ - # [ 0] 1, - # [ 1] 2, - # [ 2] 3, - # [ 3] .. [96], - # [97] 98, - # [98] 99, - # [99] 100 - # ] - #------------------------------------------------------------------------------ - def should_be_limited? - @options[:limit] == true or (@options[:limit].is_a?(Fixnum) and @options[:limit] > 0) - end - def get_limit_size @options[:limit] == true ? DEFAULT_LIMIT_SIZE : @options[:limit] end - - def limited(data, width, is_hash = false) - limit = get_limit_size - if data.length <= limit - data - else - # Calculate how many elements to be displayed above and below the separator. - head = limit / 2 - tail = head - (limit - 1) % 2 - - # Add the proper elements to the temp array and format the separator. - temp = data[0, head] + [ nil ] + data[-tail, tail] - - if is_hash - temp[head] = "#{indent}#{data[head].strip} .. #{data[data.length - tail - 1].strip}" - else - temp[head] = "#{indent}[#{head.to_s.rjust(width)}] .. [#{data.length - tail - 1}]" - end - - temp - end - end end end diff --git a/lib/awesome_print/formatters/array.rb b/lib/awesome_print/formatters/array.rb new file mode 100644 index 00000000..f9c9a70b --- /dev/null +++ b/lib/awesome_print/formatters/array.rb @@ -0,0 +1,64 @@ +module AwesomePrint + module Formatters + class Array + + def initialize(formatter, array) + @formatter = formatter + @array = array + @options = formatter.options + @inspector = formatter.inspector + end + + def call + return empty_format if array.empty? + + if array.instance_variable_defined?('@__awesome_methods__') + methods_format + elsif options[:multiline] + multiline_format + else + inline_format + end + end + + private + + attr_reader :formatter, :array + + def options + @options + end + + def inspector + @inspector + end + + def empty_format + '[]' + end + + def methods_format + formatter.methods_array(array) + end + + def inline_format + "[ " << array.map{ |item| inspector.awesome(item) }.join(", ") << " ]" + end + + def multiline_format + width = (array.size - 1).to_s.size + + data = array.inject([]) do |arr, item| + index = formatter.indent + index << formatter.colorize("[#{arr.size.to_s.rjust(width)}] ", :array) if options[:index] + formatter.indented do + arr << (index << inspector.awesome(item)) + end + end + + data = formatter.limited(data, width) if formatter.should_be_limited? + "[\n" << data.join(",\n") << "\n#{formatter.outdent}]" + end + end + end +end From 141628d766f017b16e255d498d48b3da1533f4cd Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 4 Mar 2015 19:03:19 -0300 Subject: [PATCH 07/59] Extract Hash Formmatter Moved from the Formatter to a single class that do the Hash formatation. --- lib/awesome_print/formatter.rb | 74 ++++++++++------------------ lib/awesome_print/formatters/hash.rb | 53 ++++++++++++++++++++ 2 files changed, 78 insertions(+), 49 deletions(-) create mode 100644 lib/awesome_print/formatters/hash.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index ffde834a..4b0c2bdd 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -2,6 +2,7 @@ require 'shellwords' require 'awesome_print/formatters/array' +require 'awesome_print/formatters/hash' module AwesomePrint class Formatter @@ -9,7 +10,7 @@ class Formatter CORE = [ :array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod ] DEFAULT_LIMIT_SIZE = 7 - attr_reader :options, :inspector + attr_reader :options, :inspector, :indentation def initialize(inspector) @inspector = inspector @@ -149,7 +150,29 @@ def methods_array(a) "[\n" << data.join("\n") << "\n#{outdent}]" end + # Format hash keys as plain strings regardless of underlying data type. + #------------------------------------------------------------------------------ + def plain_single_line + plain, multiline = @options[:plain], @options[:multiline] + @options[:plain], @options[:multiline] = true, false + yield + ensure + @options[:plain], @options[:multiline] = plain, multiline + end + def align(value, width) + if @options[:multiline] + if @options[:indent] > 0 + value.rjust(width) + elsif @options[:indent] == 0 + indent + value.ljust(width) + else + indent[0, @indentation + @options[:indent]] + value.ljust(width) + end + else + value + end + end private @@ -174,30 +197,7 @@ def awesome_array(a) # Format a hash. If @options[:indent] if negative left align hash keys. #------------------------------------------------------------------------------ def awesome_hash(h) - return "{}" if h == {} - - keys = @options[:sort_keys] ? h.keys.sort { |a, b| a.to_s <=> b.to_s } : h.keys - data = keys.map do |key| - plain_single_line do - [ @inspector.awesome(key), h[key] ] - end - end - - width = data.map { |key, | key.size }.max || 0 - width += @indentation if @options[:indent] > 0 - - data = data.map do |key, value| - indented do - align(key, width) << colorize(" => ", :hash) << @inspector.awesome(value) - end - end - - data = limited(data, width, :hash => true) if should_be_limited? - if @options[:multiline] - "{\n" << data.join(",\n") << "\n#{outdent}}" - else - "{ #{data.join(', ')} }" - end + AwesomePrint::Formatters::Hash.new(self, h).call end # Format an object. @@ -348,16 +348,6 @@ def method_tuple(method) [ method.name.to_s, "(#{args.join(', ')})", owner.to_s ] end - # Format hash keys as plain strings regardless of underlying data type. - #------------------------------------------------------------------------------ - def plain_single_line - plain, multiline = @options[:plain], @options[:multiline] - @options[:plain], @options[:multiline] = true, false - yield - ensure - @options[:plain], @options[:multiline] = plain, multiline - end - # Utility methods. #------------------------------------------------------------------------------ def convert_to_hash(object) @@ -376,20 +366,6 @@ def convert_to_hash(object) return hash end - def align(value, width) - if @options[:multiline] - if @options[:indent] > 0 - value.rjust(width) - elsif @options[:indent] == 0 - indent + value.ljust(width) - else - indent[0, @indentation + @options[:indent]] + value.ljust(width) - end - else - value - end - end - def left_aligned current, @options[:indent] = @options[:indent], 0 yield diff --git a/lib/awesome_print/formatters/hash.rb b/lib/awesome_print/formatters/hash.rb new file mode 100644 index 00000000..a811ae99 --- /dev/null +++ b/lib/awesome_print/formatters/hash.rb @@ -0,0 +1,53 @@ +module AwesomePrint + module Formatters + class Hash + + def initialize(formatter, hash) + @formatter = formatter + @hash = hash + @options = formatter.options + @inspector = formatter.inspector + @indentation = formatter.indentation + end + + def call + return "{}" if hash.empty? + + keys = options[:sort_keys] ? hash.keys.sort { |a, b| a.to_s <=> b.to_s } : hash.keys + data = keys.map do |key| + formatter.plain_single_line do + [ inspector.awesome(key), hash[key] ] + end + end + + width = data.map { |key, | key.size }.max || 0 + width += indentation if options[:indent] > 0 + + data = data.map do |key, value| + formatter.indented do + formatter.align(key, width) << formatter.colorize(" => ", :hash) << inspector.awesome(value) + end + end + + data = formatter.limited(data, width, :hash => true) if formatter.should_be_limited? + if options[:multiline] + "{\n" << data.join(",\n") << "\n#{formatter.outdent}}" + else + "{ #{data.join(', ')} }" + end + end + + private + + attr_reader :formatter, :hash, :indentation + + def options + @options + end + + def inspector + @inspector + end + end + end +end From f23026876b7dc0f66f4144fb92aabad69942ec2b Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 4 Mar 2015 19:09:53 -0300 Subject: [PATCH 08/59] Extract commom behavior from Formatters to a Base class --- lib/awesome_print/formatter.rb | 1 + lib/awesome_print/formatters/array.rb | 31 ++++++--------------------- lib/awesome_print/formatters/base.rb | 26 ++++++++++++++++++++++ lib/awesome_print/formatters/hash.rb | 28 ++++-------------------- 4 files changed, 38 insertions(+), 48 deletions(-) create mode 100644 lib/awesome_print/formatters/base.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 4b0c2bdd..43798c4f 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -1,6 +1,7 @@ autoload :CGI, 'cgi' require 'shellwords' +require 'awesome_print/formatters/base' require 'awesome_print/formatters/array' require 'awesome_print/formatters/hash' diff --git a/lib/awesome_print/formatters/array.rb b/lib/awesome_print/formatters/array.rb index f9c9a70b..10c146b0 100644 --- a/lib/awesome_print/formatters/array.rb +++ b/lib/awesome_print/formatters/array.rb @@ -1,18 +1,11 @@ module AwesomePrint module Formatters - class Array - - def initialize(formatter, array) - @formatter = formatter - @array = array - @options = formatter.options - @inspector = formatter.inspector - end + class Array < Base def call - return empty_format if array.empty? + return empty_format if object.empty? - if array.instance_variable_defined?('@__awesome_methods__') + if object.instance_variable_defined?('@__awesome_methods__') methods_format elsif options[:multiline] multiline_format @@ -23,32 +16,22 @@ def call private - attr_reader :formatter, :array - - def options - @options - end - - def inspector - @inspector - end - def empty_format '[]' end def methods_format - formatter.methods_array(array) + formatter.methods_array(object) end def inline_format - "[ " << array.map{ |item| inspector.awesome(item) }.join(", ") << " ]" + "[ " << object.map{ |item| inspector.awesome(item) }.join(", ") << " ]" end def multiline_format - width = (array.size - 1).to_s.size + width = (object.size - 1).to_s.size - data = array.inject([]) do |arr, item| + data = object.inject([]) do |arr, item| index = formatter.indent index << formatter.colorize("[#{arr.size.to_s.rjust(width)}] ", :array) if options[:index] formatter.indented do diff --git a/lib/awesome_print/formatters/base.rb b/lib/awesome_print/formatters/base.rb new file mode 100644 index 00000000..57d55839 --- /dev/null +++ b/lib/awesome_print/formatters/base.rb @@ -0,0 +1,26 @@ +module AwesomePrint + module Formatters + class Base + + def initialize(formatter, object) + @formatter = formatter + @object = object + @options = formatter.options + @inspector = formatter.inspector + @indentation = formatter.indentation + end + + private + + attr_reader :formatter, :object, :indentation + + def options + @options + end + + def inspector + @inspector + end + end + end +end diff --git a/lib/awesome_print/formatters/hash.rb b/lib/awesome_print/formatters/hash.rb index a811ae99..7a464b62 100644 --- a/lib/awesome_print/formatters/hash.rb +++ b/lib/awesome_print/formatters/hash.rb @@ -1,22 +1,14 @@ module AwesomePrint module Formatters - class Hash - - def initialize(formatter, hash) - @formatter = formatter - @hash = hash - @options = formatter.options - @inspector = formatter.inspector - @indentation = formatter.indentation - end + class Hash < Base def call - return "{}" if hash.empty? + return "{}" if object.empty? - keys = options[:sort_keys] ? hash.keys.sort { |a, b| a.to_s <=> b.to_s } : hash.keys + keys = options[:sort_keys] ? object.keys.sort { |a, b| a.to_s <=> b.to_s } : object.keys data = keys.map do |key| formatter.plain_single_line do - [ inspector.awesome(key), hash[key] ] + [ inspector.awesome(key), object[key] ] end end @@ -36,18 +28,6 @@ def call "{ #{data.join(', ')} }" end end - - private - - attr_reader :formatter, :hash, :indentation - - def options - @options - end - - def inspector - @inspector - end end end end From c2563be4315f8a56320110524a542a521858c926 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 4 Mar 2015 19:26:15 -0300 Subject: [PATCH 09/59] Refactoring on Hash formatter --- lib/awesome_print/formatters/hash.rb | 56 ++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/lib/awesome_print/formatters/hash.rb b/lib/awesome_print/formatters/hash.rb index 7a464b62..7360aa3b 100644 --- a/lib/awesome_print/formatters/hash.rb +++ b/lib/awesome_print/formatters/hash.rb @@ -3,31 +3,57 @@ module Formatters class Hash < Base def call - return "{}" if object.empty? + return empty_format if object.empty? + build_data + if options[:multiline] + multiline_format + else + inline_format + end + end - keys = options[:sort_keys] ? object.keys.sort { |a, b| a.to_s <=> b.to_s } : object.keys - data = keys.map do |key| - formatter.plain_single_line do - [ inspector.awesome(key), object[key] ] - end + private + + attr_reader :width, :data + + def empty_format + '{}' end - width = data.map { |key, | key.size }.max || 0 - width += indentation if options[:indent] > 0 + def keys + options[:sort_keys] ? object.keys.sort { |a, b| a.to_s <=> b.to_s } : object.keys + end + + def build_width + @width = @data.map { |key, | key.size }.max || 0 + @width += indentation if options[:indent] > 0 + end - data = data.map do |key, value| - formatter.indented do - formatter.align(key, width) << formatter.colorize(" => ", :hash) << inspector.awesome(value) + def build_data + @data = keys.map do |key| + formatter.plain_single_line do + [ inspector.awesome(key), object[key] ] + end end + + build_width + + @data = @data.map do |key, value| + formatter.indented do + formatter.align(key, width) << formatter.colorize(" => ", :hash) << inspector.awesome(value) + end + end + + @data = formatter.limited(@data, width, :hash => true) if formatter.should_be_limited? end - data = formatter.limited(data, width, :hash => true) if formatter.should_be_limited? - if options[:multiline] + def multiline_format "{\n" << data.join(",\n") << "\n#{formatter.outdent}}" - else + end + + def inline_format "{ #{data.join(', ')} }" end - end end end end From 9ad2ddbeae18e485159fadda19820956fbdb6275 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 5 Mar 2015 19:09:03 -0300 Subject: [PATCH 10/59] Extract Object Formmatter Moved from the Formatter to a single class that do the Object formatation. --- lib/awesome_print/formatter.rb | 60 ++++++------------------- lib/awesome_print/formatters/object.rb | 61 ++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 47 deletions(-) create mode 100644 lib/awesome_print/formatters/object.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 43798c4f..d6ad974e 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -4,6 +4,7 @@ require 'awesome_print/formatters/base' require 'awesome_print/formatters/array' require 'awesome_print/formatters/hash' +require 'awesome_print/formatters/object' module AwesomePrint class Formatter @@ -175,6 +176,17 @@ def align(value, width) end end + def left_aligned + current, @options[:indent] = @options[:indent], 0 + yield + ensure + @options[:indent] = current + end + + def awesome_instance(o) + "#{o.class}:0x%08x" % (o.__id__ * 2) + end + private # Catch all method to format an arbitrary object. @@ -204,41 +216,8 @@ def awesome_hash(h) # Format an object. #------------------------------------------------------------------------------ def awesome_object(o) - vars = o.instance_variables.map do |var| - property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet. - accessor = if o.respond_to?(:"#{property}=") - o.respond_to?(property) ? :accessor : :writer - else - o.respond_to?(property) ? :reader : nil - end - if accessor - [ "attr_#{accessor} :#{property}", var ] - else - [ var.to_s, var ] - end - end - - data = vars.sort.map do |declaration, var| - key = left_aligned do - align(declaration, declaration.size) - end + AwesomePrint::Formatters::Object.new(self, o).call - unless @options[:plain] - if key =~ /(@\w+)/ - key.sub!($1, colorize($1, :variable)) - else - key.sub!(/(attr_\w+)\s(\:\w+)/, "#{colorize('\\1', :keyword)} #{colorize('\\2', :method)}") - end - end - indented do - key << colorize(" = ", :hash) + @inspector.awesome(o.instance_variable_get(var)) - end - end - if @options[:multiline] - "#<#{awesome_instance(o)}\n#{data.join(%Q/,\n/)}\n#{outdent}>" - else - "#<#{awesome_instance(o)} #{data.join(', ')}>" - end end # Format a set. @@ -304,12 +283,6 @@ def awesome_method(m) end alias :awesome_unboundmethod :awesome_method - # Format object instance. - #------------------------------------------------------------------------------ - def awesome_instance(o) - "#{o.class}:0x%08x" % (o.__id__ * 2) - end - # Return [ name, arguments, owner ] tuple for a given method. #------------------------------------------------------------------------------ def method_tuple(method) @@ -367,13 +340,6 @@ def convert_to_hash(object) return hash end - def left_aligned - current, @options[:indent] = @options[:indent], 0 - yield - ensure - @options[:indent] = current - end - def get_limit_size @options[:limit] == true ? DEFAULT_LIMIT_SIZE : @options[:limit] end diff --git a/lib/awesome_print/formatters/object.rb b/lib/awesome_print/formatters/object.rb new file mode 100644 index 00000000..b9f2dd7b --- /dev/null +++ b/lib/awesome_print/formatters/object.rb @@ -0,0 +1,61 @@ +module AwesomePrint + module Formatters + class Object < Base + + def call + build_vars + build_data + if options[:multiline] + inline_format + else + multiline_format + end + end + + private + + def build_vars + @vars = object.instance_variables.map do |var| + property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet. + accessor = if object.respond_to?(:"#{property}=") + object.respond_to?(property) ? :accessor : :writer + else + object.respond_to?(property) ? :reader : nil + end + if accessor + [ "attr_#{accessor} :#{property}", var ] + else + [ var.to_s, var ] + end + end + end + + def build_data + @data = @vars.sort.map do |declaration, var| + key = formatter.left_aligned do + formatter.align(declaration, declaration.size) + end + + unless options[:plain] + if key =~ /(@\w+)/ + key.sub!($1, formatter.colorize($1, :variable)) + else + key.sub!(/(attr_\w+)\s(\:\w+)/, "#{formatter.colorize('\\1', :keyword)} #{formatter.colorize('\\2', :method)}") + end + end + formatter.indented do + key << formatter.colorize(" = ", :hash) + inspector.awesome(object.instance_variable_get(var)) + end + end + end + + def inline_format + "#<#{formatter.awesome_instance(object)}\n#{@data.join(%Q/,\n/)}\n#{formatter.outdent}>" + end + + def multiline_format + "#<#{formatter.awesome_instance(object)} #{@data.join(', ')}>" + end + end + end +end From 708a79c27e42eb5ec12bc555bbdd074b74c100bf Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 11 Mar 2015 18:59:30 -0300 Subject: [PATCH 11/59] Extract Set Formatter --- lib/awesome_print/formatter.rb | 4 ++-- lib/awesome_print/formatters/set.rb | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 lib/awesome_print/formatters/set.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index d6ad974e..1a8d8153 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -5,6 +5,7 @@ require 'awesome_print/formatters/array' require 'awesome_print/formatters/hash' require 'awesome_print/formatters/object' +require 'awesome_print/formatters/set' module AwesomePrint class Formatter @@ -217,13 +218,12 @@ def awesome_hash(h) #------------------------------------------------------------------------------ def awesome_object(o) AwesomePrint::Formatters::Object.new(self, o).call - end # Format a set. #------------------------------------------------------------------------------ def awesome_set(s) - awesome_array(s.to_a) + AwesomePrint::Formatters::Set.new(self, s).call end # Format a Struct. diff --git a/lib/awesome_print/formatters/set.rb b/lib/awesome_print/formatters/set.rb new file mode 100644 index 00000000..ea5d6251 --- /dev/null +++ b/lib/awesome_print/formatters/set.rb @@ -0,0 +1,10 @@ +module AwesomePrint + module Formatters + class Set < Base + + def call + AwesomePrint::Formatters::Array.new(formatter, object.to_a).call + end + end + end +end From ffa2e304a167e358ad99927df419c4ff65f0a41b Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 11 Mar 2015 19:03:59 -0300 Subject: [PATCH 12/59] Extract Struct Formatter --- lib/awesome_print/formatter.rb | 10 ++-------- lib/awesome_print/formatters/struct.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 lib/awesome_print/formatters/struct.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 1a8d8153..a85ed82e 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -6,6 +6,7 @@ require 'awesome_print/formatters/hash' require 'awesome_print/formatters/object' require 'awesome_print/formatters/set' +require 'awesome_print/formatters/struct' module AwesomePrint class Formatter @@ -229,14 +230,7 @@ def awesome_set(s) # Format a Struct. #------------------------------------------------------------------------------ def awesome_struct(s) - # - # The code is slightly uglier because of Ruby 1.8.6 quirks: - # awesome_hash(Hash[s.members.zip(s.values)]) <-- ArgumentError: odd number of arguments for Hash) - # awesome_hash(Hash[*s.members.zip(s.values).flatten]) <-- s.members returns strings, not symbols. - # - hash = {} - s.each_pair { |key, value| hash[key] = value } - awesome_hash(hash) + AwesomePrint::Formatters::Struct.new(self, s).call end # Format Class object. diff --git a/lib/awesome_print/formatters/struct.rb b/lib/awesome_print/formatters/struct.rb new file mode 100644 index 00000000..39f05b1f --- /dev/null +++ b/lib/awesome_print/formatters/struct.rb @@ -0,0 +1,16 @@ +module AwesomePrint + module Formatters + class Struct < Base + # + # The code is slightly uglier because of Ruby 1.8.6 quirks: + # awesome_hash(Hash[s.members.zip(s.values)]) <-- ArgumentError: odd number of arguments for Hash) + # awesome_hash(Hash[*s.members.zip(s.values).flatten]) <-- s.members returns strings, not symbols. + # + def call + hash = {} + object.each_pair { |key, value| hash[key] = value } + AwesomePrint::Formatters::Hash.new(formatter, hash).call + end + end + end +end From 3470b727904462513ef5726fb33abd12d38ccd78 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 11 Mar 2015 19:07:20 -0300 Subject: [PATCH 13/59] Extract Class Formatter --- lib/awesome_print/formatter.rb | 7 ++----- lib/awesome_print/formatters/class.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 lib/awesome_print/formatters/class.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index a85ed82e..680d8fca 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -7,6 +7,7 @@ require 'awesome_print/formatters/object' require 'awesome_print/formatters/set' require 'awesome_print/formatters/struct' +require 'awesome_print/formatters/class' module AwesomePrint class Formatter @@ -236,11 +237,7 @@ def awesome_struct(s) # Format Class object. #------------------------------------------------------------------------------ def awesome_class(c) - if superclass = c.superclass # <-- Assign and test if nil. - colorize("#{c.inspect} < #{superclass}", :class) - else - colorize(c.inspect, :class) - end + AwesomePrint::Formatters::Class.new(self, c).call end # Format File object. diff --git a/lib/awesome_print/formatters/class.rb b/lib/awesome_print/formatters/class.rb new file mode 100644 index 00000000..6c533338 --- /dev/null +++ b/lib/awesome_print/formatters/class.rb @@ -0,0 +1,14 @@ +module AwesomePrint + module Formatters + class Class < Base + + def call + if superclass = object.superclass # <-- Assign and test if nil. + formatter.colorize("#{object.inspect} < #{superclass}", :class) + else + formatter.colorize(object.inspect, :class) + end + end + end + end +end From db32b9044a9aada7841fd858d312d8f11050eb1d Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 11 Mar 2015 19:11:33 -0300 Subject: [PATCH 14/59] Extract File Formatter --- lib/awesome_print/formatter.rb | 4 ++-- lib/awesome_print/formatters/file.rb | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 lib/awesome_print/formatters/file.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 680d8fca..a71046bd 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -8,6 +8,7 @@ require 'awesome_print/formatters/set' require 'awesome_print/formatters/struct' require 'awesome_print/formatters/class' +require 'awesome_print/formatters/file' module AwesomePrint class Formatter @@ -243,8 +244,7 @@ def awesome_class(c) # Format File object. #------------------------------------------------------------------------------ def awesome_file(f) - ls = File.directory?(f) ? `ls -adlF #{f.path.shellescape}` : `ls -alF #{f.path.shellescape}` - colorize(ls.empty? ? f.inspect : "#{f.inspect}\n#{ls.chop}", :file) + AwesomePrint::Formatters::File.new(self, f).call end # Format Dir object. diff --git a/lib/awesome_print/formatters/file.rb b/lib/awesome_print/formatters/file.rb new file mode 100644 index 00000000..222021a0 --- /dev/null +++ b/lib/awesome_print/formatters/file.rb @@ -0,0 +1,11 @@ +module AwesomePrint + module Formatters + class File < Base + + def call + ls = ::File.directory?(object) ? `ls -adlF #{object.path.shellescape}` : `ls -alF #{object.path.shellescape}` + formatter.colorize(ls.empty? ? object.inspect : "#{object.inspect}\n#{ls.chop}", :file) + end + end + end +end From 3e0e1243bdfc2069991d72b32a0cbf27db151b38 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 11 Mar 2015 19:14:36 -0300 Subject: [PATCH 15/59] Extract Dir Formatter --- lib/awesome_print/formatter.rb | 4 ++-- lib/awesome_print/formatters/dir.rb | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 lib/awesome_print/formatters/dir.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index a71046bd..98ac48e8 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -9,6 +9,7 @@ require 'awesome_print/formatters/struct' require 'awesome_print/formatters/class' require 'awesome_print/formatters/file' +require 'awesome_print/formatters/dir' module AwesomePrint class Formatter @@ -250,8 +251,7 @@ def awesome_file(f) # Format Dir object. #------------------------------------------------------------------------------ def awesome_dir(d) - ls = `ls -alF #{d.path.shellescape}` - colorize(ls.empty? ? d.inspect : "#{d.inspect}\n#{ls.chop}", :dir) + AwesomePrint::Formatters::Dir.new(self, d).call end # Format BigDecimal object. diff --git a/lib/awesome_print/formatters/dir.rb b/lib/awesome_print/formatters/dir.rb new file mode 100644 index 00000000..383bf4f5 --- /dev/null +++ b/lib/awesome_print/formatters/dir.rb @@ -0,0 +1,11 @@ +module AwesomePrint + module Formatters + class Dir < Base + + def call + ls = `ls -alF #{object.path.shellescape}` + formatter.colorize(ls.empty? ? object.inspect : "#{object.inspect}\n#{ls.chop}", :dir) + end + end + end +end From 2d786cf42e6578090bbd23bf4d8ca916de8642b9 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 11 Mar 2015 19:18:01 -0300 Subject: [PATCH 16/59] Extract BigDecimal Formatter --- lib/awesome_print/formatter.rb | 3 ++- lib/awesome_print/formatters/big_decimal.rb | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 lib/awesome_print/formatters/big_decimal.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 98ac48e8..cc744e90 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -10,6 +10,7 @@ require 'awesome_print/formatters/class' require 'awesome_print/formatters/file' require 'awesome_print/formatters/dir' +require 'awesome_print/formatters/big_decimal' module AwesomePrint class Formatter @@ -257,7 +258,7 @@ def awesome_dir(d) # Format BigDecimal object. #------------------------------------------------------------------------------ def awesome_bigdecimal(n) - colorize(n.to_s("F"), :bigdecimal) + AwesomePrint::Formatters::BigDecimal.new(self, n).call end # Format Rational object. diff --git a/lib/awesome_print/formatters/big_decimal.rb b/lib/awesome_print/formatters/big_decimal.rb new file mode 100644 index 00000000..ef947ead --- /dev/null +++ b/lib/awesome_print/formatters/big_decimal.rb @@ -0,0 +1,11 @@ +module AwesomePrint + module Formatters + class BigDecimal< Base + + def call + formatter.colorize(object.to_s('F'), :bigdecimal) + end + end + end +end + From 6ec149dcef4bedcd8f5d1fed66634a379f8a44ed Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 11 Mar 2015 19:20:28 -0300 Subject: [PATCH 17/59] Extract Rational Formatter --- lib/awesome_print/formatter.rb | 3 ++- lib/awesome_print/formatters/big_decimal.rb | 2 +- lib/awesome_print/formatters/rational.rb | 11 +++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 lib/awesome_print/formatters/rational.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index cc744e90..ad0f9662 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -11,6 +11,7 @@ require 'awesome_print/formatters/file' require 'awesome_print/formatters/dir' require 'awesome_print/formatters/big_decimal' +require 'awesome_print/formatters/rational' module AwesomePrint class Formatter @@ -264,7 +265,7 @@ def awesome_bigdecimal(n) # Format Rational object. #------------------------------------------------------------------------------ def awesome_rational(n) - colorize(n.to_s, :rational) + AwesomePrint::Formatters::Rational.new(self, n).call end # Format a method. diff --git a/lib/awesome_print/formatters/big_decimal.rb b/lib/awesome_print/formatters/big_decimal.rb index ef947ead..452fdd9a 100644 --- a/lib/awesome_print/formatters/big_decimal.rb +++ b/lib/awesome_print/formatters/big_decimal.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class BigDecimal< Base + class BigDecimal < Base def call formatter.colorize(object.to_s('F'), :bigdecimal) diff --git a/lib/awesome_print/formatters/rational.rb b/lib/awesome_print/formatters/rational.rb new file mode 100644 index 00000000..9727c8a4 --- /dev/null +++ b/lib/awesome_print/formatters/rational.rb @@ -0,0 +1,11 @@ +module AwesomePrint + module Formatters + class Rational < Base + + def call + formatter.colorize(object.to_s, :rational) + end + end + end +end + From 2cbdbc8e10696afceca2c94b37953c6ad87748bc Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 11 Mar 2015 19:29:06 -0300 Subject: [PATCH 18/59] Move Formatter#methods_array to Array formatter --- lib/awesome_print/formatter.rb | 112 +++++++++----------------- lib/awesome_print/formatters/array.rb | 36 ++++++++- 2 files changed, 74 insertions(+), 74 deletions(-) diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index ad0f9662..b0c438ba 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -125,40 +125,6 @@ def limited(data, width, is_hash = false) end end - # Format object.methods array. - #------------------------------------------------------------------------------ - def methods_array(a) - a.sort! { |x, y| x.to_s <=> y.to_s } # Can't simply a.sort! because of o.methods << [ :blah ] - object = a.instance_variable_get('@__awesome_methods__') - tuples = a.map do |name| - if name.is_a?(Symbol) || name.is_a?(String) # Ignore garbage, ex. 42.methods << [ :blah ] - tuple = if object.respond_to?(name, true) # Is this a regular method? - the_method = object.method(name) rescue nil # Avoid potential ArgumentError if object#method is overridden. - if the_method && the_method.respond_to?(:arity) # Is this original object#method? - method_tuple(the_method) # Yes, we are good. - end - elsif object.respond_to?(:instance_method) # Is this an unbound method? - method_tuple(object.instance_method(name)) rescue nil # Rescue to avoid NameError when the method is not - end # available (ex. File.lchmod on Ubuntu 12). - end - tuple || [ name.to_s, '(?)', '?' ] # Return WTF default if all the above fails. - end - - width = (tuples.size - 1).to_s.size - name_width = tuples.map { |item| item[0].size }.max || 0 - args_width = tuples.map { |item| item[1].size }.max || 0 - - data = tuples.inject([]) do |arr, item| - index = indent - index << "[#{arr.size.to_s.rjust(width)}]" if @options[:index] - indented do - arr << "#{index} #{colorize(item[0].rjust(name_width), :method)}#{colorize(item[1].ljust(args_width), :args)} #{colorize(item[2], :class)}" - end - end - - "[\n" << data.join("\n") << "\n#{outdent}]" - end - # Format hash keys as plain strings regardless of underlying data type. #------------------------------------------------------------------------------ def plain_single_line @@ -194,6 +160,45 @@ def awesome_instance(o) "#{o.class}:0x%08x" % (o.__id__ * 2) end + # Return [ name, arguments, owner ] tuple for a given method. + #------------------------------------------------------------------------------ + def method_tuple(method) + if method.respond_to?(:parameters) # Ruby 1.9.2+ + # See http://ruby.runpaint.org/methods#method-objects-parameters + args = method.parameters.inject([]) do |arr, (type, name)| + name ||= (type == :block ? 'block' : "arg#{arr.size + 1}") + arr << case type + when :req then name.to_s + when :opt, :rest then "*#{name}" + when :block then "&#{name}" + else '?' + end + end + else # See http://ruby-doc.org/core/classes/Method.html#M001902 + args = (1..method.arity.abs).map { |i| "arg#{i}" } + args[-1] = "*#{args[-1]}" if method.arity < 0 + end + + # method.to_s formats to handle: + # + # # + # # + # #)#_username> + # # + # # + # # + # + if method.to_s =~ /(Unbound)*Method: (.*)[#\.]/ + unbound, klass = $1 && '(unbound)', $2 + if klass && klass =~ /(\(\w+:\s.*?\))/ # Is this ActiveRecord-style class? + klass.sub!($1, '') # Yes, strip the fields leaving class name only. + end + owner = "#{klass}#{unbound}".gsub('(', ' (') + end + + [ method.name.to_s, "(#{args.join(', ')})", owner.to_s ] + end + private # Catch all method to format an arbitrary object. @@ -276,45 +281,6 @@ def awesome_method(m) end alias :awesome_unboundmethod :awesome_method - # Return [ name, arguments, owner ] tuple for a given method. - #------------------------------------------------------------------------------ - def method_tuple(method) - if method.respond_to?(:parameters) # Ruby 1.9.2+ - # See http://ruby.runpaint.org/methods#method-objects-parameters - args = method.parameters.inject([]) do |arr, (type, name)| - name ||= (type == :block ? 'block' : "arg#{arr.size + 1}") - arr << case type - when :req then name.to_s - when :opt, :rest then "*#{name}" - when :block then "&#{name}" - else '?' - end - end - else # See http://ruby-doc.org/core/classes/Method.html#M001902 - args = (1..method.arity.abs).map { |i| "arg#{i}" } - args[-1] = "*#{args[-1]}" if method.arity < 0 - end - - # method.to_s formats to handle: - # - # # - # # - # #)#_username> - # # - # # - # # - # - if method.to_s =~ /(Unbound)*Method: (.*)[#\.]/ - unbound, klass = $1 && '(unbound)', $2 - if klass && klass =~ /(\(\w+:\s.*?\))/ # Is this ActiveRecord-style class? - klass.sub!($1, '') # Yes, strip the fields leaving class name only. - end - owner = "#{klass}#{unbound}".gsub('(', ' (') - end - - [ method.name.to_s, "(#{args.join(', ')})", owner.to_s ] - end - # Utility methods. #------------------------------------------------------------------------------ def convert_to_hash(object) diff --git a/lib/awesome_print/formatters/array.rb b/lib/awesome_print/formatters/array.rb index 10c146b0..5905978b 100644 --- a/lib/awesome_print/formatters/array.rb +++ b/lib/awesome_print/formatters/array.rb @@ -21,7 +21,7 @@ def empty_format end def methods_format - formatter.methods_array(object) + methods_array(object) end def inline_format @@ -42,6 +42,40 @@ def multiline_format data = formatter.limited(data, width) if formatter.should_be_limited? "[\n" << data.join(",\n") << "\n#{formatter.outdent}]" end + + # Format object.methods array. + #------------------------------------------------------------------------------ + def methods_array(a) + a.sort! { |x, y| x.to_s <=> y.to_s } # Can't simply a.sort! because of o.methods << [ :blah ] + object = a.instance_variable_get('@__awesome_methods__') + tuples = a.map do |name| + if name.is_a?(Symbol) || name.is_a?(String) # Ignore garbage, ex. 42.methods << [ :blah ] + tuple = if object.respond_to?(name, true) # Is this a regular method? + the_method = object.method(name) rescue nil # Avoid potential ArgumentError if object#method is overridden. + if the_method && the_method.respond_to?(:arity) # Is this original object#method? + formatter.method_tuple(the_method) # Yes, we are good. + end + elsif object.respond_to?(:instance_method) # Is this an unbound method? + formatter.method_tuple(object.instance_method(name)) rescue nil # Rescue to avoid NameError when the method is not + end # available (ex. File.lchmod on Ubuntu 12). + end + tuple || [ name.to_s, '(?)', '?' ] # Return WTF default if all the above fails. + end + + width = (tuples.size - 1).to_s.size + name_width = tuples.map { |item| item[0].size }.max || 0 + args_width = tuples.map { |item| item[1].size }.max || 0 + + data = tuples.inject([]) do |arr, item| + index = formatter.indent + index << "[#{arr.size.to_s.rjust(width)}]" if options[:index] + formatter.indented do + arr << "#{index} #{formatter.colorize(item[0].rjust(name_width), :method)}#{formatter.colorize(item[1].ljust(args_width), :args)} #{formatter.colorize(item[2], :class)}" + end + end + + "[\n" << data.join("\n") << "\n#{formatter.outdent}]" + end end end end From 040f129dcc72b2a1911b08e57b0c6b911f887540 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 11 Mar 2015 19:33:35 -0300 Subject: [PATCH 19/59] Extract Method Formatter --- lib/awesome_print/formatter.rb | 4 ++-- lib/awesome_print/formatters/method.rb | 11 +++++++++++ lib/awesome_print/formatters/rational.rb | 1 - 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 lib/awesome_print/formatters/method.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index b0c438ba..9d527323 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -12,6 +12,7 @@ require 'awesome_print/formatters/dir' require 'awesome_print/formatters/big_decimal' require 'awesome_print/formatters/rational' +require 'awesome_print/formatters/method' module AwesomePrint class Formatter @@ -276,8 +277,7 @@ def awesome_rational(n) # Format a method. #------------------------------------------------------------------------------ def awesome_method(m) - name, args, owner = method_tuple(m) - "#{colorize(owner, :class)}##{colorize(name, :method)}#{colorize(args, :args)}" + AwesomePrint::Formatters::Method.new(self, m).call end alias :awesome_unboundmethod :awesome_method diff --git a/lib/awesome_print/formatters/method.rb b/lib/awesome_print/formatters/method.rb new file mode 100644 index 00000000..40ba8b40 --- /dev/null +++ b/lib/awesome_print/formatters/method.rb @@ -0,0 +1,11 @@ +module AwesomePrint + module Formatters + class Method < Base + + def call + name, args, owner = formatter.method_tuple(object) + "#{formatter.colorize(owner, :class)}##{formatter.colorize(name, :method)}#{formatter.colorize(args, :args)}" + end + end + end +end diff --git a/lib/awesome_print/formatters/rational.rb b/lib/awesome_print/formatters/rational.rb index 9727c8a4..dea9cbff 100644 --- a/lib/awesome_print/formatters/rational.rb +++ b/lib/awesome_print/formatters/rational.rb @@ -8,4 +8,3 @@ def call end end end - From d7fe4c8e66ab94ad0d7db1c34b1d2e3c6d3b3eb4 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 11 Mar 2015 19:40:01 -0300 Subject: [PATCH 20/59] Move some Formatter methods to their specific formatter --- lib/awesome_print/formatter.rb | 14 -------------- lib/awesome_print/formatters/hash.rb | 10 +++++++++- lib/awesome_print/formatters/object.rb | 8 ++++++-- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 9d527323..4f8ac534 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -126,16 +126,6 @@ def limited(data, width, is_hash = false) end end - # Format hash keys as plain strings regardless of underlying data type. - #------------------------------------------------------------------------------ - def plain_single_line - plain, multiline = @options[:plain], @options[:multiline] - @options[:plain], @options[:multiline] = true, false - yield - ensure - @options[:plain], @options[:multiline] = plain, multiline - end - def align(value, width) if @options[:multiline] if @options[:indent] > 0 @@ -157,10 +147,6 @@ def left_aligned @options[:indent] = current end - def awesome_instance(o) - "#{o.class}:0x%08x" % (o.__id__ * 2) - end - # Return [ name, arguments, owner ] tuple for a given method. #------------------------------------------------------------------------------ def method_tuple(method) diff --git a/lib/awesome_print/formatters/hash.rb b/lib/awesome_print/formatters/hash.rb index 7360aa3b..edba88e8 100644 --- a/lib/awesome_print/formatters/hash.rb +++ b/lib/awesome_print/formatters/hash.rb @@ -31,7 +31,7 @@ def build_width def build_data @data = keys.map do |key| - formatter.plain_single_line do + plain_single_line do [ inspector.awesome(key), object[key] ] end end @@ -54,6 +54,14 @@ def multiline_format def inline_format "{ #{data.join(', ')} }" end + + def plain_single_line + plain, multiline = options[:plain], options[:multiline] + options[:plain], options[:multiline] = true, false + yield + ensure + options[:plain], options[:multiline] = plain, multiline + end end end end diff --git a/lib/awesome_print/formatters/object.rb b/lib/awesome_print/formatters/object.rb index b9f2dd7b..f609fb59 100644 --- a/lib/awesome_print/formatters/object.rb +++ b/lib/awesome_print/formatters/object.rb @@ -50,11 +50,15 @@ def build_data end def inline_format - "#<#{formatter.awesome_instance(object)}\n#{@data.join(%Q/,\n/)}\n#{formatter.outdent}>" + "#<#{awesome_instance(object)}\n#{@data.join(%Q/,\n/)}\n#{formatter.outdent}>" end def multiline_format - "#<#{formatter.awesome_instance(object)} #{@data.join(', ')}>" + "#<#{awesome_instance(object)} #{@data.join(', ')}>" + end + + def awesome_instance(o) + "#{o.class}:0x%08x" % (o.__id__ * 2) end end end From 4b9f0bf9351d462732d6b7a00596ab9665456052 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 12 Mar 2015 18:45:44 -0300 Subject: [PATCH 21/59] Extract specific methods of array and a hash Since we have some methods on formatter that is used only on the array and hash moved to a module. --- lib/awesome_print/formatter.rb | 50 ------------------- lib/awesome_print/formatters/array.rb | 5 +- lib/awesome_print/formatters/enumerable.rb | 58 ++++++++++++++++++++++ lib/awesome_print/formatters/hash.rb | 5 +- 4 files changed, 66 insertions(+), 52 deletions(-) create mode 100644 lib/awesome_print/formatters/enumerable.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 4f8ac534..fafe9b05 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -80,52 +80,6 @@ def indented @indentation -= @options[:indent].abs end - # To support limited output, for example: - # - # ap ('a'..'z').to_a, :limit => 3 - # [ - # [ 0] "a", - # [ 1] .. [24], - # [25] "z" - # ] - # - # ap (1..100).to_a, :limit => true # Default limit is 7. - # [ - # [ 0] 1, - # [ 1] 2, - # [ 2] 3, - # [ 3] .. [96], - # [97] 98, - # [98] 99, - # [99] 100 - # ] - #------------------------------------------------------------------------------ - def should_be_limited? - @options[:limit] == true or (@options[:limit].is_a?(Fixnum) and @options[:limit] > 0) - end - - def limited(data, width, is_hash = false) - limit = get_limit_size - if data.length <= limit - data - else - # Calculate how many elements to be displayed above and below the separator. - head = limit / 2 - tail = head - (limit - 1) % 2 - - # Add the proper elements to the temp array and format the separator. - temp = data[0, head] + [ nil ] + data[-tail, tail] - - if is_hash - temp[head] = "#{indent}#{data[head].strip} .. #{data[data.length - tail - 1].strip}" - else - temp[head] = "#{indent}[#{head.to_s.rjust(width)}] .. [#{data.length - tail - 1}]" - end - - temp - end - end - def align(value, width) if @options[:multiline] if @options[:indent] > 0 @@ -284,9 +238,5 @@ def convert_to_hash(object) return hash end - - def get_limit_size - @options[:limit] == true ? DEFAULT_LIMIT_SIZE : @options[:limit] - end end end diff --git a/lib/awesome_print/formatters/array.rb b/lib/awesome_print/formatters/array.rb index 5905978b..389e8d62 100644 --- a/lib/awesome_print/formatters/array.rb +++ b/lib/awesome_print/formatters/array.rb @@ -1,6 +1,9 @@ +require 'awesome_print/formatters/enumerable' + module AwesomePrint module Formatters class Array < Base + include Enumerable def call return empty_format if object.empty? @@ -39,7 +42,7 @@ def multiline_format end end - data = formatter.limited(data, width) if formatter.should_be_limited? + data = limited(data, width) if should_be_limited? "[\n" << data.join(",\n") << "\n#{formatter.outdent}]" end diff --git a/lib/awesome_print/formatters/enumerable.rb b/lib/awesome_print/formatters/enumerable.rb new file mode 100644 index 00000000..a5c96339 --- /dev/null +++ b/lib/awesome_print/formatters/enumerable.rb @@ -0,0 +1,58 @@ +module AwesomePrint + module Formatters + module Enumerable + + # To support limited output, for example: + # + # ap ('a'..'z').to_a, :limit => 3 + # [ + # [ 0] "a", + # [ 1] .. [24], + # [25] "z" + # ] + # + # ap (1..100).to_a, :limit => true # Default limit is 7. + # [ + # [ 0] 1, + # [ 1] 2, + # [ 2] 3, + # [ 3] .. [96], + # [97] 98, + # [98] 99, + # [99] 100 + # ] + #------------------------------------------------------------------------------ + def should_be_limited? + @options[:limit] == true or (@options[:limit].is_a?(Fixnum) and @options[:limit] > 0) + end + + # Mover para um modulo e incluir apenas no array e hash formatters + # fazer o mesmo com o should_be_limited? + def limited(data, width, is_hash = false) + limit = get_limit_size + if data.length <= limit + data + else + # Calculate how many elements to be displayed above and below the separator. + head = limit / 2 + tail = head - (limit - 1) % 2 + + # Add the proper elements to the temp array and format the separator. + temp = data[0, head] + [ nil ] + data[-tail, tail] + + if is_hash + temp[head] = "#{formatter.indent}#{data[head].strip} .. #{data[data.length - tail - 1].strip}" + else + temp[head] = "#{formatter.indent}[#{head.to_s.rjust(width)}] .. [#{data.length - tail - 1}]" + end + + temp + end + end + + def get_limit_size + options[:limit] == true ? AwesomePrint::Formatter::DEFAULT_LIMIT_SIZE : options[:limit] + end + end + end +end diff --git a/lib/awesome_print/formatters/hash.rb b/lib/awesome_print/formatters/hash.rb index edba88e8..ab34ccb7 100644 --- a/lib/awesome_print/formatters/hash.rb +++ b/lib/awesome_print/formatters/hash.rb @@ -1,6 +1,9 @@ +require 'awesome_print/formatters/enumerable' + module AwesomePrint module Formatters class Hash < Base + include Enumerable def call return empty_format if object.empty? @@ -44,7 +47,7 @@ def build_data end end - @data = formatter.limited(@data, width, :hash => true) if formatter.should_be_limited? + @data = limited(@data, width, :hash => true) if should_be_limited? end def multiline_format From 5a9da75f41238c017021dcc70d6ae2349d08c607 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 12 Mar 2015 19:26:03 -0300 Subject: [PATCH 22/59] Create ActiveRecordInstance formatter --- lib/awesome_print/ext/active_record.rb | 21 +--------- lib/awesome_print/formatter.rb | 1 + .../formatters/active_record_instance.rb | 38 +++++++++++++++++++ 3 files changed, 40 insertions(+), 20 deletions(-) create mode 100644 lib/awesome_print/formatters/active_record_instance.rb diff --git a/lib/awesome_print/ext/active_record.rb b/lib/awesome_print/ext/active_record.rb index 6a3dbebb..708bcb77 100644 --- a/lib/awesome_print/ext/active_record.rb +++ b/lib/awesome_print/ext/active_record.rb @@ -24,27 +24,8 @@ def cast_with_active_record(object, type) private - # Format ActiveRecord instance object. - # - # NOTE: by default only instance attributes (i.e. columns) are shown. To format - # ActiveRecord instance as regular object showing its instance variables and - # accessors use :raw => true option: - # - # ap record, :raw => true - # - #------------------------------------------------------------------------------ def awesome_active_record_instance(object) - return object.inspect if !defined?(::ActiveSupport::OrderedHash) - return awesome_object(object) if @options[:raw] - - data = object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name| - if object.has_attribute?(name) || object.new_record? - value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name) - hash[name.to_sym] = value - end - hash - end - "#{object} " << awesome_hash(data) + AwesomePrint::Formatters::ActiveRecordInstance.new(self, object).call end # Format ActiveRecord class object. diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index fafe9b05..6ee31dea 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -13,6 +13,7 @@ require 'awesome_print/formatters/big_decimal' require 'awesome_print/formatters/rational' require 'awesome_print/formatters/method' +require 'awesome_print/formatters/active_record_instance' module AwesomePrint class Formatter diff --git a/lib/awesome_print/formatters/active_record_instance.rb b/lib/awesome_print/formatters/active_record_instance.rb new file mode 100644 index 00000000..df0afd76 --- /dev/null +++ b/lib/awesome_print/formatters/active_record_instance.rb @@ -0,0 +1,38 @@ +module AwesomePrint + module Formatters + class ActiveRecordInstance < Base + + # Format ActiveRecord instance object. + # + # NOTE: by default only instance attributes (i.e. columns) are shown. To format + # ActiveRecord instance as regular object showing its instance variables and + # accessors use :raw => true option: + # + # ap record, :raw => true + # + #------------------------------------------------------------------------------ + def call + return object.inspect if !defined?(::ActiveSupport::OrderedHash) + return raw_format if options[:raw] + + "#{object} " << AwesomePrint::Formatters::Hash.new(formatter, columns).call + end + + private + + def raw_format + AwesomePrint::Formatters::Object.new(formatter, object).call + end + + def columns + object.class.column_names.inject(::ActiveSupport::OrderedHash.new) do |hash, name| + if object.has_attribute?(name) || object.new_record? + value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name) + hash[name.to_sym] = value + end + hash + end + end + end + end +end From e9a4c721aab229a72c2c91ebe83438e07c42817c Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 12 Mar 2015 19:39:48 -0300 Subject: [PATCH 23/59] Extract ActiveRecordClass Formatter --- lib/awesome_print/ext/active_record.rb | 11 +----- lib/awesome_print/formatter.rb | 1 + .../formatters/active_record_class.rb | 34 +++++++++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 lib/awesome_print/formatters/active_record_class.rb diff --git a/lib/awesome_print/ext/active_record.rb b/lib/awesome_print/ext/active_record.rb index 708bcb77..3111e5d5 100644 --- a/lib/awesome_print/ext/active_record.rb +++ b/lib/awesome_print/ext/active_record.rb @@ -28,17 +28,8 @@ def awesome_active_record_instance(object) AwesomePrint::Formatters::ActiveRecordInstance.new(self, object).call end - # Format ActiveRecord class object. - #------------------------------------------------------------------------------ def awesome_active_record_class(object) - return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:columns) || object.to_s == "ActiveRecord::Base" - return awesome_class(object) if object.respond_to?(:abstract_class?) && object.abstract_class? - - data = object.columns.inject(::ActiveSupport::OrderedHash.new) do |hash, c| - hash[c.name.to_sym] = c.type - hash - end - "class #{object} < #{object.superclass} " << awesome_hash(data) + AwesomePrint::Formatters::ActiveRecordClass.new(self, object).call end end end diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 6ee31dea..2fcb4b49 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -14,6 +14,7 @@ require 'awesome_print/formatters/rational' require 'awesome_print/formatters/method' require 'awesome_print/formatters/active_record_instance' +require 'awesome_print/formatters/active_record_class' module AwesomePrint class Formatter diff --git a/lib/awesome_print/formatters/active_record_class.rb b/lib/awesome_print/formatters/active_record_class.rb new file mode 100644 index 00000000..de5af6e9 --- /dev/null +++ b/lib/awesome_print/formatters/active_record_class.rb @@ -0,0 +1,34 @@ +module AwesomePrint + module Formatters + class ActiveRecordClass < Base + + def call + return object.inspect if not_a_active_record_class? + return class_format if object_is_abstract_class? + + "class #{object} < #{object.superclass} " << AwesomePrint::Formatters::Hash.new(formatter, columns).call + end + + private + + def class_format + AwesomePrint::Formatters::Class.new(formatter, object).call + end + + def object_is_abstract_class? + object.respond_to?(:abstract_class?) && object.abstract_class? + end + + def not_a_active_record_class? + !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:columns) || object.to_s == "ActiveRecord::Base" + end + + def columns + object.columns.inject(::ActiveSupport::OrderedHash.new) do |hash, c| + hash[c.name.to_sym] = c.type + hash + end + end + end + end +end From 2426de2899a267f1ffb7bece47297a50bfe55ede Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 12 Mar 2015 19:52:22 -0300 Subject: [PATCH 24/59] Create a AwesomePrint::Formatters to include all formatters --- lib/awesome_print/formatter.rb | 16 +--------------- lib/awesome_print/formatters.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 lib/awesome_print/formatters.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 2fcb4b49..47dc47d1 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -1,20 +1,6 @@ autoload :CGI, 'cgi' require 'shellwords' - -require 'awesome_print/formatters/base' -require 'awesome_print/formatters/array' -require 'awesome_print/formatters/hash' -require 'awesome_print/formatters/object' -require 'awesome_print/formatters/set' -require 'awesome_print/formatters/struct' -require 'awesome_print/formatters/class' -require 'awesome_print/formatters/file' -require 'awesome_print/formatters/dir' -require 'awesome_print/formatters/big_decimal' -require 'awesome_print/formatters/rational' -require 'awesome_print/formatters/method' -require 'awesome_print/formatters/active_record_instance' -require 'awesome_print/formatters/active_record_class' +require 'awesome_print/formatters' module AwesomePrint class Formatter diff --git a/lib/awesome_print/formatters.rb b/lib/awesome_print/formatters.rb new file mode 100644 index 00000000..19ec199f --- /dev/null +++ b/lib/awesome_print/formatters.rb @@ -0,0 +1,18 @@ +module AwesomePrint + module Formatters + require 'awesome_print/formatters/base' + require 'awesome_print/formatters/array' + require 'awesome_print/formatters/hash' + require 'awesome_print/formatters/object' + require 'awesome_print/formatters/set' + require 'awesome_print/formatters/struct' + require 'awesome_print/formatters/class' + require 'awesome_print/formatters/file' + require 'awesome_print/formatters/dir' + require 'awesome_print/formatters/big_decimal' + require 'awesome_print/formatters/rational' + require 'awesome_print/formatters/method' + require 'awesome_print/formatters/active_record_instance' + require 'awesome_print/formatters/active_record_class' + end +end From 2cffbaab9994908bd70d826ee78b75c1ad71c4e1 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Sat, 14 Mar 2015 14:33:13 -0300 Subject: [PATCH 25/59] Extract ActiveSupport Formatter --- lib/awesome_print/ext/active_support.rb | 4 ++-- lib/awesome_print/formatters.rb | 2 ++ lib/awesome_print/formatters/active_support_time.rb | 10 ++++++++++ .../formatters/hash_with_indifferent_access.rb | 10 ++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 lib/awesome_print/formatters/active_support_time.rb create mode 100644 lib/awesome_print/formatters/hash_with_indifferent_access.rb diff --git a/lib/awesome_print/ext/active_support.rb b/lib/awesome_print/ext/active_support.rb index ba2a730b..941c0048 100644 --- a/lib/awesome_print/ext/active_support.rb +++ b/lib/awesome_print/ext/active_support.rb @@ -21,13 +21,13 @@ def cast_with_active_support(object, type) # Format ActiveSupport::TimeWithZone as standard Time. #------------------------------------------------------------------------------ def awesome_active_support_time(object) - colorize(object.inspect, :time) + AwesomePrint::Formatters::ActiveSupportTime.new(self, object).call end # Format HashWithIndifferentAccess as standard Hash. #------------------------------------------------------------------------------ def awesome_hash_with_indifferent_access(object) - awesome_hash(object) + AwesomePrint::Formatters::HashWithIndifferentAccess.new(self, object).call end end end diff --git a/lib/awesome_print/formatters.rb b/lib/awesome_print/formatters.rb index 19ec199f..52e6882f 100644 --- a/lib/awesome_print/formatters.rb +++ b/lib/awesome_print/formatters.rb @@ -14,5 +14,7 @@ module Formatters require 'awesome_print/formatters/method' require 'awesome_print/formatters/active_record_instance' require 'awesome_print/formatters/active_record_class' + require 'awesome_print/formatters/active_support_time' + require 'awesome_print/formatters/hash_with_indifferent_access' end end diff --git a/lib/awesome_print/formatters/active_support_time.rb b/lib/awesome_print/formatters/active_support_time.rb new file mode 100644 index 00000000..b3ccce96 --- /dev/null +++ b/lib/awesome_print/formatters/active_support_time.rb @@ -0,0 +1,10 @@ +module AwesomePrint + module Formatters + class ActiveSupportTime < Base + + def call + formatter.colorize(object.inspect, :time) + end + end + end +end diff --git a/lib/awesome_print/formatters/hash_with_indifferent_access.rb b/lib/awesome_print/formatters/hash_with_indifferent_access.rb new file mode 100644 index 00000000..e988c090 --- /dev/null +++ b/lib/awesome_print/formatters/hash_with_indifferent_access.rb @@ -0,0 +1,10 @@ +module AwesomePrint + module Formatters + class HashWithIndifferentAccess < Base + + def call + AwesomePrint::Formatters::Hash.new(formatter, object).call + end + end + end +end From 556a1cc27e5926e7addb11d4062a630f8ac7a2ea Mon Sep 17 00:00:00 2001 From: Mauro George Date: Sat, 14 Mar 2015 14:52:17 -0300 Subject: [PATCH 26/59] Extract Mongoid formmatter --- lib/awesome_print/ext/mongoid.rb | 25 +++---------------- lib/awesome_print/formatters.rb | 3 +++ .../formatters/mongoid_bson_id.rb | 10 ++++++++ lib/awesome_print/formatters/mongoid_class.rb | 21 ++++++++++++++++ .../formatters/mongoid_document.rb | 24 ++++++++++++++++++ 5 files changed, 61 insertions(+), 22 deletions(-) create mode 100644 lib/awesome_print/formatters/mongoid_bson_id.rb create mode 100644 lib/awesome_print/formatters/mongoid_class.rb create mode 100644 lib/awesome_print/formatters/mongoid_document.rb diff --git a/lib/awesome_print/ext/mongoid.rb b/lib/awesome_print/ext/mongoid.rb index bd862027..ba6edc52 100644 --- a/lib/awesome_print/ext/mongoid.rb +++ b/lib/awesome_print/ext/mongoid.rb @@ -22,37 +22,18 @@ def cast_with_mongoid(object, type) cast end - # Format Mongoid class object. - #------------------------------------------------------------------------------ def awesome_mongoid_class(object) - return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields) - - data = object.fields.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c| - hash[c[1].name.to_sym] = (c[1].type || "undefined").to_s.underscore.intern - hash - end - "class #{object} < #{object.superclass} " << awesome_hash(data) + AwesomePrint::Formatters::MongoidClass.new(self, object).call end - # Format Mongoid Document object. - #------------------------------------------------------------------------------ def awesome_mongoid_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)}" + AwesomePrint::Formatters::MongoidDocument.new(self, object).call end # Format BSON::ObjectId #------------------------------------------------------------------------------ def awesome_mongoid_bson_id(object) - object.inspect + AwesomePrint::Formatters::MongoidBsonId.new(self, object).call end end end diff --git a/lib/awesome_print/formatters.rb b/lib/awesome_print/formatters.rb index 52e6882f..065a2fb1 100644 --- a/lib/awesome_print/formatters.rb +++ b/lib/awesome_print/formatters.rb @@ -16,5 +16,8 @@ module Formatters require 'awesome_print/formatters/active_record_class' require 'awesome_print/formatters/active_support_time' require 'awesome_print/formatters/hash_with_indifferent_access' + require 'awesome_print/formatters/mongoid_class' + require 'awesome_print/formatters/mongoid_document' + require 'awesome_print/formatters/mongoid_bson_id' end end diff --git a/lib/awesome_print/formatters/mongoid_bson_id.rb b/lib/awesome_print/formatters/mongoid_bson_id.rb new file mode 100644 index 00000000..7ec4862e --- /dev/null +++ b/lib/awesome_print/formatters/mongoid_bson_id.rb @@ -0,0 +1,10 @@ +module AwesomePrint + module Formatters + class MongoidBsonId < Base + + def call + object.inspect + end + end + end +end diff --git a/lib/awesome_print/formatters/mongoid_class.rb b/lib/awesome_print/formatters/mongoid_class.rb new file mode 100644 index 00000000..0607e785 --- /dev/null +++ b/lib/awesome_print/formatters/mongoid_class.rb @@ -0,0 +1,21 @@ +module AwesomePrint + module Formatters + class MongoidClass < Base + + def call + return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields) + + "class #{object} < #{object.superclass} " << AwesomePrint::Formatters::Hash.new(formatter, columns).call + end + + private + + def columns + object.fields.sort_by { |key| key }.inject(::ActiveSupport::OrderedHash.new) do |hash, c| + hash[c[1].name.to_sym] = (c[1].type || "undefined").to_s.underscore.intern + hash + end + end + end + end +end diff --git a/lib/awesome_print/formatters/mongoid_document.rb b/lib/awesome_print/formatters/mongoid_document.rb new file mode 100644 index 00000000..95662767 --- /dev/null +++ b/lib/awesome_print/formatters/mongoid_document.rb @@ -0,0 +1,24 @@ +module AwesomePrint + module Formatters + class MongoidDocument < Base + + def call + return object.inspect if !defined?(::ActiveSupport::OrderedHash) + "#{object} #{AwesomePrint::Formatters::Hash.new(formatter, columns).call}" + end + + private + + def columns + 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 + data + end + end + end +end From 437857b5587efdccda2b73316d9cad0bb545974d Mon Sep 17 00:00:00 2001 From: Mauro George Date: Sat, 14 Mar 2015 15:43:05 -0300 Subject: [PATCH 27/59] Extract MongoMapper formmatter --- lib/awesome_print/ext/mongo_mapper.rb | 71 ++----------------- lib/awesome_print/formatters.rb | 4 ++ lib/awesome_print/formatters/mongo_mapper.rb | 21 ++++++ .../formatters/mongo_mapper_association.rb | 17 +++++ .../formatters/mongo_mapper_bson_id.rb | 10 +++ .../formatters/mongo_mapper_class.rb | 32 +++++++++ .../formatters/mongo_mapper_instance.rb | 52 ++++++++++++++ 7 files changed, 140 insertions(+), 67 deletions(-) create mode 100644 lib/awesome_print/formatters/mongo_mapper.rb create mode 100644 lib/awesome_print/formatters/mongo_mapper_association.rb create mode 100644 lib/awesome_print/formatters/mongo_mapper_bson_id.rb create mode 100644 lib/awesome_print/formatters/mongo_mapper_class.rb create mode 100644 lib/awesome_print/formatters/mongo_mapper_instance.rb diff --git a/lib/awesome_print/ext/mongo_mapper.rb b/lib/awesome_print/ext/mongo_mapper.rb index 32461341..3cd9522b 100644 --- a/lib/awesome_print/ext/mongo_mapper.rb +++ b/lib/awesome_print/ext/mongo_mapper.rb @@ -9,7 +9,6 @@ def self.included(base) # Add MongoMapper class names to the dispatcher pipeline. #------------------------------------------------------------------------------ def cast_with_mongo_mapper(object, type) - apply_default_mongo_mapper_options cast = cast_without_mongo_mapper(object, type) if defined?(::MongoMapper::Document) @@ -30,85 +29,23 @@ def cast_with_mongo_mapper(object, type) # 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.inject(::ActiveSupport::OrderedHash.new) do |hash, c| - hash[c.first] = (c.last.type || "undefined").to_s.underscore.intern - hash - end - - # Add in associations - if @options[:mongo_mapper][:show_associations] - object.associations.each do |name, assoc| - data[name.to_s] = assoc - end - end - - "class #{object} < #{object.superclass} " << awesome_hash(data) + AwesomePrint::Formatters::MongoMapperClass.new(self, object).call end - # Format MongoMapper instance object. - # - # NOTE: by default only instance attributes (i.e. keys) are shown. To format - # MongoMapper instance as regular object showing its instance variables and - # accessors use :raw => true option: - # - # ap record, :raw => true - # - #------------------------------------------------------------------------------ def awesome_mongo_mapper_instance(object) - return object.inspect if !defined?(::ActiveSupport::OrderedHash) - return awesome_object(object) if @options[:raw] - - data = object.keys.keys.sort_by{|k| k}.inject(::ActiveSupport::OrderedHash.new) do |hash, name| - hash[name] = object[name] - hash - end - - # Add in associations - if @options[:mongo_mapper][:show_associations] - object.associations.each do |name, assoc| - if @options[:mongo_mapper][:inline_embedded] and assoc.embeddable? - data[name.to_s] = object.send(name) - else - data[name.to_s] = assoc - end - end - end - - label = object.to_s - label = "#{colorize('embedded', :assoc)} #{label}" if object.is_a?(::MongoMapper::EmbeddedDocument) - - "#{label} " << awesome_hash(data) + AwesomePrint::Formatters::MongoMapperInstance.new(self, object).call end # Format MongoMapper association object. #------------------------------------------------------------------------------ def awesome_mongo_mapper_association(object) - return object.inspect if !defined?(::ActiveSupport::OrderedHash) - return awesome_object(object) if @options[:raw] - - association = object.class.name.split('::').last.titleize.downcase.sub(/ association$/,'') - association = "embeds #{association}" if object.embeddable? - class_name = object.class_name - - "#{colorize(association, :assoc)} #{colorize(class_name, :class)}" + AwesomePrint::Formatters::MongoMapperAssociation.new(self, object).call end # Format BSON::ObjectId #------------------------------------------------------------------------------ def awesome_mongo_mapper_bson_id(object) - object.inspect - end - - private - - def apply_default_mongo_mapper_options - @options[:color][:assoc] ||= :greenish - @options[:mongo_mapper] ||= { - :show_associations => false, # Display association data for MongoMapper documents and classes. - :inline_embedded => false # Display embedded associations inline with MongoMapper documents. - } + AwesomePrint::Formatters::MongoMapperBsonId.new(self, object).call end end end diff --git a/lib/awesome_print/formatters.rb b/lib/awesome_print/formatters.rb index 065a2fb1..8ada908e 100644 --- a/lib/awesome_print/formatters.rb +++ b/lib/awesome_print/formatters.rb @@ -19,5 +19,9 @@ module Formatters require 'awesome_print/formatters/mongoid_class' require 'awesome_print/formatters/mongoid_document' require 'awesome_print/formatters/mongoid_bson_id' + require 'awesome_print/formatters/mongo_mapper_instance' + require 'awesome_print/formatters/mongo_mapper_class' + require 'awesome_print/formatters/mongo_mapper_association' + require 'awesome_print/formatters/mongo_mapper_bson_id' end end diff --git a/lib/awesome_print/formatters/mongo_mapper.rb b/lib/awesome_print/formatters/mongo_mapper.rb new file mode 100644 index 00000000..1ecacd66 --- /dev/null +++ b/lib/awesome_print/formatters/mongo_mapper.rb @@ -0,0 +1,21 @@ +module AwesomePrint + module Formatters + module MongoMapper + + def initialize(*args) + super(*args) + apply_default_mongo_mapper_options + end + + private + + def apply_default_mongo_mapper_options + @options[:color][:assoc] ||= :greenish + @options[:mongo_mapper] ||= { + :show_associations => false, # Display association data for MongoMapper documents and classes. + :inline_embedded => false # Display embedded associations inline with MongoMapper documents. + } + end + end + end +end diff --git a/lib/awesome_print/formatters/mongo_mapper_association.rb b/lib/awesome_print/formatters/mongo_mapper_association.rb new file mode 100644 index 00000000..a37f7b50 --- /dev/null +++ b/lib/awesome_print/formatters/mongo_mapper_association.rb @@ -0,0 +1,17 @@ +module AwesomePrint + module Formatters + class MongoMapperAssociation < Base + + def call + return object.inspect if !defined?(::ActiveSupport::OrderedHash) + return awesome_object(object) if @options[:raw] + + association = object.class.name.split('::').last.titleize.downcase.sub(/ association$/,'') + association = "embeds #{association}" if object.embeddable? + class_name = object.class_name + + "#{formatter.colorize(association, :assoc)} #{formatter.colorize(class_name, :class)}" + end + end + end +end diff --git a/lib/awesome_print/formatters/mongo_mapper_bson_id.rb b/lib/awesome_print/formatters/mongo_mapper_bson_id.rb new file mode 100644 index 00000000..360ff288 --- /dev/null +++ b/lib/awesome_print/formatters/mongo_mapper_bson_id.rb @@ -0,0 +1,10 @@ +module AwesomePrint + module Formatters + class MongoMapperBsonId < Base + + def call + object.inspect + end + end + end +end diff --git a/lib/awesome_print/formatters/mongo_mapper_class.rb b/lib/awesome_print/formatters/mongo_mapper_class.rb new file mode 100644 index 00000000..d890a0ac --- /dev/null +++ b/lib/awesome_print/formatters/mongo_mapper_class.rb @@ -0,0 +1,32 @@ +require 'awesome_print/formatters/mongo_mapper' + +module AwesomePrint + module Formatters + class MongoMapperClass < Base + include MongoMapper + + def call + return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:keys) + + "class #{object} < #{object.superclass} " << AwesomePrint::Formatters::Hash.new(formatter, columns).call + end + + private + + def columns + data = object.keys.sort.inject(::ActiveSupport::OrderedHash.new) do |hash, c| + hash[c.first] = (c.last.type || "undefined").to_s.underscore.intern + hash + end + + # Add in associations + if options[:mongo_mapper][:show_associations] + object.associations.each do |name, assoc| + data[name.to_s] = assoc + end + end + data + end + end + end +end diff --git a/lib/awesome_print/formatters/mongo_mapper_instance.rb b/lib/awesome_print/formatters/mongo_mapper_instance.rb new file mode 100644 index 00000000..60aee52a --- /dev/null +++ b/lib/awesome_print/formatters/mongo_mapper_instance.rb @@ -0,0 +1,52 @@ +require 'awesome_print/formatters/mongo_mapper' + +module AwesomePrint + module Formatters + class MongoMapperInstance < Base + include MongoMapper + + # Format MongoMapper instance object. + # + # NOTE: by default only instance attributes (i.e. keys) are shown. To format + # MongoMapper instance as regular object showing its instance variables and + # accessors use :raw => true option: + # + # ap record, :raw => true + # + #------------------------------------------------------------------------------ + def call + return object.inspect if !defined?(::ActiveSupport::OrderedHash) + return AwesomePrint::Formatters::Object.new(formatter, object).call if options[:raw] + + "#{label} " << AwesomePrint::Formatters::Hash.new(formatter, columns).call + end + + private + + def label + label = object.to_s + label = "#{formatter.colorize('embedded', :assoc)} #{label}" if object.is_a?(::MongoMapper::EmbeddedDocument) + label + end + + def columns + data = object.keys.keys.sort_by{|k| k}.inject(::ActiveSupport::OrderedHash.new) do |hash, name| + hash[name] = object[name] + hash + end + + # Add in associations + if options[:mongo_mapper][:show_associations] + object.associations.each do |name, assoc| + if options[:mongo_mapper][:inline_embedded] and assoc.embeddable? + data[name.to_s] = object.send(name) + else + data[name.to_s] = assoc + end + end + end + data + end + end + end +end From 8696ec2db94450a31038dd356f04138158f29ac5 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Sat, 14 Mar 2015 16:18:23 -0300 Subject: [PATCH 28/59] Extract NoBrainer formmatter --- lib/awesome_print/ext/nobrainer.rb | 11 ++-------- lib/awesome_print/formatters.rb | 2 ++ .../formatters/nobrainer_class.rb | 18 +++++++++++++++++ .../formatters/nobrainer_document.rb | 20 +++++++++++++++++++ 4 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 lib/awesome_print/formatters/nobrainer_class.rb create mode 100644 lib/awesome_print/formatters/nobrainer_document.rb diff --git a/lib/awesome_print/ext/nobrainer.rb b/lib/awesome_print/ext/nobrainer.rb index 177fe9c0..525a591a 100644 --- a/lib/awesome_print/ext/nobrainer.rb +++ b/lib/awesome_print/ext/nobrainer.rb @@ -23,20 +23,13 @@ def cast_with_nobrainer(object, type) # Format NoBrainer class object. #------------------------------------------------------------------------------ def awesome_nobrainer_class(object) - 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) + AwesomePrint::Formatters::NoBrainerClass.new(self, object).call end # Format NoBrainer Document object. #------------------------------------------------------------------------------ def awesome_nobrainer_document(object) - data = object.inspectable_attributes.symbolize_keys - if object.errors.present? - data = {:errors => object.errors, :attributes => data} - end - "#{object} #{awesome_hash(data)}" + AwesomePrint::Formatters::NoBrainerDocument.new(self, object).call end end end diff --git a/lib/awesome_print/formatters.rb b/lib/awesome_print/formatters.rb index 8ada908e..d6c111aa 100644 --- a/lib/awesome_print/formatters.rb +++ b/lib/awesome_print/formatters.rb @@ -23,5 +23,7 @@ module Formatters require 'awesome_print/formatters/mongo_mapper_class' require 'awesome_print/formatters/mongo_mapper_association' require 'awesome_print/formatters/mongo_mapper_bson_id' + require 'awesome_print/formatters/nobrainer_class' + require 'awesome_print/formatters/nobrainer_document' end end diff --git a/lib/awesome_print/formatters/nobrainer_class.rb b/lib/awesome_print/formatters/nobrainer_class.rb new file mode 100644 index 00000000..7611e124 --- /dev/null +++ b/lib/awesome_print/formatters/nobrainer_class.rb @@ -0,0 +1,18 @@ +module AwesomePrint + module Formatters + class NoBrainerClass < Base + + def call + "class #{object} < #{object.superclass} " << AwesomePrint::Formatters::Hash.new(formatter, columns).call + end + + private + + def columns + ::Hash[object.fields.map do |field, opt| + [field, (opt[:type] || ::Object).to_s.underscore.to_sym] + end] + end + end + end +end diff --git a/lib/awesome_print/formatters/nobrainer_document.rb b/lib/awesome_print/formatters/nobrainer_document.rb new file mode 100644 index 00000000..fa9f201e --- /dev/null +++ b/lib/awesome_print/formatters/nobrainer_document.rb @@ -0,0 +1,20 @@ +module AwesomePrint + module Formatters + class NoBrainerDocument < Base + + def call + "#{object} #{AwesomePrint::Formatters::Hash.new(formatter, columns).call}" + end + + private + + def columns + data = object.inspectable_attributes.symbolize_keys + if object.errors.present? + data = {:errors => object.errors, :attributes => data} + end + data + end + end + end +end From 2727d383013e582d16397e14108798d07af2cb6a Mon Sep 17 00:00:00 2001 From: Mauro George Date: Sat, 14 Mar 2015 16:24:04 -0300 Subject: [PATCH 29/59] Extract Nokogiri formmatter --- lib/awesome_print/ext/nokogiri.rb | 17 ++--------- lib/awesome_print/formatters.rb | 1 + .../formatters/nokogiri_xml_node.rb | 29 +++++++++++++++++++ 3 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 lib/awesome_print/formatters/nokogiri_xml_node.rb diff --git a/lib/awesome_print/ext/nokogiri.rb b/lib/awesome_print/ext/nokogiri.rb index 36853002..0ece4429 100644 --- a/lib/awesome_print/ext/nokogiri.rb +++ b/lib/awesome_print/ext/nokogiri.rb @@ -10,7 +10,7 @@ def self.included(base) #------------------------------------------------------------------------------ def cast_with_nokogiri(object, type) cast = cast_without_nokogiri(object, type) - if (defined?(::Nokogiri::XML::Node) && object.is_a?(::Nokogiri::XML::Node)) || + if (defined?(::Nokogiri::XML::Node) && object.is_a?(::Nokogiri::XML::Node)) || (defined?(::Nokogiri::XML::NodeSet) && object.is_a?(::Nokogiri::XML::NodeSet)) cast = :nokogiri_xml_node end @@ -19,20 +19,7 @@ def cast_with_nokogiri(object, type) #------------------------------------------------------------------------------ def awesome_nokogiri_xml_node(object) - if object.is_a?(::Nokogiri::XML::NodeSet) && object.empty? - return "[]" - end - xml = object.to_xml(:indent => 2) - # - # Colorize tag, id/class name, and contents. - # - xml.gsub!(/(<)(\/?[A-Za-z1-9]+)/) { |tag| "#{$1}#{colorize($2, :keyword)}" } - xml.gsub!(/(id|class)="[^"]+"/i) { |id| colorize(id, :class) } - xml.gsub!(/>([^<]+)#{contents}<" - end - xml + AwesomePrint::Formatters::NokogiriXmlNode.new(self, object).call end end end diff --git a/lib/awesome_print/formatters.rb b/lib/awesome_print/formatters.rb index d6c111aa..3fd80fbc 100644 --- a/lib/awesome_print/formatters.rb +++ b/lib/awesome_print/formatters.rb @@ -25,5 +25,6 @@ module Formatters require 'awesome_print/formatters/mongo_mapper_bson_id' require 'awesome_print/formatters/nobrainer_class' require 'awesome_print/formatters/nobrainer_document' + require 'awesome_print/formatters/nokogiri_xml_node' end end diff --git a/lib/awesome_print/formatters/nokogiri_xml_node.rb b/lib/awesome_print/formatters/nokogiri_xml_node.rb new file mode 100644 index 00000000..aa585559 --- /dev/null +++ b/lib/awesome_print/formatters/nokogiri_xml_node.rb @@ -0,0 +1,29 @@ +module AwesomePrint + module Formatters + class NokogiriXmlNode < Base + + def call + if empty? + return "[]" + end + xml = object.to_xml(:indent => 2) + # + # Colorize tag, id/class name, and contents. + # + xml.gsub!(/(<)(\/?[A-Za-z1-9]+)/) { |tag| "#{$1}#{formatter.colorize($2, :keyword)}" } + xml.gsub!(/(id|class)="[^"]+"/i) { |id| formatter.colorize(id, :class) } + xml.gsub!(/>([^<]+)#{contents}<" + end + xml + end + + private + + def empty? + object.is_a?(::Nokogiri::XML::NodeSet) && object.empty? + end + end + end +end From b4e782c262c32ff1b4c4377bf715f1fc3bad9917 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Sat, 14 Mar 2015 16:28:16 -0300 Subject: [PATCH 30/59] Extract OpenStruct formmatter --- lib/awesome_print/ext/ostruct.rb | 2 +- lib/awesome_print/formatters.rb | 1 + lib/awesome_print/formatters/open_struct_instance.rb | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 lib/awesome_print/formatters/open_struct_instance.rb diff --git a/lib/awesome_print/ext/ostruct.rb b/lib/awesome_print/ext/ostruct.rb index 84b53eb9..a8b9123a 100644 --- a/lib/awesome_print/ext/ostruct.rb +++ b/lib/awesome_print/ext/ostruct.rb @@ -14,7 +14,7 @@ def cast_with_ostruct(object, type) end def awesome_open_struct_instance(object) - "#{object.class} #{awesome_hash(object.marshal_dump)}" + AwesomePrint::Formatters::OpenStructInstance.new(self, object).call end end end diff --git a/lib/awesome_print/formatters.rb b/lib/awesome_print/formatters.rb index 3fd80fbc..d0b52956 100644 --- a/lib/awesome_print/formatters.rb +++ b/lib/awesome_print/formatters.rb @@ -26,5 +26,6 @@ module Formatters require 'awesome_print/formatters/nobrainer_class' require 'awesome_print/formatters/nobrainer_document' require 'awesome_print/formatters/nokogiri_xml_node' + require 'awesome_print/formatters/open_struct_instance' end end diff --git a/lib/awesome_print/formatters/open_struct_instance.rb b/lib/awesome_print/formatters/open_struct_instance.rb new file mode 100644 index 00000000..c1b8746e --- /dev/null +++ b/lib/awesome_print/formatters/open_struct_instance.rb @@ -0,0 +1,10 @@ +module AwesomePrint + module Formatters + class OpenStructInstance < Base + + def call + "#{object.class} #{AwesomePrint::Formatters::Hash.new(formatter, object.marshal_dump).call}" + end + end + end +end From 02982d182a8b98aa6a6af6284a611698e766812e Mon Sep 17 00:00:00 2001 From: Mauro George Date: Sat, 14 Mar 2015 16:40:41 -0300 Subject: [PATCH 31/59] Extract Ripple formmatter --- lib/awesome_print/ext/ripple.rb | 26 ++------------- lib/awesome_print/formatters.rb | 2 ++ .../formatters/ripple_document_class.rb | 22 +++++++++++++ .../formatters/ripple_document_instance.rb | 32 +++++++++++++++++++ 4 files changed, 58 insertions(+), 24 deletions(-) create mode 100644 lib/awesome_print/formatters/ripple_document_class.rb create mode 100644 lib/awesome_print/formatters/ripple_document_instance.rb diff --git a/lib/awesome_print/ext/ripple.rb b/lib/awesome_print/ext/ripple.rb index 1956dc79..b338a517 100644 --- a/lib/awesome_print/ext/ripple.rb +++ b/lib/awesome_print/ext/ripple.rb @@ -31,35 +31,13 @@ def cast_with_ripple(object, type) # #------------------------------------------------------------------------------ def awesome_ripple_document_instance(object) - return object.inspect if !defined?(::ActiveSupport::OrderedHash) - return awesome_object(object) if @options[:raw] - exclude_assoc = @options[:exclude_assoc] or @options[:exclude_associations] - - data = object.attributes.inject(::ActiveSupport::OrderedHash.new) do |hash, (name, value)| - hash[name.to_sym] = object.send(name) - hash - end - - unless exclude_assoc - data = object.class.embedded_associations.inject(data) do |hash, assoc| - hash[assoc.name] = object.get_proxy(assoc) # Should always be array or Ripple::EmbeddedDocument for embedded associations - hash - end - end - - "#{object} " << awesome_hash(data) + AwesomePrint::Formatters::RippleDocumentInstance.new(self, object).call end # Format Ripple class object. #------------------------------------------------------------------------------ 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) + AwesomePrint::Formatters::RippleDocumentClass.new(self, object).call end end end diff --git a/lib/awesome_print/formatters.rb b/lib/awesome_print/formatters.rb index d0b52956..501d3a60 100644 --- a/lib/awesome_print/formatters.rb +++ b/lib/awesome_print/formatters.rb @@ -27,5 +27,7 @@ module Formatters require 'awesome_print/formatters/nobrainer_document' require 'awesome_print/formatters/nokogiri_xml_node' require 'awesome_print/formatters/open_struct_instance' + require 'awesome_print/formatters/ripple_document_instance' + require 'awesome_print/formatters/ripple_document_class' end end diff --git a/lib/awesome_print/formatters/ripple_document_class.rb b/lib/awesome_print/formatters/ripple_document_class.rb new file mode 100644 index 00000000..66bd7b65 --- /dev/null +++ b/lib/awesome_print/formatters/ripple_document_class.rb @@ -0,0 +1,22 @@ +module AwesomePrint + module Formatters + class RippleDocumentClass < Base + + def call + return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:properties) + + "class #{object} < #{object.superclass} " << AwesomePrint::Formatters::Hash.new(formatter, columns).call + end + + private + + def columns + data = object.properties.inject(::ActiveSupport::OrderedHash.new) do |hash, (name, defn)| + hash[name.to_sym] = defn.type.to_s.downcase.to_sym + hash + end + data + end + end + end +end diff --git a/lib/awesome_print/formatters/ripple_document_instance.rb b/lib/awesome_print/formatters/ripple_document_instance.rb new file mode 100644 index 00000000..cb3bc66d --- /dev/null +++ b/lib/awesome_print/formatters/ripple_document_instance.rb @@ -0,0 +1,32 @@ +module AwesomePrint + module Formatters + class RippleDocumentInstance < Base + + def call + return object.inspect if !defined?(::ActiveSupport::OrderedHash) + return AwesomePrint::Formatters::Object.new(formatter, object).call if options[:raw] + + "#{object} " << AwesomePrint::Formatters::Hash.new(formatter, columns).call + end + + private + + def columns + exclude_assoc = options[:exclude_assoc] or options[:exclude_associations] + + data = object.attributes.inject(::ActiveSupport::OrderedHash.new) do |hash, (name, value)| + hash[name.to_sym] = object.send(name) + hash + end + + unless exclude_assoc + data = object.class.embedded_associations.inject(data) do |hash, assoc| + hash[assoc.name] = object.get_proxy(assoc) # Should always be array or Ripple::EmbeddedDocument for embedded associations + hash + end + end + data + end + end + end +end From f791021aa8ddce8de357640f8a1573cd62620835 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Sat, 14 Mar 2015 16:53:52 -0300 Subject: [PATCH 32/59] Extract Sequel formmatter --- lib/awesome_print/ext/sequel.rb | 16 ++++------------ lib/awesome_print/formatters.rb | 3 +++ lib/awesome_print/formatters/sequel_dataset.rb | 11 +++++++++++ lib/awesome_print/formatters/sequel_document.rb | 17 +++++++++++++++++ .../formatters/sequel_model_class.rb | 11 +++++++++++ 5 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 lib/awesome_print/formatters/sequel_dataset.rb create mode 100644 lib/awesome_print/formatters/sequel_document.rb create mode 100644 lib/awesome_print/formatters/sequel_model_class.rb diff --git a/lib/awesome_print/ext/sequel.rb b/lib/awesome_print/ext/sequel.rb index 95c12e20..d2bad480 100644 --- a/lib/awesome_print/ext/sequel.rb +++ b/lib/awesome_print/ext/sequel.rb @@ -23,27 +23,19 @@ def cast_with_sequel(object, type) # Format Sequel Document object. #------------------------------------------------------------------------------ def awesome_sequel_document(object) - data = object.values.sort_by { |key| key.to_s }.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)}" + AwesomePrint::Formatters::SequelDocument.new(self, object).call end # Format Sequel Dataset object. #------------------------------------------------------------------------------ - def awesome_sequel_dataset(dataset) - [awesome_array(dataset.to_a), awesome_print(dataset.sql)].join("\n") + def awesome_sequel_dataset(object) + AwesomePrint::Formatters::SequelDataset.new(self, object).call end # Format 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) + AwesomePrint::Formatters::SequelModelClass.new(self, object).call end end diff --git a/lib/awesome_print/formatters.rb b/lib/awesome_print/formatters.rb index 501d3a60..2d8c645c 100644 --- a/lib/awesome_print/formatters.rb +++ b/lib/awesome_print/formatters.rb @@ -29,5 +29,8 @@ module Formatters require 'awesome_print/formatters/open_struct_instance' require 'awesome_print/formatters/ripple_document_instance' require 'awesome_print/formatters/ripple_document_class' + require 'awesome_print/formatters/sequel_document' + require 'awesome_print/formatters/sequel_dataset' + require 'awesome_print/formatters/sequel_model_class' end end diff --git a/lib/awesome_print/formatters/sequel_dataset.rb b/lib/awesome_print/formatters/sequel_dataset.rb new file mode 100644 index 00000000..e7cb34d1 --- /dev/null +++ b/lib/awesome_print/formatters/sequel_dataset.rb @@ -0,0 +1,11 @@ +module AwesomePrint + module Formatters + class SequelDataset < Base + + def call + [AwesomePrint::Formatters::Array.new(formatter, object.to_a).call, + awesome_print(dataset.sql)].join("\n") + end + end + end +end diff --git a/lib/awesome_print/formatters/sequel_document.rb b/lib/awesome_print/formatters/sequel_document.rb new file mode 100644 index 00000000..ef959b78 --- /dev/null +++ b/lib/awesome_print/formatters/sequel_document.rb @@ -0,0 +1,17 @@ +module AwesomePrint + module Formatters + class SequelDocument < Base + + def call + data = object.values.sort_by { |key| key.to_s }.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} #{AwesomePrint::Formatters::Hash.new(formatter, data).call}" + end + end + end +end diff --git a/lib/awesome_print/formatters/sequel_model_class.rb b/lib/awesome_print/formatters/sequel_model_class.rb new file mode 100644 index 00000000..c6cc8486 --- /dev/null +++ b/lib/awesome_print/formatters/sequel_model_class.rb @@ -0,0 +1,11 @@ +module AwesomePrint + module Formatters + class SequelModelClass < Base + + def call + data = object.db_schema.inject({}) {|h, (name,data)| h.merge(name => data[:db_type])} + "class #{object} < #{object.superclass} " << AwesomePrint::Formatters::Hash.new(formatter, data).call + end + end + end +end From 413a350ae1956d535187ca48da0254483b1d0410 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Sat, 14 Mar 2015 17:21:36 -0300 Subject: [PATCH 33/59] Create a method that instantiate and call all formatters --- lib/awesome_print/ext/active_record.rb | 10 --- lib/awesome_print/ext/active_support.rb | 12 --- lib/awesome_print/ext/mongo_mapper.rb | 22 ------ lib/awesome_print/ext/mongoid.rb | 14 ---- lib/awesome_print/ext/nobrainer.rb | 12 --- lib/awesome_print/ext/nokogiri.rb | 5 -- lib/awesome_print/ext/ostruct.rb | 4 - lib/awesome_print/ext/ripple.rb | 20 ----- lib/awesome_print/ext/sequel.rb | 19 ----- lib/awesome_print/formatter.rb | 79 +++---------------- lib/awesome_print/formatters.rb | 3 +- .../{big_decimal.rb => bigdecimal.rb} | 2 +- .../formatters/ripple_document_instance.rb | 8 ++ lib/awesome_print/formatters/unboundmethod.rb | 10 +++ 14 files changed, 30 insertions(+), 190 deletions(-) rename lib/awesome_print/formatters/{big_decimal.rb => bigdecimal.rb} (83%) create mode 100644 lib/awesome_print/formatters/unboundmethod.rb diff --git a/lib/awesome_print/ext/active_record.rb b/lib/awesome_print/ext/active_record.rb index 3111e5d5..bac6b33f 100644 --- a/lib/awesome_print/ext/active_record.rb +++ b/lib/awesome_print/ext/active_record.rb @@ -21,16 +21,6 @@ def cast_with_active_record(object, type) end cast end - - private - - def awesome_active_record_instance(object) - AwesomePrint::Formatters::ActiveRecordInstance.new(self, object).call - end - - def awesome_active_record_class(object) - AwesomePrint::Formatters::ActiveRecordClass.new(self, object).call - end end end diff --git a/lib/awesome_print/ext/active_support.rb b/lib/awesome_print/ext/active_support.rb index 941c0048..368f2295 100644 --- a/lib/awesome_print/ext/active_support.rb +++ b/lib/awesome_print/ext/active_support.rb @@ -17,18 +17,6 @@ def cast_with_active_support(object, type) end cast end - - # Format ActiveSupport::TimeWithZone as standard Time. - #------------------------------------------------------------------------------ - def awesome_active_support_time(object) - AwesomePrint::Formatters::ActiveSupportTime.new(self, object).call - end - - # Format HashWithIndifferentAccess as standard Hash. - #------------------------------------------------------------------------------ - def awesome_hash_with_indifferent_access(object) - AwesomePrint::Formatters::HashWithIndifferentAccess.new(self, object).call - end end end diff --git a/lib/awesome_print/ext/mongo_mapper.rb b/lib/awesome_print/ext/mongo_mapper.rb index 3cd9522b..457166c5 100644 --- a/lib/awesome_print/ext/mongo_mapper.rb +++ b/lib/awesome_print/ext/mongo_mapper.rb @@ -25,28 +25,6 @@ def cast_with_mongo_mapper(object, type) cast end - - # Format MongoMapper class object. - #------------------------------------------------------------------------------ - def awesome_mongo_mapper_class(object) - AwesomePrint::Formatters::MongoMapperClass.new(self, object).call - end - - def awesome_mongo_mapper_instance(object) - AwesomePrint::Formatters::MongoMapperInstance.new(self, object).call - end - - # Format MongoMapper association object. - #------------------------------------------------------------------------------ - def awesome_mongo_mapper_association(object) - AwesomePrint::Formatters::MongoMapperAssociation.new(self, object).call - end - - # Format BSON::ObjectId - #------------------------------------------------------------------------------ - def awesome_mongo_mapper_bson_id(object) - AwesomePrint::Formatters::MongoMapperBsonId.new(self, object).call - end end end diff --git a/lib/awesome_print/ext/mongoid.rb b/lib/awesome_print/ext/mongoid.rb index ba6edc52..0a8d0165 100644 --- a/lib/awesome_print/ext/mongoid.rb +++ b/lib/awesome_print/ext/mongoid.rb @@ -21,20 +21,6 @@ def cast_with_mongoid(object, type) end cast end - - def awesome_mongoid_class(object) - AwesomePrint::Formatters::MongoidClass.new(self, object).call - end - - def awesome_mongoid_document(object) - AwesomePrint::Formatters::MongoidDocument.new(self, object).call - end - - # Format BSON::ObjectId - #------------------------------------------------------------------------------ - def awesome_mongoid_bson_id(object) - AwesomePrint::Formatters::MongoidBsonId.new(self, object).call - end end end diff --git a/lib/awesome_print/ext/nobrainer.rb b/lib/awesome_print/ext/nobrainer.rb index 525a591a..4857d488 100644 --- a/lib/awesome_print/ext/nobrainer.rb +++ b/lib/awesome_print/ext/nobrainer.rb @@ -19,18 +19,6 @@ def cast_with_nobrainer(object, type) end cast end - - # Format NoBrainer class object. - #------------------------------------------------------------------------------ - def awesome_nobrainer_class(object) - AwesomePrint::Formatters::NoBrainerClass.new(self, object).call - end - - # Format NoBrainer Document object. - #------------------------------------------------------------------------------ - def awesome_nobrainer_document(object) - AwesomePrint::Formatters::NoBrainerDocument.new(self, object).call - end end end diff --git a/lib/awesome_print/ext/nokogiri.rb b/lib/awesome_print/ext/nokogiri.rb index 0ece4429..fc8f4f18 100644 --- a/lib/awesome_print/ext/nokogiri.rb +++ b/lib/awesome_print/ext/nokogiri.rb @@ -16,11 +16,6 @@ def cast_with_nokogiri(object, type) end cast end - - #------------------------------------------------------------------------------ - def awesome_nokogiri_xml_node(object) - AwesomePrint::Formatters::NokogiriXmlNode.new(self, object).call - end end end diff --git a/lib/awesome_print/ext/ostruct.rb b/lib/awesome_print/ext/ostruct.rb index a8b9123a..df3cb4a6 100644 --- a/lib/awesome_print/ext/ostruct.rb +++ b/lib/awesome_print/ext/ostruct.rb @@ -12,10 +12,6 @@ def cast_with_ostruct(object, type) end cast end - - def awesome_open_struct_instance(object) - AwesomePrint::Formatters::OpenStructInstance.new(self, object).call - end end end diff --git a/lib/awesome_print/ext/ripple.rb b/lib/awesome_print/ext/ripple.rb index b338a517..ee78bc6b 100644 --- a/lib/awesome_print/ext/ripple.rb +++ b/lib/awesome_print/ext/ripple.rb @@ -19,26 +19,6 @@ def cast_with_ripple(object, type) end cast end - - private - - # Format Ripple instance object. - # - # NOTE: by default only instance attributes are shown. To format a Ripple document instance - # as a regular object showing its instance variables and accessors use :raw => true option: - # - # ap document, :raw => true - # - #------------------------------------------------------------------------------ - def awesome_ripple_document_instance(object) - AwesomePrint::Formatters::RippleDocumentInstance.new(self, object).call - end - - # Format Ripple class object. - #------------------------------------------------------------------------------ - def awesome_ripple_document_class(object) - AwesomePrint::Formatters::RippleDocumentClass.new(self, object).call - end end end diff --git a/lib/awesome_print/ext/sequel.rb b/lib/awesome_print/ext/sequel.rb index d2bad480..0a334aae 100644 --- a/lib/awesome_print/ext/sequel.rb +++ b/lib/awesome_print/ext/sequel.rb @@ -19,26 +19,7 @@ def cast_with_sequel(object, type) end cast end - - # Format Sequel Document object. - #------------------------------------------------------------------------------ - def awesome_sequel_document(object) - AwesomePrint::Formatters::SequelDocument.new(self, object).call - end - - # Format Sequel Dataset object. - #------------------------------------------------------------------------------ - def awesome_sequel_dataset(object) - AwesomePrint::Formatters::SequelDataset.new(self, object).call - end - - # Format Sequel Model class. - #------------------------------------------------------------------------------ - def awesome_sequel_model_class(object) - AwesomePrint::Formatters::SequelModelClass.new(self, object).call - end end - end AwesomePrint::Formatter.send(:include, AwesomePrint::Sequel) diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 47dc47d1..ccc24c5f 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -21,7 +21,7 @@ def initialize(inspector) def format(object, type = nil) core_class = cast(object, type) awesome = if core_class != :self - send(:"awesome_#{core_class}", object) # Core formatters. + factory_class(core_class, object) else awesome_self(object, type) # Catch all that falls back to object.inspect. end @@ -130,85 +130,24 @@ def method_tuple(method) private + def factory_class(core_class, object) + class_name = core_class.to_s.split('_').map(&:capitalize).join('') + klass = Object.const_get("AwesomePrint::Formatters::#{class_name}") + klass.new(self, object).call + end + # Catch all method to format an arbitrary object. #------------------------------------------------------------------------------ def awesome_self(object, type) if @options[:raw] && object.instance_variables.any? - return awesome_object(object) + return AwesomePrint::Formatters::Object.new(self, object).call elsif hash = convert_to_hash(object) - awesome_hash(hash) + AwesomePrint::Formatters::Hash.new(self, hash).call else colorize(object.inspect.to_s, type) end end - # Format an array. - #------------------------------------------------------------------------------ - def awesome_array(a) - AwesomePrint::Formatters::Array.new(self, a).call - end - - # Format a hash. If @options[:indent] if negative left align hash keys. - #------------------------------------------------------------------------------ - def awesome_hash(h) - AwesomePrint::Formatters::Hash.new(self, h).call - end - - # Format an object. - #------------------------------------------------------------------------------ - def awesome_object(o) - AwesomePrint::Formatters::Object.new(self, o).call - end - - # Format a set. - #------------------------------------------------------------------------------ - def awesome_set(s) - AwesomePrint::Formatters::Set.new(self, s).call - end - - # Format a Struct. - #------------------------------------------------------------------------------ - def awesome_struct(s) - AwesomePrint::Formatters::Struct.new(self, s).call - end - - # Format Class object. - #------------------------------------------------------------------------------ - def awesome_class(c) - AwesomePrint::Formatters::Class.new(self, c).call - end - - # Format File object. - #------------------------------------------------------------------------------ - def awesome_file(f) - AwesomePrint::Formatters::File.new(self, f).call - end - - # Format Dir object. - #------------------------------------------------------------------------------ - def awesome_dir(d) - AwesomePrint::Formatters::Dir.new(self, d).call - end - - # Format BigDecimal object. - #------------------------------------------------------------------------------ - def awesome_bigdecimal(n) - AwesomePrint::Formatters::BigDecimal.new(self, n).call - end - - # Format Rational object. - #------------------------------------------------------------------------------ - def awesome_rational(n) - AwesomePrint::Formatters::Rational.new(self, n).call - end - - # Format a method. - #------------------------------------------------------------------------------ - def awesome_method(m) - AwesomePrint::Formatters::Method.new(self, m).call - end - alias :awesome_unboundmethod :awesome_method - # Utility methods. #------------------------------------------------------------------------------ def convert_to_hash(object) diff --git a/lib/awesome_print/formatters.rb b/lib/awesome_print/formatters.rb index 2d8c645c..c6a78ad2 100644 --- a/lib/awesome_print/formatters.rb +++ b/lib/awesome_print/formatters.rb @@ -9,9 +9,10 @@ module Formatters require 'awesome_print/formatters/class' require 'awesome_print/formatters/file' require 'awesome_print/formatters/dir' - require 'awesome_print/formatters/big_decimal' + require 'awesome_print/formatters/bigdecimal' require 'awesome_print/formatters/rational' require 'awesome_print/formatters/method' + require 'awesome_print/formatters/unboundmethod' require 'awesome_print/formatters/active_record_instance' require 'awesome_print/formatters/active_record_class' require 'awesome_print/formatters/active_support_time' diff --git a/lib/awesome_print/formatters/big_decimal.rb b/lib/awesome_print/formatters/bigdecimal.rb similarity index 83% rename from lib/awesome_print/formatters/big_decimal.rb rename to lib/awesome_print/formatters/bigdecimal.rb index 452fdd9a..73625cc2 100644 --- a/lib/awesome_print/formatters/big_decimal.rb +++ b/lib/awesome_print/formatters/bigdecimal.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class BigDecimal < Base + class Bigdecimal < Base def call formatter.colorize(object.to_s('F'), :bigdecimal) diff --git a/lib/awesome_print/formatters/ripple_document_instance.rb b/lib/awesome_print/formatters/ripple_document_instance.rb index cb3bc66d..9f491fa4 100644 --- a/lib/awesome_print/formatters/ripple_document_instance.rb +++ b/lib/awesome_print/formatters/ripple_document_instance.rb @@ -2,6 +2,14 @@ module AwesomePrint module Formatters class RippleDocumentInstance < Base + # Format Ripple instance object. + # + # NOTE: by default only instance attributes are shown. To format a Ripple document instance + # as a regular object showing its instance variables and accessors use :raw => true option: + # + # ap document, :raw => true + # + #------------------------------------------------------------------------------ def call return object.inspect if !defined?(::ActiveSupport::OrderedHash) return AwesomePrint::Formatters::Object.new(formatter, object).call if options[:raw] diff --git a/lib/awesome_print/formatters/unboundmethod.rb b/lib/awesome_print/formatters/unboundmethod.rb new file mode 100644 index 00000000..70594ee2 --- /dev/null +++ b/lib/awesome_print/formatters/unboundmethod.rb @@ -0,0 +1,10 @@ +module AwesomePrint + module Formatters + class Unboundmethod < Base + + def call + AwesomePrint::Formatters::Method.new(formatter, object).call + end + end + end +end From 05fad937249d495d4f693cc64fc5dc59f0132f60 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Mon, 16 Mar 2015 10:53:15 -0300 Subject: [PATCH 34/59] Create FormatterFactory Create a factory to call all formatters. Create a constantize method to handle the instantiation of the class. Since the `Object.const_get` brokes on ruby 1.9.3. The constantize is the same of the ActiveSupport since we need only this for the ActiveSupport, copied the method over add a new dependency. --- lib/awesome_print/formatter.rb | 10 ++---- lib/awesome_print/formatter_factory.rb | 47 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 lib/awesome_print/formatter_factory.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index ccc24c5f..3c295e4f 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -1,6 +1,6 @@ autoload :CGI, 'cgi' require 'shellwords' -require 'awesome_print/formatters' +require 'awesome_print/formatter_factory' module AwesomePrint class Formatter @@ -21,7 +21,7 @@ def initialize(inspector) def format(object, type = nil) core_class = cast(object, type) awesome = if core_class != :self - factory_class(core_class, object) + AwesomePrint::FormatterFactory.new(core_class, self, object).call else awesome_self(object, type) # Catch all that falls back to object.inspect. end @@ -130,12 +130,6 @@ def method_tuple(method) private - def factory_class(core_class, object) - class_name = core_class.to_s.split('_').map(&:capitalize).join('') - klass = Object.const_get("AwesomePrint::Formatters::#{class_name}") - klass.new(self, object).call - end - # Catch all method to format an arbitrary object. #------------------------------------------------------------------------------ def awesome_self(object, type) diff --git a/lib/awesome_print/formatter_factory.rb b/lib/awesome_print/formatter_factory.rb new file mode 100644 index 00000000..2c3041f8 --- /dev/null +++ b/lib/awesome_print/formatter_factory.rb @@ -0,0 +1,47 @@ +require 'awesome_print/formatters' + +module AwesomePrint + class FormatterFactory + + def initialize(class_name, formatter, object) + @class_name = class_name.to_s.split('_').map(&:capitalize).join('') + @formatter = formatter + @object = object + end + + def call + klass = constantize("AwesomePrint::Formatters::#{class_name}") + klass.new(formatter, object).call + end + + private + + attr_reader :class_name, :formatter, :object + + def constantize(camel_cased_word) + names = camel_cased_word.split('::') + # Trigger a built-in NameError exception including the ill-formed constant in the message. + Object.const_get(camel_cased_word) if names.empty? + # Remove the first blank element in case of '::ClassName' notation. + names.shift if names.size > 1 && names.first.empty? + names.inject(Object) do |constant, name| + if constant == Object + constant.const_get(name) + else + candidate = constant.const_get(name) + next candidate if constant.const_defined?(name, false) + next candidate unless Object.const_defined?(name) + # Go down the ancestors to check if it is owned directly. The check + # stops when we reach Object or the end of ancestors tree. + constant = constant.ancestors.inject do |const, ancestor| + break const if ancestor == Object + break ancestor if ancestor.const_defined?(name, false) + const + end + # owner is in Object, so raise + constant.const_get(name, false) + end + end + end + end +end From 9c320e6e73aac48fd61bf756b21c2a64d2bfc146 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Mon, 16 Mar 2015 10:56:09 -0300 Subject: [PATCH 35/59] Fix NoBrainer specs --- lib/awesome_print/formatters/nobrainer_class.rb | 2 +- lib/awesome_print/formatters/nobrainer_document.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/awesome_print/formatters/nobrainer_class.rb b/lib/awesome_print/formatters/nobrainer_class.rb index 7611e124..532d345a 100644 --- a/lib/awesome_print/formatters/nobrainer_class.rb +++ b/lib/awesome_print/formatters/nobrainer_class.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class NoBrainerClass < Base + class NobrainerClass < Base def call "class #{object} < #{object.superclass} " << AwesomePrint::Formatters::Hash.new(formatter, columns).call diff --git a/lib/awesome_print/formatters/nobrainer_document.rb b/lib/awesome_print/formatters/nobrainer_document.rb index fa9f201e..64c765b8 100644 --- a/lib/awesome_print/formatters/nobrainer_document.rb +++ b/lib/awesome_print/formatters/nobrainer_document.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class NoBrainerDocument < Base + class NobrainerDocument < Base def call "#{object} #{AwesomePrint::Formatters::Hash.new(formatter, columns).call}" From 1def6462f10146f680d3661a7c72e290b32ea60e Mon Sep 17 00:00:00 2001 From: Mauro George Date: Mon, 16 Mar 2015 18:33:10 -0300 Subject: [PATCH 36/59] Extract Self formatter --- lib/awesome_print/formatter.rb | 42 ++-------------------------- lib/awesome_print/formatters.rb | 1 + lib/awesome_print/formatters/self.rb | 34 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 39 deletions(-) create mode 100644 lib/awesome_print/formatters/self.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 3c295e4f..a0acfcee 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -8,7 +8,7 @@ class Formatter CORE = [ :array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod ] DEFAULT_LIMIT_SIZE = 7 - attr_reader :options, :inspector, :indentation + attr_reader :options, :inspector, :indentation, :type def initialize(inspector) @inspector = inspector @@ -19,13 +19,9 @@ def initialize(inspector) # Main entry point to format an object. #------------------------------------------------------------------------------ def format(object, type = nil) + @type = type core_class = cast(object, type) - awesome = if core_class != :self - AwesomePrint::FormatterFactory.new(core_class, self, object).call - else - awesome_self(object, type) # Catch all that falls back to object.inspect. - end - awesome + AwesomePrint::FormatterFactory.new(core_class, self, object).call end # Hook this when adding custom formatters. Check out lib/awesome_print/ext @@ -127,37 +123,5 @@ def method_tuple(method) [ method.name.to_s, "(#{args.join(', ')})", owner.to_s ] end - - private - - # Catch all method to format an arbitrary object. - #------------------------------------------------------------------------------ - def awesome_self(object, type) - if @options[:raw] && object.instance_variables.any? - return AwesomePrint::Formatters::Object.new(self, object).call - elsif hash = convert_to_hash(object) - AwesomePrint::Formatters::Hash.new(self, hash).call - else - colorize(object.inspect.to_s, type) - end - end - - # Utility methods. - #------------------------------------------------------------------------------ - def convert_to_hash(object) - if ! object.respond_to?(:to_hash) - return nil - end - if object.method(:to_hash).arity != 0 - return nil - end - - hash = object.to_hash - if ! hash.respond_to?(:keys) || ! hash.respond_to?('[]') - return nil - end - - return hash - end end end diff --git a/lib/awesome_print/formatters.rb b/lib/awesome_print/formatters.rb index c6a78ad2..bcead411 100644 --- a/lib/awesome_print/formatters.rb +++ b/lib/awesome_print/formatters.rb @@ -1,6 +1,7 @@ module AwesomePrint module Formatters require 'awesome_print/formatters/base' + require 'awesome_print/formatters/self' require 'awesome_print/formatters/array' require 'awesome_print/formatters/hash' require 'awesome_print/formatters/object' diff --git a/lib/awesome_print/formatters/self.rb b/lib/awesome_print/formatters/self.rb new file mode 100644 index 00000000..5dce77e5 --- /dev/null +++ b/lib/awesome_print/formatters/self.rb @@ -0,0 +1,34 @@ +module AwesomePrint + module Formatters + class Self < Base + + def call + if options[:raw] && object.instance_variables.any? + return AwesomePrint::Formatters::Object.new(formatter, object).call + elsif hash = convert_to_hash(object) + AwesomePrint::Formatters::Hash.new(formatter, hash).call + else + formatter.colorize(object.inspect.to_s, formatter.type) + end + end + + private + + def convert_to_hash(object) + if ! object.respond_to?(:to_hash) + return nil + end + if object.method(:to_hash).arity != 0 + return nil + end + + hash = object.to_hash + if ! hash.respond_to?(:keys) || ! hash.respond_to?('[]') + return nil + end + + return hash + end + end + end +end From 17abe6de7f440b5e7029030fd53c1fdf0dcaaef2 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Mon, 16 Mar 2015 18:51:22 -0300 Subject: [PATCH 37/59] Extract MethodTuple module --- lib/awesome_print/formatter.rb | 39 ----------------- lib/awesome_print/formatters/array.rb | 6 ++- lib/awesome_print/formatters/method.rb | 5 ++- lib/awesome_print/formatters/method_tuple.rb | 45 ++++++++++++++++++++ 4 files changed, 53 insertions(+), 42 deletions(-) create mode 100644 lib/awesome_print/formatters/method_tuple.rb diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index a0acfcee..dc0aab30 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -84,44 +84,5 @@ def left_aligned ensure @options[:indent] = current end - - # Return [ name, arguments, owner ] tuple for a given method. - #------------------------------------------------------------------------------ - def method_tuple(method) - if method.respond_to?(:parameters) # Ruby 1.9.2+ - # See http://ruby.runpaint.org/methods#method-objects-parameters - args = method.parameters.inject([]) do |arr, (type, name)| - name ||= (type == :block ? 'block' : "arg#{arr.size + 1}") - arr << case type - when :req then name.to_s - when :opt, :rest then "*#{name}" - when :block then "&#{name}" - else '?' - end - end - else # See http://ruby-doc.org/core/classes/Method.html#M001902 - args = (1..method.arity.abs).map { |i| "arg#{i}" } - args[-1] = "*#{args[-1]}" if method.arity < 0 - end - - # method.to_s formats to handle: - # - # # - # # - # #)#_username> - # # - # # - # # - # - if method.to_s =~ /(Unbound)*Method: (.*)[#\.]/ - unbound, klass = $1 && '(unbound)', $2 - if klass && klass =~ /(\(\w+:\s.*?\))/ # Is this ActiveRecord-style class? - klass.sub!($1, '') # Yes, strip the fields leaving class name only. - end - owner = "#{klass}#{unbound}".gsub('(', ' (') - end - - [ method.name.to_s, "(#{args.join(', ')})", owner.to_s ] - end end end diff --git a/lib/awesome_print/formatters/array.rb b/lib/awesome_print/formatters/array.rb index 389e8d62..f62a6942 100644 --- a/lib/awesome_print/formatters/array.rb +++ b/lib/awesome_print/formatters/array.rb @@ -1,9 +1,11 @@ require 'awesome_print/formatters/enumerable' +require 'awesome_print/formatters/method_tuple' module AwesomePrint module Formatters class Array < Base include Enumerable + include MethodTuple def call return empty_format if object.empty? @@ -56,10 +58,10 @@ def methods_array(a) tuple = if object.respond_to?(name, true) # Is this a regular method? the_method = object.method(name) rescue nil # Avoid potential ArgumentError if object#method is overridden. if the_method && the_method.respond_to?(:arity) # Is this original object#method? - formatter.method_tuple(the_method) # Yes, we are good. + method_tuple(the_method) # Yes, we are good. end elsif object.respond_to?(:instance_method) # Is this an unbound method? - formatter.method_tuple(object.instance_method(name)) rescue nil # Rescue to avoid NameError when the method is not + method_tuple(object.instance_method(name)) rescue nil # Rescue to avoid NameError when the method is not end # available (ex. File.lchmod on Ubuntu 12). end tuple || [ name.to_s, '(?)', '?' ] # Return WTF default if all the above fails. diff --git a/lib/awesome_print/formatters/method.rb b/lib/awesome_print/formatters/method.rb index 40ba8b40..b4b6df3f 100644 --- a/lib/awesome_print/formatters/method.rb +++ b/lib/awesome_print/formatters/method.rb @@ -1,9 +1,12 @@ +require 'awesome_print/formatters/method_tuple' + module AwesomePrint module Formatters class Method < Base + include MethodTuple def call - name, args, owner = formatter.method_tuple(object) + name, args, owner = method_tuple(object) "#{formatter.colorize(owner, :class)}##{formatter.colorize(name, :method)}#{formatter.colorize(args, :args)}" end end diff --git a/lib/awesome_print/formatters/method_tuple.rb b/lib/awesome_print/formatters/method_tuple.rb new file mode 100644 index 00000000..70ca60d7 --- /dev/null +++ b/lib/awesome_print/formatters/method_tuple.rb @@ -0,0 +1,45 @@ +module AwesomePrint + module Formatters + module MethodTuple + + # Return [ name, arguments, owner ] tuple for a given method. + #------------------------------------------------------------------------------ + def method_tuple(method) + if method.respond_to?(:parameters) # Ruby 1.9.2+ + # See http://ruby.runpaint.org/methods#method-objects-parameters + args = method.parameters.inject([]) do |arr, (type, name)| + name ||= (type == :block ? 'block' : "arg#{arr.size + 1}") + arr << case type + when :req then name.to_s + when :opt, :rest then "*#{name}" + when :block then "&#{name}" + else '?' + end + end + else # See http://ruby-doc.org/core/classes/Method.html#M001902 + args = (1..method.arity.abs).map { |i| "arg#{i}" } + args[-1] = "*#{args[-1]}" if method.arity < 0 + end + + # method.to_s formats to handle: + # + # # + # # + # #)#_username> + # # + # # + # # + # + if method.to_s =~ /(Unbound)*Method: (.*)[#\.]/ + unbound, klass = $1 && '(unbound)', $2 + if klass && klass =~ /(\(\w+:\s.*?\))/ # Is this ActiveRecord-style class? + klass.sub!($1, '') # Yes, strip the fields leaving class name only. + end + owner = "#{klass}#{unbound}".gsub('(', ' (') + end + + [ method.name.to_s, "(#{args.join(', ')})", owner.to_s ] + end + end + end +end From d27284fa10840ca48419387c8d333921ab5a277d Mon Sep 17 00:00:00 2001 From: Mauro George Date: Tue, 17 Mar 2015 09:39:32 -0300 Subject: [PATCH 38/59] Create ActiveRecord type --- lib/awesome_print.rb | 2 +- lib/awesome_print/ext/active_record.rb | 27 --------------------- lib/awesome_print/formatter.rb | 3 ++- lib/awesome_print/formatter_factory.rb | 28 +--------------------- lib/awesome_print/support.rb | 30 ++++++++++++++++++++++++ lib/awesome_print/type_discover.rb | 27 +++++++++++++++++++++ lib/awesome_print/types.rb | 5 ++++ lib/awesome_print/types/active_record.rb | 24 +++++++++++++++++++ 8 files changed, 90 insertions(+), 56 deletions(-) delete mode 100644 lib/awesome_print/ext/active_record.rb create mode 100644 lib/awesome_print/support.rb create mode 100644 lib/awesome_print/type_discover.rb create mode 100644 lib/awesome_print/types.rb create mode 100644 lib/awesome_print/types/active_record.rb diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index 0bdfb507..cdf0bff3 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -7,6 +7,7 @@ require File.dirname(__FILE__) + "/awesome_print/core_ext/#{file}" end + require File.dirname(__FILE__) + "/awesome_print/support" require File.dirname(__FILE__) + "/awesome_print/configuration" require File.dirname(__FILE__) + "/awesome_print/inspector" require File.dirname(__FILE__) + "/awesome_print/formatter" @@ -16,7 +17,6 @@ # Load the following under normal circumstances as well as in Rails # console when required from ~/.irbrc or ~/.pryrc. # - require File.dirname(__FILE__) + "/awesome_print/ext/active_record" if defined?(ActiveRecord) || AwesomePrint.rails_console? require File.dirname(__FILE__) + "/awesome_print/ext/active_support" if defined?(ActiveSupport) || AwesomePrint.rails_console? # # Load remaining extensions. diff --git a/lib/awesome_print/ext/active_record.rb b/lib/awesome_print/ext/active_record.rb deleted file mode 100644 index bac6b33f..00000000 --- a/lib/awesome_print/ext/active_record.rb +++ /dev/null @@ -1,27 +0,0 @@ -module AwesomePrint - module ActiveRecord - - def self.included(base) - base.send :alias_method, :cast_without_active_record, :cast - base.send :alias_method, :cast, :cast_with_active_record - end - - # Add ActiveRecord class names to the dispatcher pipeline. - #------------------------------------------------------------------------------ - def cast_with_active_record(object, type) - cast = cast_without_active_record(object, type) - return cast if !defined?(::ActiveRecord) - - if object.is_a?(::ActiveRecord::Base) - cast = :active_record_instance - elsif object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base) - cast = :active_record_class - elsif type == :activerecord_relation || object.class.ancestors.include?(::ActiveRecord::Relation) - cast = :array - end - cast - end - end -end - -AwesomePrint::Formatter.send(:include, AwesomePrint::ActiveRecord) diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index dc0aab30..531cc633 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -1,6 +1,7 @@ autoload :CGI, 'cgi' require 'shellwords' require 'awesome_print/formatter_factory' +require 'awesome_print/type_discover' module AwesomePrint class Formatter @@ -28,7 +29,7 @@ def format(object, type = nil) # directory for custom formatters that ship with awesome_print. #------------------------------------------------------------------------------ def cast(object, type) - CORE.grep(type)[0] || :self + AwesomePrint::TypeDiscover.new(object).call || CORE.grep(type)[0] || :self end # Pick the color and apply it to the given string as necessary. diff --git a/lib/awesome_print/formatter_factory.rb b/lib/awesome_print/formatter_factory.rb index 2c3041f8..29d5fd6a 100644 --- a/lib/awesome_print/formatter_factory.rb +++ b/lib/awesome_print/formatter_factory.rb @@ -10,38 +10,12 @@ def initialize(class_name, formatter, object) end def call - klass = constantize("AwesomePrint::Formatters::#{class_name}") + klass = AwesomePrint::Support.constantize("AwesomePrint::Formatters::#{class_name}") klass.new(formatter, object).call end private attr_reader :class_name, :formatter, :object - - def constantize(camel_cased_word) - names = camel_cased_word.split('::') - # Trigger a built-in NameError exception including the ill-formed constant in the message. - Object.const_get(camel_cased_word) if names.empty? - # Remove the first blank element in case of '::ClassName' notation. - names.shift if names.size > 1 && names.first.empty? - names.inject(Object) do |constant, name| - if constant == Object - constant.const_get(name) - else - candidate = constant.const_get(name) - next candidate if constant.const_defined?(name, false) - next candidate unless Object.const_defined?(name) - # Go down the ancestors to check if it is owned directly. The check - # stops when we reach Object or the end of ancestors tree. - constant = constant.ancestors.inject do |const, ancestor| - break const if ancestor == Object - break ancestor if ancestor.const_defined?(name, false) - const - end - # owner is in Object, so raise - constant.const_get(name, false) - end - end - end end end diff --git a/lib/awesome_print/support.rb b/lib/awesome_print/support.rb new file mode 100644 index 00000000..a4b9a03a --- /dev/null +++ b/lib/awesome_print/support.rb @@ -0,0 +1,30 @@ +module AwesomePrint + class Support + + def self.constantize(camel_cased_word) + names = camel_cased_word.split('::') + # Trigger a built-in NameError exception including the ill-formed constant in the message. + Object.const_get(camel_cased_word) if names.empty? + # Remove the first blank element in case of '::ClassName' notation. + names.shift if names.size > 1 && names.first.empty? + names.inject(Object) do |constant, name| + if constant == Object + constant.const_get(name) + else + candidate = constant.const_get(name) + next candidate if constant.const_defined?(name, false) + next candidate unless Object.const_defined?(name) + # Go down the ancestors to check if it is owned directly. The check + # stops when we reach Object or the end of ancestors tree. + constant = constant.ancestors.inject do |const, ancestor| + break const if ancestor == Object + break ancestor if ancestor.const_defined?(name, false) + const + end + # owner is in Object, so raise + constant.const_get(name, false) + end + end + end + end +end diff --git a/lib/awesome_print/type_discover.rb b/lib/awesome_print/type_discover.rb new file mode 100644 index 00000000..ff8c1b3f --- /dev/null +++ b/lib/awesome_print/type_discover.rb @@ -0,0 +1,27 @@ +require 'awesome_print/types' + +module AwesomePrint + class TypeDiscover + + TYPES = %w(ActiveRecord) + + def initialize(object) + @object = object + end + + def call + TYPES.map do |type| + begin + klass = AwesomePrint::Support.constantize("AwesomePrint::Types::#{type}") + klass.new(object).call + rescue NameError + nil + end + end.detect { |type| !type.nil? } + end + + private + + attr_reader :object + end +end diff --git a/lib/awesome_print/types.rb b/lib/awesome_print/types.rb new file mode 100644 index 00000000..17fee980 --- /dev/null +++ b/lib/awesome_print/types.rb @@ -0,0 +1,5 @@ +module AwesomePrint + module Types + require 'awesome_print/types/active_record' if defined?(ActiveRecord) || AwesomePrint.rails_console? + end +end diff --git a/lib/awesome_print/types/active_record.rb b/lib/awesome_print/types/active_record.rb new file mode 100644 index 00000000..c668f594 --- /dev/null +++ b/lib/awesome_print/types/active_record.rb @@ -0,0 +1,24 @@ +module AwesomePrint + module Types + class ActiveRecord + + def initialize(object) + @object = object + end + + def call + if object.is_a?(::ActiveRecord::Base) + :active_record_instance + elsif object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base) + :active_record_class + elsif object.class.ancestors.include?(::ActiveRecord::Relation) + :array + end + end + + private + + attr_reader :object + end + end +end From 58d618c8ef852a267b216d9b42d90bfae9230e3d Mon Sep 17 00:00:00 2001 From: Mauro George Date: Tue, 17 Mar 2015 18:40:01 -0300 Subject: [PATCH 39/59] Create ActiveSupport type --- lib/awesome_print.rb | 6 +---- lib/awesome_print/ext/active_support.rb | 30 ----------------------- lib/awesome_print/type_discover.rb | 2 +- lib/awesome_print/types.rb | 2 ++ lib/awesome_print/types/active_record.rb | 10 +------- lib/awesome_print/types/active_support.rb | 18 ++++++++++++++ lib/awesome_print/types/base.rb | 14 +++++++++++ 7 files changed, 37 insertions(+), 45 deletions(-) delete mode 100644 lib/awesome_print/ext/active_support.rb create mode 100644 lib/awesome_print/types/active_support.rb create mode 100644 lib/awesome_print/types/base.rb diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index cdf0bff3..477fee89 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -13,11 +13,7 @@ require File.dirname(__FILE__) + "/awesome_print/formatter" require File.dirname(__FILE__) + "/awesome_print/version" require File.dirname(__FILE__) + "/awesome_print/core_ext/logger" if defined?(Logger) - # - # Load the following under normal circumstances as well as in Rails - # console when required from ~/.irbrc or ~/.pryrc. - # - require File.dirname(__FILE__) + "/awesome_print/ext/active_support" if defined?(ActiveSupport) || AwesomePrint.rails_console? + # # Load remaining extensions. # diff --git a/lib/awesome_print/ext/active_support.rb b/lib/awesome_print/ext/active_support.rb deleted file mode 100644 index 368f2295..00000000 --- a/lib/awesome_print/ext/active_support.rb +++ /dev/null @@ -1,30 +0,0 @@ -module AwesomePrint - module ActiveSupport - - def self.included(base) - base.send :alias_method, :cast_without_active_support, :cast - base.send :alias_method, :cast, :cast_with_active_support - end - - def cast_with_active_support(object, type) - cast = cast_without_active_support(object, type) - if defined?(::ActiveSupport) && defined?(::HashWithIndifferentAccess) - if (defined?(::ActiveSupport::TimeWithZone) && object.is_a?(::ActiveSupport::TimeWithZone)) || object.is_a?(::Date) - cast = :active_support_time - elsif object.is_a?(::HashWithIndifferentAccess) - cast = :hash_with_indifferent_access - end - end - cast - end - end -end - -AwesomePrint::Formatter.send(:include, AwesomePrint::ActiveSupport) -# -# Colorize Rails logs. -# -if defined?(ActiveSupport::LogSubscriber) - AwesomePrint.force_colors! ActiveSupport::LogSubscriber.colorize_logging -end - diff --git a/lib/awesome_print/type_discover.rb b/lib/awesome_print/type_discover.rb index ff8c1b3f..4c83ad34 100644 --- a/lib/awesome_print/type_discover.rb +++ b/lib/awesome_print/type_discover.rb @@ -3,7 +3,7 @@ module AwesomePrint class TypeDiscover - TYPES = %w(ActiveRecord) + TYPES = %w(ActiveRecord ActiveSupport) def initialize(object) @object = object diff --git a/lib/awesome_print/types.rb b/lib/awesome_print/types.rb index 17fee980..20013edd 100644 --- a/lib/awesome_print/types.rb +++ b/lib/awesome_print/types.rb @@ -1,5 +1,7 @@ module AwesomePrint module Types + require 'awesome_print/types/base' require 'awesome_print/types/active_record' if defined?(ActiveRecord) || AwesomePrint.rails_console? + require 'awesome_print/types/active_support' if defined?(ActiveSupport) || AwesomePrint.rails_console? end end diff --git a/lib/awesome_print/types/active_record.rb b/lib/awesome_print/types/active_record.rb index c668f594..1dd05a1b 100644 --- a/lib/awesome_print/types/active_record.rb +++ b/lib/awesome_print/types/active_record.rb @@ -1,10 +1,6 @@ module AwesomePrint module Types - class ActiveRecord - - def initialize(object) - @object = object - end + class ActiveRecord < Base def call if object.is_a?(::ActiveRecord::Base) @@ -15,10 +11,6 @@ def call :array end end - - private - - attr_reader :object end end end diff --git a/lib/awesome_print/types/active_support.rb b/lib/awesome_print/types/active_support.rb new file mode 100644 index 00000000..b67ef7fb --- /dev/null +++ b/lib/awesome_print/types/active_support.rb @@ -0,0 +1,18 @@ +if defined?(ActiveSupport::LogSubscriber) + AwesomePrint.force_colors! ActiveSupport::LogSubscriber.colorize_logging +end + +module AwesomePrint + module Types + class ActiveSupport < Base + + def call + if (defined?(::ActiveSupport::TimeWithZone) && object.is_a?(::ActiveSupport::TimeWithZone)) || object.is_a?(::Date) + :active_support_time + elsif object.is_a?(::HashWithIndifferentAccess) + :hash_with_indifferent_access + end + end + end + end +end diff --git a/lib/awesome_print/types/base.rb b/lib/awesome_print/types/base.rb new file mode 100644 index 00000000..f11aa4ce --- /dev/null +++ b/lib/awesome_print/types/base.rb @@ -0,0 +1,14 @@ +module AwesomePrint + module Types + class Base + + def initialize(object) + @object = object + end + + private + + attr_reader :object + end + end +end From 0f5d96b5919f7ef72819a1b5ffe3fa83720a812d Mon Sep 17 00:00:00 2001 From: Mauro George Date: Tue, 17 Mar 2015 18:47:58 -0300 Subject: [PATCH 40/59] Create Mongoid type --- lib/awesome_print.rb | 1 - lib/awesome_print/ext/mongoid.rb | 27 --------------------------- lib/awesome_print/type_discover.rb | 2 +- lib/awesome_print/types.rb | 1 + lib/awesome_print/types/mongoid.rb | 16 ++++++++++++++++ 5 files changed, 18 insertions(+), 29 deletions(-) delete mode 100644 lib/awesome_print/ext/mongoid.rb create mode 100644 lib/awesome_print/types/mongoid.rb diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index 477fee89..80d203a4 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -23,7 +23,6 @@ end end 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/nobrainer" if defined?(NoBrainer) require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple) diff --git a/lib/awesome_print/ext/mongoid.rb b/lib/awesome_print/ext/mongoid.rb deleted file mode 100644 index 0a8d0165..00000000 --- a/lib/awesome_print/ext/mongoid.rb +++ /dev/null @@ -1,27 +0,0 @@ -module AwesomePrint - module Mongoid - - def self.included(base) - base.send :alias_method, :cast_without_mongoid, :cast - base.send :alias_method, :cast, :cast_with_mongoid - end - - # Add Mongoid class names to the dispatcher pipeline. - #------------------------------------------------------------------------------ - def cast_with_mongoid(object, type) - cast = cast_without_mongoid(object, type) - if defined?(::Mongoid::Document) - if object.is_a?(Class) && object.ancestors.include?(::Mongoid::Document) - cast = :mongoid_class - elsif object.class.ancestors.include?(::Mongoid::Document) - cast = :mongoid_document - elsif (defined?(::BSON) && object.is_a?(::BSON::ObjectId)) || (defined?(::Moped::BSON) && object.is_a?(::Moped::BSON::ObjectId)) - cast = :mongoid_bson_id - end - end - cast - end - end -end - -AwesomePrint::Formatter.send(:include, AwesomePrint::Mongoid) diff --git a/lib/awesome_print/type_discover.rb b/lib/awesome_print/type_discover.rb index 4c83ad34..18e83ac0 100644 --- a/lib/awesome_print/type_discover.rb +++ b/lib/awesome_print/type_discover.rb @@ -3,7 +3,7 @@ module AwesomePrint class TypeDiscover - TYPES = %w(ActiveRecord ActiveSupport) + TYPES = %w(ActiveRecord ActiveSupport Mongoid) def initialize(object) @object = object diff --git a/lib/awesome_print/types.rb b/lib/awesome_print/types.rb index 20013edd..ff69ff8f 100644 --- a/lib/awesome_print/types.rb +++ b/lib/awesome_print/types.rb @@ -3,5 +3,6 @@ module Types require 'awesome_print/types/base' require 'awesome_print/types/active_record' if defined?(ActiveRecord) || AwesomePrint.rails_console? require 'awesome_print/types/active_support' if defined?(ActiveSupport) || AwesomePrint.rails_console? + require 'awesome_print/types/mongoid' if defined?(Mongoid) end end diff --git a/lib/awesome_print/types/mongoid.rb b/lib/awesome_print/types/mongoid.rb new file mode 100644 index 00000000..2650b944 --- /dev/null +++ b/lib/awesome_print/types/mongoid.rb @@ -0,0 +1,16 @@ +module AwesomePrint + module Types + class Mongoid < Base + + def call + if object.is_a?(Class) && object.ancestors.include?(::Mongoid::Document) + :mongoid_class + elsif object.class.ancestors.include?(::Mongoid::Document) + :mongoid_document + elsif (defined?(::BSON) && object.is_a?(::BSON::ObjectId)) || (defined?(::Moped::BSON) && object.is_a?(::Moped::BSON::ObjectId)) + :mongoid_bson_id + end + end + end + end +end From 4d6e96334e84d49ebe45ca9d22e4f244cc494abe Mon Sep 17 00:00:00 2001 From: Mauro George Date: Tue, 17 Mar 2015 18:57:44 -0300 Subject: [PATCH 41/59] Create MongoMapper type --- lib/awesome_print.rb | 1 - lib/awesome_print/ext/mongo_mapper.rb | 31 ------------------------- lib/awesome_print/type_discover.rb | 2 +- lib/awesome_print/types.rb | 1 + lib/awesome_print/types/mongo_mapper.rb | 18 ++++++++++++++ 5 files changed, 20 insertions(+), 33 deletions(-) delete mode 100644 lib/awesome_print/ext/mongo_mapper.rb create mode 100644 lib/awesome_print/types/mongo_mapper.rb diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index 80d203a4..123e2f36 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -22,7 +22,6 @@ require File.dirname(__FILE__) + "/awesome_print/ext/action_view" end end - require File.dirname(__FILE__) + "/awesome_print/ext/mongo_mapper" if defined?(MongoMapper) require File.dirname(__FILE__) + "/awesome_print/ext/nokogiri" if defined?(Nokogiri) require File.dirname(__FILE__) + "/awesome_print/ext/nobrainer" if defined?(NoBrainer) require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple) diff --git a/lib/awesome_print/ext/mongo_mapper.rb b/lib/awesome_print/ext/mongo_mapper.rb deleted file mode 100644 index 457166c5..00000000 --- a/lib/awesome_print/ext/mongo_mapper.rb +++ /dev/null @@ -1,31 +0,0 @@ -module AwesomePrint - module MongoMapper - - def self.included(base) - base.send :alias_method, :cast_without_mongo_mapper, :cast - base.send :alias_method, :cast, :cast_with_mongo_mapper - end - - # Add MongoMapper class names to the dispatcher pipeline. - #------------------------------------------------------------------------------ - def cast_with_mongo_mapper(object, type) - cast = cast_without_mongo_mapper(object, type) - - if defined?(::MongoMapper::Document) - if object.is_a?(Class) && (object.ancestors & [ ::MongoMapper::Document, ::MongoMapper::EmbeddedDocument ]).size > 0 - cast = :mongo_mapper_class - elsif object.is_a?(::MongoMapper::Document) || object.is_a?(::MongoMapper::EmbeddedDocument) - cast = :mongo_mapper_instance - elsif object.is_a?(::MongoMapper::Plugins::Associations::Base) - cast = :mongo_mapper_association - elsif object.is_a?(::BSON::ObjectId) - cast = :mongo_mapper_bson_id - end - end - - cast - end - end -end - -AwesomePrint::Formatter.send(:include, AwesomePrint::MongoMapper) diff --git a/lib/awesome_print/type_discover.rb b/lib/awesome_print/type_discover.rb index 18e83ac0..164073cd 100644 --- a/lib/awesome_print/type_discover.rb +++ b/lib/awesome_print/type_discover.rb @@ -3,7 +3,7 @@ module AwesomePrint class TypeDiscover - TYPES = %w(ActiveRecord ActiveSupport Mongoid) + TYPES = %w(ActiveRecord ActiveSupport Mongoid MongoMapper) def initialize(object) @object = object diff --git a/lib/awesome_print/types.rb b/lib/awesome_print/types.rb index ff69ff8f..54a994df 100644 --- a/lib/awesome_print/types.rb +++ b/lib/awesome_print/types.rb @@ -4,5 +4,6 @@ module Types require 'awesome_print/types/active_record' if defined?(ActiveRecord) || AwesomePrint.rails_console? require 'awesome_print/types/active_support' if defined?(ActiveSupport) || AwesomePrint.rails_console? require 'awesome_print/types/mongoid' if defined?(Mongoid) + require 'awesome_print/types/mongo_mapper' if defined?(MongoMapper) end end diff --git a/lib/awesome_print/types/mongo_mapper.rb b/lib/awesome_print/types/mongo_mapper.rb new file mode 100644 index 00000000..b9bc9389 --- /dev/null +++ b/lib/awesome_print/types/mongo_mapper.rb @@ -0,0 +1,18 @@ +module AwesomePrint + module Types + class MongoMapper < Base + + def call + if object.is_a?(Class) && (object.ancestors & [ ::MongoMapper::Document, ::MongoMapper::EmbeddedDocument ]).size > 0 + :mongo_mapper_class + elsif object.is_a?(::MongoMapper::Document) || object.is_a?(::MongoMapper::EmbeddedDocument) + :mongo_mapper_instance + elsif object.is_a?(::MongoMapper::Plugins::Associations::Base) + :mongo_mapper_association + elsif object.is_a?(::BSON::ObjectId) + :mongo_mapper_bson_id + end + end + end + end +end From ea274a516c2a986f6fd164eb0b3b4cbb5a30077e Mon Sep 17 00:00:00 2001 From: Mauro George Date: Tue, 17 Mar 2015 19:01:57 -0300 Subject: [PATCH 42/59] Create NoBrainer type --- lib/awesome_print.rb | 1 - lib/awesome_print/ext/nobrainer.rb | 25 ------------------------- lib/awesome_print/type_discover.rb | 2 +- lib/awesome_print/types.rb | 1 + lib/awesome_print/types/no_brainer.rb | 14 ++++++++++++++ 5 files changed, 16 insertions(+), 27 deletions(-) delete mode 100644 lib/awesome_print/ext/nobrainer.rb create mode 100644 lib/awesome_print/types/no_brainer.rb diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index 123e2f36..64daa8db 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -23,7 +23,6 @@ end end require File.dirname(__FILE__) + "/awesome_print/ext/nokogiri" if defined?(Nokogiri) - require File.dirname(__FILE__) + "/awesome_print/ext/nobrainer" if defined?(NoBrainer) require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple) require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel) require File.dirname(__FILE__) + "/awesome_print/ext/ostruct" if defined?(OpenStruct) diff --git a/lib/awesome_print/ext/nobrainer.rb b/lib/awesome_print/ext/nobrainer.rb deleted file mode 100644 index 4857d488..00000000 --- a/lib/awesome_print/ext/nobrainer.rb +++ /dev/null @@ -1,25 +0,0 @@ -module AwesomePrint - module NoBrainer - - def self.included(base) - base.send :alias_method, :cast_without_nobrainer, :cast - base.send :alias_method, :cast, :cast_with_nobrainer - end - - # Add NoBrainer class names to the dispatcher pipeline. - #------------------------------------------------------------------------------ - def cast_with_nobrainer(object, type) - cast = cast_without_nobrainer(object, type) - if defined?(::NoBrainer::Document) - if object.is_a?(Class) && object < ::NoBrainer::Document - cast = :nobrainer_class - elsif object.is_a?(::NoBrainer::Document) - cast = :nobrainer_document - end - end - cast - end - end -end - -AwesomePrint::Formatter.send(:include, AwesomePrint::NoBrainer) diff --git a/lib/awesome_print/type_discover.rb b/lib/awesome_print/type_discover.rb index 164073cd..c93ed921 100644 --- a/lib/awesome_print/type_discover.rb +++ b/lib/awesome_print/type_discover.rb @@ -3,7 +3,7 @@ module AwesomePrint class TypeDiscover - TYPES = %w(ActiveRecord ActiveSupport Mongoid MongoMapper) + TYPES = %w(ActiveRecord ActiveSupport Mongoid MongoMapper NoBrainer) def initialize(object) @object = object diff --git a/lib/awesome_print/types.rb b/lib/awesome_print/types.rb index 54a994df..bda7111c 100644 --- a/lib/awesome_print/types.rb +++ b/lib/awesome_print/types.rb @@ -5,5 +5,6 @@ module Types require 'awesome_print/types/active_support' if defined?(ActiveSupport) || AwesomePrint.rails_console? require 'awesome_print/types/mongoid' if defined?(Mongoid) require 'awesome_print/types/mongo_mapper' if defined?(MongoMapper) + require 'awesome_print/types/no_brainer' if defined?(NoBrainer) end end diff --git a/lib/awesome_print/types/no_brainer.rb b/lib/awesome_print/types/no_brainer.rb new file mode 100644 index 00000000..8b17c943 --- /dev/null +++ b/lib/awesome_print/types/no_brainer.rb @@ -0,0 +1,14 @@ +module AwesomePrint + module Types + class NoBrainer < Base + + def call + if object.is_a?(Class) && object < ::NoBrainer::Document + :nobrainer_class + elsif object.is_a?(::NoBrainer::Document) + :nobrainer_document + end + end + end + end +end From 9168b0d9b8a1d71313c0c3b1b6c12e789287aece Mon Sep 17 00:00:00 2001 From: Mauro George Date: Tue, 17 Mar 2015 19:12:52 -0300 Subject: [PATCH 43/59] Create Nokogiri type --- lib/awesome_print.rb | 1 - lib/awesome_print/ext/nokogiri.rb | 22 ---------------------- lib/awesome_print/type_discover.rb | 2 +- lib/awesome_print/types.rb | 1 + lib/awesome_print/types/nokogiri.rb | 13 +++++++++++++ 5 files changed, 15 insertions(+), 24 deletions(-) delete mode 100644 lib/awesome_print/ext/nokogiri.rb create mode 100644 lib/awesome_print/types/nokogiri.rb diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index 64daa8db..0f724063 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -22,7 +22,6 @@ require File.dirname(__FILE__) + "/awesome_print/ext/action_view" end end - require File.dirname(__FILE__) + "/awesome_print/ext/nokogiri" if defined?(Nokogiri) require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple) require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel) require File.dirname(__FILE__) + "/awesome_print/ext/ostruct" if defined?(OpenStruct) diff --git a/lib/awesome_print/ext/nokogiri.rb b/lib/awesome_print/ext/nokogiri.rb deleted file mode 100644 index fc8f4f18..00000000 --- a/lib/awesome_print/ext/nokogiri.rb +++ /dev/null @@ -1,22 +0,0 @@ -module AwesomePrint - module Nokogiri - - def self.included(base) - base.send :alias_method, :cast_without_nokogiri, :cast - base.send :alias_method, :cast, :cast_with_nokogiri - end - - # Add Nokogiri XML Node and NodeSet names to the dispatcher pipeline. - #------------------------------------------------------------------------------ - def cast_with_nokogiri(object, type) - cast = cast_without_nokogiri(object, type) - if (defined?(::Nokogiri::XML::Node) && object.is_a?(::Nokogiri::XML::Node)) || - (defined?(::Nokogiri::XML::NodeSet) && object.is_a?(::Nokogiri::XML::NodeSet)) - cast = :nokogiri_xml_node - end - cast - end - end -end - -AwesomePrint::Formatter.send(:include, AwesomePrint::Nokogiri) diff --git a/lib/awesome_print/type_discover.rb b/lib/awesome_print/type_discover.rb index c93ed921..2fc3d3bf 100644 --- a/lib/awesome_print/type_discover.rb +++ b/lib/awesome_print/type_discover.rb @@ -3,7 +3,7 @@ module AwesomePrint class TypeDiscover - TYPES = %w(ActiveRecord ActiveSupport Mongoid MongoMapper NoBrainer) + TYPES = %w(ActiveRecord ActiveSupport Mongoid MongoMapper NoBrainer Nokogiri) def initialize(object) @object = object diff --git a/lib/awesome_print/types.rb b/lib/awesome_print/types.rb index bda7111c..02823d0c 100644 --- a/lib/awesome_print/types.rb +++ b/lib/awesome_print/types.rb @@ -6,5 +6,6 @@ module Types require 'awesome_print/types/mongoid' if defined?(Mongoid) require 'awesome_print/types/mongo_mapper' if defined?(MongoMapper) require 'awesome_print/types/no_brainer' if defined?(NoBrainer) + require 'awesome_print/types/nokogiri' if defined?(Nokogiri) end end diff --git a/lib/awesome_print/types/nokogiri.rb b/lib/awesome_print/types/nokogiri.rb new file mode 100644 index 00000000..e53e56ef --- /dev/null +++ b/lib/awesome_print/types/nokogiri.rb @@ -0,0 +1,13 @@ +module AwesomePrint + module Types + class Nokogiri < Base + + def call + if (defined?(::Nokogiri::XML::Node) && object.is_a?(::Nokogiri::XML::Node)) || + (defined?(::Nokogiri::XML::NodeSet) && object.is_a?(::Nokogiri::XML::NodeSet)) + :nokogiri_xml_node + end + end + end + end +end From ab63f91f57989c15b72b4aa09a999536f5abb653 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Tue, 17 Mar 2015 19:16:48 -0300 Subject: [PATCH 44/59] Create OpenStruct type --- lib/awesome_print.rb | 1 - lib/awesome_print/ext/ostruct.rb | 18 ------------------ lib/awesome_print/type_discover.rb | 2 +- lib/awesome_print/types.rb | 1 + lib/awesome_print/types/ostruct.rb | 12 ++++++++++++ 5 files changed, 14 insertions(+), 20 deletions(-) delete mode 100644 lib/awesome_print/ext/ostruct.rb create mode 100644 lib/awesome_print/types/ostruct.rb diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index 0f724063..f9019f66 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -24,5 +24,4 @@ end require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple) require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel) - require File.dirname(__FILE__) + "/awesome_print/ext/ostruct" if defined?(OpenStruct) end diff --git a/lib/awesome_print/ext/ostruct.rb b/lib/awesome_print/ext/ostruct.rb deleted file mode 100644 index df3cb4a6..00000000 --- a/lib/awesome_print/ext/ostruct.rb +++ /dev/null @@ -1,18 +0,0 @@ -module AwesomePrint - module OpenStruct - def self.included(base) - base.send :alias_method, :cast_without_ostruct, :cast - base.send :alias_method, :cast, :cast_with_ostruct - end - - def cast_with_ostruct(object, type) - cast = cast_without_ostruct(object, type) - if (defined?(::OpenStruct)) && (object.is_a?(::OpenStruct)) - cast = :open_struct_instance - end - cast - end - end -end - -AwesomePrint::Formatter.send(:include, AwesomePrint::OpenStruct) diff --git a/lib/awesome_print/type_discover.rb b/lib/awesome_print/type_discover.rb index 2fc3d3bf..0a6092e4 100644 --- a/lib/awesome_print/type_discover.rb +++ b/lib/awesome_print/type_discover.rb @@ -3,7 +3,7 @@ module AwesomePrint class TypeDiscover - TYPES = %w(ActiveRecord ActiveSupport Mongoid MongoMapper NoBrainer Nokogiri) + TYPES = %w(ActiveRecord ActiveSupport Mongoid MongoMapper NoBrainer Nokogiri OpenStruct) def initialize(object) @object = object diff --git a/lib/awesome_print/types.rb b/lib/awesome_print/types.rb index 02823d0c..4eb6b756 100644 --- a/lib/awesome_print/types.rb +++ b/lib/awesome_print/types.rb @@ -7,5 +7,6 @@ module Types require 'awesome_print/types/mongo_mapper' if defined?(MongoMapper) require 'awesome_print/types/no_brainer' if defined?(NoBrainer) require 'awesome_print/types/nokogiri' if defined?(Nokogiri) + require 'awesome_print/types/ostruct' if defined?(OpenStruct) end end diff --git a/lib/awesome_print/types/ostruct.rb b/lib/awesome_print/types/ostruct.rb new file mode 100644 index 00000000..8c720c25 --- /dev/null +++ b/lib/awesome_print/types/ostruct.rb @@ -0,0 +1,12 @@ +module AwesomePrint + module Types + class OpenStruct < Base + + def call + if (defined?(::OpenStruct)) && (object.is_a?(::OpenStruct)) + :open_struct_instance + end + end + end + end +end From ad175c1d1989da30d1a45cd03cbf90520f528a5c Mon Sep 17 00:00:00 2001 From: Mauro George Date: Tue, 17 Mar 2015 19:22:50 -0300 Subject: [PATCH 45/59] Create Ripple type --- lib/awesome_print.rb | 1 - lib/awesome_print/ext/ripple.rb | 25 ------------------------- lib/awesome_print/type_discover.rb | 3 ++- lib/awesome_print/types.rb | 1 + lib/awesome_print/types/ripple.rb | 14 ++++++++++++++ 5 files changed, 17 insertions(+), 27 deletions(-) delete mode 100644 lib/awesome_print/ext/ripple.rb create mode 100644 lib/awesome_print/types/ripple.rb diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index f9019f66..bb11db64 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -22,6 +22,5 @@ require File.dirname(__FILE__) + "/awesome_print/ext/action_view" end end - require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple) require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel) end diff --git a/lib/awesome_print/ext/ripple.rb b/lib/awesome_print/ext/ripple.rb deleted file mode 100644 index ee78bc6b..00000000 --- a/lib/awesome_print/ext/ripple.rb +++ /dev/null @@ -1,25 +0,0 @@ -module AwesomePrint - module Ripple - - def self.included(base) - base.send :alias_method, :cast_without_ripple, :cast - base.send :alias_method, :cast, :cast_with_ripple - end - - # Add Ripple class names to the dispatcher pipeline. - #------------------------------------------------------------------------------ - def cast_with_ripple(object, type) - cast = cast_without_ripple(object, type) - return cast if !defined?(::Ripple) - - if object.is_a?(::Ripple::AttributeMethods) # Module used to access attributes across documents and embedded documents - cast = :ripple_document_instance - elsif object.is_a?(::Ripple::Properties) # Used to access property metadata on Ripple classes - cast = :ripple_document_class - end - cast - end - end -end - -AwesomePrint::Formatter.send(:include, AwesomePrint::Ripple) diff --git a/lib/awesome_print/type_discover.rb b/lib/awesome_print/type_discover.rb index 0a6092e4..58bd75ad 100644 --- a/lib/awesome_print/type_discover.rb +++ b/lib/awesome_print/type_discover.rb @@ -3,7 +3,8 @@ module AwesomePrint class TypeDiscover - TYPES = %w(ActiveRecord ActiveSupport Mongoid MongoMapper NoBrainer Nokogiri OpenStruct) + TYPES = %w(ActiveRecord ActiveSupport Mongoid MongoMapper NoBrainer Nokogiri + OpenStruct Ripple) def initialize(object) @object = object diff --git a/lib/awesome_print/types.rb b/lib/awesome_print/types.rb index 4eb6b756..45015109 100644 --- a/lib/awesome_print/types.rb +++ b/lib/awesome_print/types.rb @@ -8,5 +8,6 @@ module Types require 'awesome_print/types/no_brainer' if defined?(NoBrainer) require 'awesome_print/types/nokogiri' if defined?(Nokogiri) require 'awesome_print/types/ostruct' if defined?(OpenStruct) + require 'awesome_print/types/ripple' if defined?(Ripple) end end diff --git a/lib/awesome_print/types/ripple.rb b/lib/awesome_print/types/ripple.rb new file mode 100644 index 00000000..e6cd8f56 --- /dev/null +++ b/lib/awesome_print/types/ripple.rb @@ -0,0 +1,14 @@ +module AwesomePrint + module Types + class Ripple < Base + + def call + if object.is_a?(::Ripple::AttributeMethods) # Module used to access attributes across documents and embedded documents + :ripple_document_instance + elsif object.is_a?(::Ripple::Properties) # Used to access property metadata on Ripple classes + :ripple_document_class + end + end + end + end +end From 9cb3a15e91972915e2de90cfcace24998a33f779 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Tue, 17 Mar 2015 19:26:16 -0300 Subject: [PATCH 46/59] Create Sequel type --- lib/awesome_print.rb | 1 - lib/awesome_print/ext/sequel.rb | 25 ------------------------- lib/awesome_print/type_discover.rb | 2 +- lib/awesome_print/types.rb | 1 + lib/awesome_print/types/sequel.rb | 16 ++++++++++++++++ 5 files changed, 18 insertions(+), 27 deletions(-) delete mode 100644 lib/awesome_print/ext/sequel.rb create mode 100644 lib/awesome_print/types/sequel.rb diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index bb11db64..13411029 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -22,5 +22,4 @@ require File.dirname(__FILE__) + "/awesome_print/ext/action_view" end end - 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 deleted file mode 100644 index 0a334aae..00000000 --- a/lib/awesome_print/ext/sequel.rb +++ /dev/null @@ -1,25 +0,0 @@ -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.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 - cast - end - end -end - -AwesomePrint::Formatter.send(:include, AwesomePrint::Sequel) diff --git a/lib/awesome_print/type_discover.rb b/lib/awesome_print/type_discover.rb index 58bd75ad..d4084000 100644 --- a/lib/awesome_print/type_discover.rb +++ b/lib/awesome_print/type_discover.rb @@ -4,7 +4,7 @@ module AwesomePrint class TypeDiscover TYPES = %w(ActiveRecord ActiveSupport Mongoid MongoMapper NoBrainer Nokogiri - OpenStruct Ripple) + OpenStruct Ripple Sequel) def initialize(object) @object = object diff --git a/lib/awesome_print/types.rb b/lib/awesome_print/types.rb index 45015109..cc539005 100644 --- a/lib/awesome_print/types.rb +++ b/lib/awesome_print/types.rb @@ -9,5 +9,6 @@ module Types require 'awesome_print/types/nokogiri' if defined?(Nokogiri) require 'awesome_print/types/ostruct' if defined?(OpenStruct) require 'awesome_print/types/ripple' if defined?(Ripple) + require 'awesome_print/types/sequel' if defined?(Sequel) end end diff --git a/lib/awesome_print/types/sequel.rb b/lib/awesome_print/types/sequel.rb new file mode 100644 index 00000000..c5dc9994 --- /dev/null +++ b/lib/awesome_print/types/sequel.rb @@ -0,0 +1,16 @@ +module AwesomePrint + module Types + class Sequel < Base + + def call + if defined?(::Sequel::Model) && object.is_a?(::Sequel::Model) + :sequel_document + elsif defined?(::Sequel::Model) && object.is_a?(Class) && object.ancestors.include?(::Sequel::Model) + :sequel_model_class + elsif defined?(::Sequel::Mysql2::Dataset) && object.class.ancestors.include?(::Sequel::Mysql2::Dataset) + :sequel_dataset + end + end + end + end +end From 7695cfcbe65033a8c9e669fdc5c32c5ff9e37e47 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Tue, 17 Mar 2015 19:30:50 -0300 Subject: [PATCH 47/59] Simple load of files on AwesomePrint --- lib/awesome_print.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index 13411029..5f6d3f21 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -4,22 +4,22 @@ # unless defined?(AwesomePrint::Inspector) %w(array string method object class kernel).each do |file| - require File.dirname(__FILE__) + "/awesome_print/core_ext/#{file}" + require "awesome_print/core_ext/#{file}" end - require File.dirname(__FILE__) + "/awesome_print/support" - require File.dirname(__FILE__) + "/awesome_print/configuration" - require File.dirname(__FILE__) + "/awesome_print/inspector" - require File.dirname(__FILE__) + "/awesome_print/formatter" - require File.dirname(__FILE__) + "/awesome_print/version" - require File.dirname(__FILE__) + "/awesome_print/core_ext/logger" if defined?(Logger) + require 'awesome_print/support' + require 'awesome_print/configuration' + require 'awesome_print/inspector' + require 'awesome_print/formatter' + require 'awesome_print/version' + require 'awesome_print/core_ext/logger' if defined?(Logger) # # Load remaining extensions. # if defined?(ActiveSupport) ActiveSupport.on_load(:action_view) do - require File.dirname(__FILE__) + "/awesome_print/ext/action_view" + require 'awesome_print/ext/action_view' end end end From a9629ba578f725b991a545fc24fb6ed2fa762128 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 18 Mar 2015 18:38:58 -0300 Subject: [PATCH 48/59] Move some methods to the right place --- lib/awesome_print/formatter.rb | 54 ++++++++++++++++------ lib/awesome_print/formatter_factory.rb | 5 +- lib/awesome_print/formatters/enumerable.rb | 4 +- lib/awesome_print/inspector.rb | 38 +-------------- lib/awesome_print/type_discover.rb | 34 +++++++++----- 5 files changed, 69 insertions(+), 66 deletions(-) diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 531cc633..7dd8297b 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -6,10 +6,7 @@ module AwesomePrint class Formatter - CORE = [ :array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod ] - DEFAULT_LIMIT_SIZE = 7 - - attr_reader :options, :inspector, :indentation, :type + attr_reader :options, :inspector, :indentation, :type, :object def initialize(inspector) @inspector = inspector @@ -19,17 +16,10 @@ def initialize(inspector) # Main entry point to format an object. #------------------------------------------------------------------------------ - def format(object, type = nil) - @type = type - core_class = cast(object, type) - AwesomePrint::FormatterFactory.new(core_class, self, object).call - end - - # Hook this when adding custom formatters. Check out lib/awesome_print/ext - # directory for custom formatters that ship with awesome_print. - #------------------------------------------------------------------------------ - def cast(object, type) - AwesomePrint::TypeDiscover.new(object).call || CORE.grep(type)[0] || :self + def format(object) + @type = printable(object) + @object = object + AwesomePrint::FormatterFactory.new(self, object).call end # Pick the color and apply it to the given string as necessary. @@ -85,5 +75,39 @@ def left_aligned ensure @options[:indent] = current end + + # Format nested data, for example: + # arr = [1, 2]; arr << arr + # => [1,2, [...]] + # hash = { :a => 1 }; hash[:b] = hash + # => { :a => 1, :b => {...} } + #------------------------------------------------------------------------------ + def nested(object) + case printable(object) + when :array then colorize("[...]", :array) + when :hash then colorize("{...}", :hash) + when :struct then colorize("{...}", :struct) + else colorize("...#{object.class}...", :class) + end + end + + #------------------------------------------------------------------------------ + def unnested(object) + format(object) + end + + # Turn class name into symbol, ex: Hello::World => :hello_world. Classes that + # inherit from Array, Hash, File, Dir, and Struct are treated as the base class. + #------------------------------------------------------------------------------ + def printable(object) + case object + when Array then :array + when Hash then :hash + when File then :file + when Dir then :dir + when Struct then :struct + else object.class.to_s.gsub(/:+/, "_").downcase.to_sym + end + end end end diff --git a/lib/awesome_print/formatter_factory.rb b/lib/awesome_print/formatter_factory.rb index 29d5fd6a..98e63469 100644 --- a/lib/awesome_print/formatter_factory.rb +++ b/lib/awesome_print/formatter_factory.rb @@ -3,8 +3,9 @@ module AwesomePrint class FormatterFactory - def initialize(class_name, formatter, object) - @class_name = class_name.to_s.split('_').map(&:capitalize).join('') + def initialize(formatter, object) + @type = AwesomePrint::TypeDiscover.new(formatter).call + @class_name = @type.to_s.split('_').map(&:capitalize).join('') @formatter = formatter @object = object end diff --git a/lib/awesome_print/formatters/enumerable.rb b/lib/awesome_print/formatters/enumerable.rb index a5c96339..a31601ea 100644 --- a/lib/awesome_print/formatters/enumerable.rb +++ b/lib/awesome_print/formatters/enumerable.rb @@ -2,6 +2,8 @@ module AwesomePrint module Formatters module Enumerable + DEFAULT_LIMIT_SIZE = 7 + # To support limited output, for example: # # ap ('a'..'z').to_a, :limit => 3 @@ -51,7 +53,7 @@ def limited(data, width, is_hash = false) end def get_limit_size - options[:limit] == true ? AwesomePrint::Formatter::DEFAULT_LIMIT_SIZE : options[:limit] + options[:limit] == true ? DEFAULT_LIMIT_SIZE : options[:limit] end end end diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index e887618a..dc3d257d 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -49,11 +49,11 @@ def initialize(options = {}) #------------------------------------------------------------------------------ def awesome(object) if Thread.current[AP].include?(object.object_id) - nested(object) + @formatter.nested(object) else begin Thread.current[AP] << object.object_id - unnested(object) + @formatter.unnested(object) ensure Thread.current[AP].pop end @@ -69,40 +69,6 @@ def colorize? private - # Format nested data, for example: - # arr = [1, 2]; arr << arr - # => [1,2, [...]] - # hash = { :a => 1 }; hash[:b] = hash - # => { :a => 1, :b => {...} } - #------------------------------------------------------------------------------ - def nested(object) - case printable(object) - when :array then @formatter.colorize("[...]", :array) - when :hash then @formatter.colorize("{...}", :hash) - when :struct then @formatter.colorize("{...}", :struct) - else @formatter.colorize("...#{object.class}...", :class) - end - end - - #------------------------------------------------------------------------------ - def unnested(object) - @formatter.format(object, printable(object)) - end - - # Turn class name into symbol, ex: Hello::World => :hello_world. Classes that - # inherit from Array, Hash, File, Dir, and Struct are treated as the base class. - #------------------------------------------------------------------------------ - def printable(object) - case object - when Array then :array - when Hash then :hash - when File then :file - when Dir then :dir - when Struct then :struct - else object.class.to_s.gsub(/:+/, "_").downcase.to_sym - end - end - # Update @options by first merging the :color hash and then the remaining keys. #------------------------------------------------------------------------------ def merge_options!(options = {}) diff --git a/lib/awesome_print/type_discover.rb b/lib/awesome_print/type_discover.rb index d4084000..6723a7c3 100644 --- a/lib/awesome_print/type_discover.rb +++ b/lib/awesome_print/type_discover.rb @@ -3,26 +3,36 @@ module AwesomePrint class TypeDiscover - TYPES = %w(ActiveRecord ActiveSupport Mongoid MongoMapper NoBrainer Nokogiri + BUILT_IN_TYPES = [ :array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod ] + CUSTOM_TYPES = %w(ActiveRecord ActiveSupport Mongoid MongoMapper NoBrainer Nokogiri OpenStruct Ripple Sequel) - def initialize(object) - @object = object + def initialize(formatter) + @type = formatter.type + @object = formatter.object end def call - TYPES.map do |type| - begin - klass = AwesomePrint::Support.constantize("AwesomePrint::Types::#{type}") - klass.new(object).call - rescue NameError - nil - end - end.detect { |type| !type.nil? } + custom_type || built_in_type || :self end private - attr_reader :object + attr_reader :object, :type + + def custom_type + CUSTOM_TYPES.map do |type| + begin + klass = AwesomePrint::Support.constantize("AwesomePrint::Types::#{type}") + klass.new(object).call + rescue NameError + nil + end + end.detect { |type| !type.nil? } + end + + def built_in_type + BUILT_IN_TYPES.grep(type)[0] + end end end From 3f248971fd870fece0ec752a0a7cc550bf685ce9 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 18 Mar 2015 18:42:06 -0300 Subject: [PATCH 49/59] Notify about last release that support old rubies and rails --- README.md | 6 +++--- spec/spec_helper.rb | 7 ------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index fb2b4e51..e7cf4fae 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,9 @@ Awesome Print is a Ruby library that pretty prints Ruby objects in full color exposing their internal structure with proper indentation. Rails ActiveRecord objects and usage within Rails templates are supported via included mixins. -__NOTE__: awesome_print v1.2.0 is the last release supporting Ruby versions -prior to v1.9.3 and Rails versions prior to v3.0. The upcoming awesome_print -v2.0 will *require* Ruby v1.9.3 or later and Rails v3.0 or later. +__NOTE__: awesome_print v1.6.1 is the last release supporting Ruby versions +prior to v1.9.3 and Rails versions prior to v3.2. The upcoming awesome_print +v2.0 will *require* Ruby v1.9.3 or later and Rails v3.2 or later. ### Installation ### # Installing as Ruby gem diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 804b5ff4..1d983c5b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,13 +2,6 @@ # Running specs from the command line: # $ rake spec # Entire spec suite. # $ rspec spec/objects_spec.rb # Individual spec file. -# -# NOTE: To successfully run specs with Ruby 1.8.6 the older versions of -# Bundler and RSpec gems are required: -# -# $ gem install bundler -v=1.0.2 -# $ gem install rspec -v=2.6.0 -# require 'codeclimate-test-reporter' CodeClimate::TestReporter.start $LOAD_PATH.unshift(File.dirname(__FILE__)) From 188becfadfbff3b5fd952e0366bf36c8c6f59b59 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 18 Mar 2015 19:48:00 -0300 Subject: [PATCH 50/59] Add the new_hash_syntax syntax option --- README.md | 17 +++++++------- lib/awesome_print/formatters/hash.rb | 19 +++++++++++++++- lib/awesome_print/inspector.rb | 17 +++++++------- spec/formats_spec.rb | 34 ++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index e7cf4fae..f5e6f069 100644 --- a/README.md +++ b/README.md @@ -33,14 +33,15 @@ ap object, options = {} Default options: ```ruby -:indent => 4, # Indent using 4 spaces. -:index => true, # Display array indices. -:html => false, # Use ANSI color codes rather than HTML. -:multiline => true, # Display in multiple lines. -:plain => false, # Use colors. -:raw => false, # Do not recursively format object instance variables. -:sort_keys => false, # Do not sort hash keys. -:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. +:indent => 4, # Indent using 4 spaces. +:index => true, # Display array indices. +:html => false, # Use ANSI color codes rather than HTML. +:multiline => true, # Display in multiple lines. +:plain => false, # Use colors. +:raw => false, # Do not recursively format object instance variables. +:sort_keys => false, # Do not sort hash keys. +:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. +:new_hash_syntax => false, # Use the foo: 'bar' syntax, when the key is a symbol :color => { :args => :pale, :array => :white, diff --git a/lib/awesome_print/formatters/hash.rb b/lib/awesome_print/formatters/hash.rb index ab34ccb7..0d069c71 100644 --- a/lib/awesome_print/formatters/hash.rb +++ b/lib/awesome_print/formatters/hash.rb @@ -43,7 +43,11 @@ def build_data @data = @data.map do |key, value| formatter.indented do - formatter.align(key, width) << formatter.colorize(" => ", :hash) << inspector.awesome(value) + if options[:new_hash_syntax] && is_a_symbol?(key) + new_hash_syntax_format(key, value) + else + old_hash_syntax_format(key, value) + end end end @@ -65,6 +69,19 @@ def plain_single_line ensure options[:plain], options[:multiline] = plain, multiline end + + def old_hash_syntax_format(key, value) + formatter.align(key, width) << formatter.colorize(" => ", :hash) << inspector.awesome(value) + end + + def new_hash_syntax_format(key, value) + key[0] = '' + formatter.align(key, width - 1) << formatter.colorize(": ", :hash) << inspector.awesome(value) + end + + def is_a_symbol?(key) + key[0] == ':' + end end end end diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index dc3d257d..94634a70 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -6,14 +6,15 @@ class Inspector def initialize(options = {}) @options = { - :indent => 4, # Indent using 4 spaces. - :index => true, # Display array indices. - :html => false, # Use ANSI color codes rather than HTML. - :multiline => true, # Display in multiple lines. - :plain => false, # Use colors. - :raw => false, # Do not recursively format object instance variables. - :sort_keys => false, # Do not sort hash keys. - :limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. + :indent => 4, # Indent using 4 spaces. + :index => true, # Display array indices. + :html => false, # Use ANSI color codes rather than HTML. + :multiline => true, # Display in multiple lines. + :plain => false, # Use colors. + :raw => false, # Do not recursively format object instance variables. + :sort_keys => false, # Do not sort hash keys. + :limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. + :new_hash_syntax => false, # Use the foo: 'bar' syntax, when the key is a symbol :color => { :args => :pale, :array => :white, diff --git a/spec/formats_spec.rb b/spec/formats_spec.rb index 91c8b0bd..cf76cb6d 100644 --- a/spec/formats_spec.rb +++ b/spec/formats_spec.rb @@ -275,6 +275,24 @@ EOS end + it "new hash syntax" do + expect(@hash.ai(:plain => true, :new_hash_syntax => true)).to eq <<-EOS.strip +{ + 1 => { + sym: { + "str" => { + [ 1, 2, 3 ] => { + { k: :v } => Hash < Object + } + } + } + } +} +EOS + end + + + it "plain multiline indented" do expect(@hash.ai(:plain => true, :indent => 1)).to eq <<-EOS.strip { @@ -311,6 +329,22 @@ EOS end + it "colored with new hash syntax" do + expect(@hash.ai(:new_hash_syntax => true)).to eq <<-EOS.strip +{ + 1\e[0;37m => \e[0m{ + sym\e[0;37m: \e[0m{ + \"str\"\e[0;37m => \e[0m{ + [ 1, 2, 3 ]\e[0;37m => \e[0m{ + { k: :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m + } + } + } + } +} +EOS + end + it "colored multiline indented" do expect(@hash.ai(:indent => 2)).to eq <<-EOS.strip { From 43323667e5afb8a8cb47eafba10ed970a7b51afd Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 18 Mar 2015 20:00:12 -0300 Subject: [PATCH 51/59] Add missing CHANGELOG --- CHANGELOG | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index c190ad7a..ecacbc24 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,8 @@ -1.6.2 (unreleased) +2.0.0.beta (unreleased) - Improves spec performance and simplicity (Mauro George) + - Remove the support for Ruby 1.8.x and Rails 2.x (Mauro George) + - New plugin structure, no more unnecessary monkey patching (Mauro George) + - Add the new hash syntax support (Mauro George) - Handle objects that have a custom #to_hash method (Elliot Shank) 1.6.1 From 95c6bfd401a025861c155c0fd669892e4e170a7d Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 18 Mar 2015 20:02:38 -0300 Subject: [PATCH 52/59] Use a single place to define the Gem Version --- awesome_print.gemspec | 4 +++- lib/awesome_print/version.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/awesome_print.gemspec b/awesome_print.gemspec index a00b20dd..6cdd2f5b 100644 --- a/awesome_print.gemspec +++ b/awesome_print.gemspec @@ -1,8 +1,10 @@ require "rake" +require 'awesome_print/version' + Gem::Specification.new do |s| s.name = "awesome_print" - s.version = "1.6.2" + s.version = AwesomePrint.version # s.platform = Gem::Platform::RUBY s.authors = "Michael Dvorkin" s.date = Time.now.strftime("%Y-%m-%d") diff --git a/lib/awesome_print/version.rb b/lib/awesome_print/version.rb index 7bb69fc9..9c7a37dd 100644 --- a/lib/awesome_print/version.rb +++ b/lib/awesome_print/version.rb @@ -1,5 +1,5 @@ module AwesomePrint def self.version - '1.6.2' + '2.0.0.beta' end end From d2a07190c25830d4dbd2eb21da9f6f06358da70d Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 19 Mar 2015 09:19:04 -0300 Subject: [PATCH 53/59] Fix the load of version on gemspec --- awesome_print.gemspec | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/awesome_print.gemspec b/awesome_print.gemspec index 6cdd2f5b..d933fe48 100644 --- a/awesome_print.gemspec +++ b/awesome_print.gemspec @@ -1,5 +1,6 @@ -require "rake" +$:.push File.expand_path('../lib', __FILE__) +require "rake" require 'awesome_print/version' Gem::Specification.new do |s| From de51f832602d4d683adf9d82d45ae91da8922690 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 19 Mar 2015 18:09:53 -0300 Subject: [PATCH 54/59] Rename Base formatter to Formatter --- lib/awesome_print/formatters.rb | 2 +- lib/awesome_print/formatters/active_record_class.rb | 2 +- lib/awesome_print/formatters/active_record_instance.rb | 2 +- lib/awesome_print/formatters/active_support_time.rb | 2 +- lib/awesome_print/formatters/array.rb | 2 +- lib/awesome_print/formatters/bigdecimal.rb | 2 +- lib/awesome_print/formatters/class.rb | 2 +- lib/awesome_print/formatters/dir.rb | 2 +- lib/awesome_print/formatters/file.rb | 2 +- lib/awesome_print/formatters/{base.rb => formatter.rb} | 2 +- lib/awesome_print/formatters/hash.rb | 2 +- lib/awesome_print/formatters/hash_with_indifferent_access.rb | 2 +- lib/awesome_print/formatters/method.rb | 2 +- lib/awesome_print/formatters/mongo_mapper_association.rb | 2 +- lib/awesome_print/formatters/mongo_mapper_bson_id.rb | 2 +- lib/awesome_print/formatters/mongo_mapper_class.rb | 2 +- lib/awesome_print/formatters/mongo_mapper_instance.rb | 2 +- lib/awesome_print/formatters/mongoid_bson_id.rb | 2 +- lib/awesome_print/formatters/mongoid_class.rb | 2 +- lib/awesome_print/formatters/mongoid_document.rb | 2 +- lib/awesome_print/formatters/nobrainer_class.rb | 2 +- lib/awesome_print/formatters/nobrainer_document.rb | 2 +- lib/awesome_print/formatters/nokogiri_xml_node.rb | 2 +- lib/awesome_print/formatters/object.rb | 2 +- lib/awesome_print/formatters/open_struct_instance.rb | 2 +- lib/awesome_print/formatters/rational.rb | 2 +- lib/awesome_print/formatters/ripple_document_class.rb | 2 +- lib/awesome_print/formatters/ripple_document_instance.rb | 2 +- lib/awesome_print/formatters/self.rb | 2 +- lib/awesome_print/formatters/sequel_dataset.rb | 2 +- lib/awesome_print/formatters/sequel_document.rb | 2 +- lib/awesome_print/formatters/sequel_model_class.rb | 2 +- lib/awesome_print/formatters/set.rb | 2 +- lib/awesome_print/formatters/struct.rb | 2 +- lib/awesome_print/formatters/unboundmethod.rb | 2 +- 35 files changed, 35 insertions(+), 35 deletions(-) rename lib/awesome_print/formatters/{base.rb => formatter.rb} (95%) diff --git a/lib/awesome_print/formatters.rb b/lib/awesome_print/formatters.rb index bcead411..5e0b85e4 100644 --- a/lib/awesome_print/formatters.rb +++ b/lib/awesome_print/formatters.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - require 'awesome_print/formatters/base' + require 'awesome_print/formatters/formatter' require 'awesome_print/formatters/self' require 'awesome_print/formatters/array' require 'awesome_print/formatters/hash' diff --git a/lib/awesome_print/formatters/active_record_class.rb b/lib/awesome_print/formatters/active_record_class.rb index de5af6e9..4ad5319b 100644 --- a/lib/awesome_print/formatters/active_record_class.rb +++ b/lib/awesome_print/formatters/active_record_class.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class ActiveRecordClass < Base + class ActiveRecordClass < Formatter def call return object.inspect if not_a_active_record_class? diff --git a/lib/awesome_print/formatters/active_record_instance.rb b/lib/awesome_print/formatters/active_record_instance.rb index df0afd76..45e52b7a 100644 --- a/lib/awesome_print/formatters/active_record_instance.rb +++ b/lib/awesome_print/formatters/active_record_instance.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class ActiveRecordInstance < Base + class ActiveRecordInstance < Formatter # Format ActiveRecord instance object. # diff --git a/lib/awesome_print/formatters/active_support_time.rb b/lib/awesome_print/formatters/active_support_time.rb index b3ccce96..f30e5bc0 100644 --- a/lib/awesome_print/formatters/active_support_time.rb +++ b/lib/awesome_print/formatters/active_support_time.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class ActiveSupportTime < Base + class ActiveSupportTime < Formatter def call formatter.colorize(object.inspect, :time) diff --git a/lib/awesome_print/formatters/array.rb b/lib/awesome_print/formatters/array.rb index f62a6942..75062eb2 100644 --- a/lib/awesome_print/formatters/array.rb +++ b/lib/awesome_print/formatters/array.rb @@ -3,7 +3,7 @@ module AwesomePrint module Formatters - class Array < Base + class Array < Formatter include Enumerable include MethodTuple diff --git a/lib/awesome_print/formatters/bigdecimal.rb b/lib/awesome_print/formatters/bigdecimal.rb index 73625cc2..feea430d 100644 --- a/lib/awesome_print/formatters/bigdecimal.rb +++ b/lib/awesome_print/formatters/bigdecimal.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class Bigdecimal < Base + class Bigdecimal < Formatter def call formatter.colorize(object.to_s('F'), :bigdecimal) diff --git a/lib/awesome_print/formatters/class.rb b/lib/awesome_print/formatters/class.rb index 6c533338..ddc2e2ef 100644 --- a/lib/awesome_print/formatters/class.rb +++ b/lib/awesome_print/formatters/class.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class Class < Base + class Class < Formatter def call if superclass = object.superclass # <-- Assign and test if nil. diff --git a/lib/awesome_print/formatters/dir.rb b/lib/awesome_print/formatters/dir.rb index 383bf4f5..b9b3cd94 100644 --- a/lib/awesome_print/formatters/dir.rb +++ b/lib/awesome_print/formatters/dir.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class Dir < Base + class Dir < Formatter def call ls = `ls -alF #{object.path.shellescape}` diff --git a/lib/awesome_print/formatters/file.rb b/lib/awesome_print/formatters/file.rb index 222021a0..f11ce730 100644 --- a/lib/awesome_print/formatters/file.rb +++ b/lib/awesome_print/formatters/file.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class File < Base + class File < Formatter def call ls = ::File.directory?(object) ? `ls -adlF #{object.path.shellescape}` : `ls -alF #{object.path.shellescape}` diff --git a/lib/awesome_print/formatters/base.rb b/lib/awesome_print/formatters/formatter.rb similarity index 95% rename from lib/awesome_print/formatters/base.rb rename to lib/awesome_print/formatters/formatter.rb index 57d55839..cc963cec 100644 --- a/lib/awesome_print/formatters/base.rb +++ b/lib/awesome_print/formatters/formatter.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class Base + class Formatter def initialize(formatter, object) @formatter = formatter diff --git a/lib/awesome_print/formatters/hash.rb b/lib/awesome_print/formatters/hash.rb index 0d069c71..217d0dc6 100644 --- a/lib/awesome_print/formatters/hash.rb +++ b/lib/awesome_print/formatters/hash.rb @@ -2,7 +2,7 @@ module AwesomePrint module Formatters - class Hash < Base + class Hash < Formatter include Enumerable def call diff --git a/lib/awesome_print/formatters/hash_with_indifferent_access.rb b/lib/awesome_print/formatters/hash_with_indifferent_access.rb index e988c090..37ff5552 100644 --- a/lib/awesome_print/formatters/hash_with_indifferent_access.rb +++ b/lib/awesome_print/formatters/hash_with_indifferent_access.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class HashWithIndifferentAccess < Base + class HashWithIndifferentAccess < Formatter def call AwesomePrint::Formatters::Hash.new(formatter, object).call diff --git a/lib/awesome_print/formatters/method.rb b/lib/awesome_print/formatters/method.rb index b4b6df3f..97c32616 100644 --- a/lib/awesome_print/formatters/method.rb +++ b/lib/awesome_print/formatters/method.rb @@ -2,7 +2,7 @@ module AwesomePrint module Formatters - class Method < Base + class Method < Formatter include MethodTuple def call diff --git a/lib/awesome_print/formatters/mongo_mapper_association.rb b/lib/awesome_print/formatters/mongo_mapper_association.rb index a37f7b50..2a2b5059 100644 --- a/lib/awesome_print/formatters/mongo_mapper_association.rb +++ b/lib/awesome_print/formatters/mongo_mapper_association.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class MongoMapperAssociation < Base + class MongoMapperAssociation < Formatter def call return object.inspect if !defined?(::ActiveSupport::OrderedHash) diff --git a/lib/awesome_print/formatters/mongo_mapper_bson_id.rb b/lib/awesome_print/formatters/mongo_mapper_bson_id.rb index 360ff288..f45788cc 100644 --- a/lib/awesome_print/formatters/mongo_mapper_bson_id.rb +++ b/lib/awesome_print/formatters/mongo_mapper_bson_id.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class MongoMapperBsonId < Base + class MongoMapperBsonId < Formatter def call object.inspect diff --git a/lib/awesome_print/formatters/mongo_mapper_class.rb b/lib/awesome_print/formatters/mongo_mapper_class.rb index d890a0ac..3530c4d9 100644 --- a/lib/awesome_print/formatters/mongo_mapper_class.rb +++ b/lib/awesome_print/formatters/mongo_mapper_class.rb @@ -2,7 +2,7 @@ module AwesomePrint module Formatters - class MongoMapperClass < Base + class MongoMapperClass < Formatter include MongoMapper def call diff --git a/lib/awesome_print/formatters/mongo_mapper_instance.rb b/lib/awesome_print/formatters/mongo_mapper_instance.rb index 60aee52a..fbbb8418 100644 --- a/lib/awesome_print/formatters/mongo_mapper_instance.rb +++ b/lib/awesome_print/formatters/mongo_mapper_instance.rb @@ -2,7 +2,7 @@ module AwesomePrint module Formatters - class MongoMapperInstance < Base + class MongoMapperInstance < Formatter include MongoMapper # Format MongoMapper instance object. diff --git a/lib/awesome_print/formatters/mongoid_bson_id.rb b/lib/awesome_print/formatters/mongoid_bson_id.rb index 7ec4862e..3d0be01e 100644 --- a/lib/awesome_print/formatters/mongoid_bson_id.rb +++ b/lib/awesome_print/formatters/mongoid_bson_id.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class MongoidBsonId < Base + class MongoidBsonId < Formatter def call object.inspect diff --git a/lib/awesome_print/formatters/mongoid_class.rb b/lib/awesome_print/formatters/mongoid_class.rb index 0607e785..bd69f6f9 100644 --- a/lib/awesome_print/formatters/mongoid_class.rb +++ b/lib/awesome_print/formatters/mongoid_class.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class MongoidClass < Base + class MongoidClass < Formatter def call return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:fields) diff --git a/lib/awesome_print/formatters/mongoid_document.rb b/lib/awesome_print/formatters/mongoid_document.rb index 95662767..5b9b9b33 100644 --- a/lib/awesome_print/formatters/mongoid_document.rb +++ b/lib/awesome_print/formatters/mongoid_document.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class MongoidDocument < Base + class MongoidDocument < Formatter def call return object.inspect if !defined?(::ActiveSupport::OrderedHash) diff --git a/lib/awesome_print/formatters/nobrainer_class.rb b/lib/awesome_print/formatters/nobrainer_class.rb index 532d345a..a5ae9691 100644 --- a/lib/awesome_print/formatters/nobrainer_class.rb +++ b/lib/awesome_print/formatters/nobrainer_class.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class NobrainerClass < Base + class NobrainerClass < Formatter def call "class #{object} < #{object.superclass} " << AwesomePrint::Formatters::Hash.new(formatter, columns).call diff --git a/lib/awesome_print/formatters/nobrainer_document.rb b/lib/awesome_print/formatters/nobrainer_document.rb index 64c765b8..879336d0 100644 --- a/lib/awesome_print/formatters/nobrainer_document.rb +++ b/lib/awesome_print/formatters/nobrainer_document.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class NobrainerDocument < Base + class NobrainerDocument < Formatter def call "#{object} #{AwesomePrint::Formatters::Hash.new(formatter, columns).call}" diff --git a/lib/awesome_print/formatters/nokogiri_xml_node.rb b/lib/awesome_print/formatters/nokogiri_xml_node.rb index aa585559..4300d664 100644 --- a/lib/awesome_print/formatters/nokogiri_xml_node.rb +++ b/lib/awesome_print/formatters/nokogiri_xml_node.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class NokogiriXmlNode < Base + class NokogiriXmlNode < Formatter def call if empty? diff --git a/lib/awesome_print/formatters/object.rb b/lib/awesome_print/formatters/object.rb index f609fb59..b0b2917d 100644 --- a/lib/awesome_print/formatters/object.rb +++ b/lib/awesome_print/formatters/object.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class Object < Base + class Object < Formatter def call build_vars diff --git a/lib/awesome_print/formatters/open_struct_instance.rb b/lib/awesome_print/formatters/open_struct_instance.rb index c1b8746e..cca03450 100644 --- a/lib/awesome_print/formatters/open_struct_instance.rb +++ b/lib/awesome_print/formatters/open_struct_instance.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class OpenStructInstance < Base + class OpenStructInstance < Formatter def call "#{object.class} #{AwesomePrint::Formatters::Hash.new(formatter, object.marshal_dump).call}" diff --git a/lib/awesome_print/formatters/rational.rb b/lib/awesome_print/formatters/rational.rb index dea9cbff..1a9da120 100644 --- a/lib/awesome_print/formatters/rational.rb +++ b/lib/awesome_print/formatters/rational.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class Rational < Base + class Rational < Formatter def call formatter.colorize(object.to_s, :rational) diff --git a/lib/awesome_print/formatters/ripple_document_class.rb b/lib/awesome_print/formatters/ripple_document_class.rb index 66bd7b65..fd55c305 100644 --- a/lib/awesome_print/formatters/ripple_document_class.rb +++ b/lib/awesome_print/formatters/ripple_document_class.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class RippleDocumentClass < Base + class RippleDocumentClass < Formatter def call return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:properties) diff --git a/lib/awesome_print/formatters/ripple_document_instance.rb b/lib/awesome_print/formatters/ripple_document_instance.rb index 9f491fa4..8fe0d838 100644 --- a/lib/awesome_print/formatters/ripple_document_instance.rb +++ b/lib/awesome_print/formatters/ripple_document_instance.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class RippleDocumentInstance < Base + class RippleDocumentInstance < Formatter # Format Ripple instance object. # diff --git a/lib/awesome_print/formatters/self.rb b/lib/awesome_print/formatters/self.rb index 5dce77e5..d5b497f2 100644 --- a/lib/awesome_print/formatters/self.rb +++ b/lib/awesome_print/formatters/self.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class Self < Base + class Self < Formatter def call if options[:raw] && object.instance_variables.any? diff --git a/lib/awesome_print/formatters/sequel_dataset.rb b/lib/awesome_print/formatters/sequel_dataset.rb index e7cb34d1..5bfae74a 100644 --- a/lib/awesome_print/formatters/sequel_dataset.rb +++ b/lib/awesome_print/formatters/sequel_dataset.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class SequelDataset < Base + class SequelDataset < Formatter def call [AwesomePrint::Formatters::Array.new(formatter, object.to_a).call, diff --git a/lib/awesome_print/formatters/sequel_document.rb b/lib/awesome_print/formatters/sequel_document.rb index ef959b78..186187ff 100644 --- a/lib/awesome_print/formatters/sequel_document.rb +++ b/lib/awesome_print/formatters/sequel_document.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class SequelDocument < Base + class SequelDocument < Formatter def call data = object.values.sort_by { |key| key.to_s }.inject({}) do |hash, c| diff --git a/lib/awesome_print/formatters/sequel_model_class.rb b/lib/awesome_print/formatters/sequel_model_class.rb index c6cc8486..9510880d 100644 --- a/lib/awesome_print/formatters/sequel_model_class.rb +++ b/lib/awesome_print/formatters/sequel_model_class.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class SequelModelClass < Base + class SequelModelClass < Formatter def call data = object.db_schema.inject({}) {|h, (name,data)| h.merge(name => data[:db_type])} diff --git a/lib/awesome_print/formatters/set.rb b/lib/awesome_print/formatters/set.rb index ea5d6251..01d3bf2b 100644 --- a/lib/awesome_print/formatters/set.rb +++ b/lib/awesome_print/formatters/set.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class Set < Base + class Set < Formatter def call AwesomePrint::Formatters::Array.new(formatter, object.to_a).call diff --git a/lib/awesome_print/formatters/struct.rb b/lib/awesome_print/formatters/struct.rb index 39f05b1f..d4512e16 100644 --- a/lib/awesome_print/formatters/struct.rb +++ b/lib/awesome_print/formatters/struct.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class Struct < Base + class Struct < Formatter # # The code is slightly uglier because of Ruby 1.8.6 quirks: # awesome_hash(Hash[s.members.zip(s.values)]) <-- ArgumentError: odd number of arguments for Hash) diff --git a/lib/awesome_print/formatters/unboundmethod.rb b/lib/awesome_print/formatters/unboundmethod.rb index 70594ee2..5fa5cdfd 100644 --- a/lib/awesome_print/formatters/unboundmethod.rb +++ b/lib/awesome_print/formatters/unboundmethod.rb @@ -1,6 +1,6 @@ module AwesomePrint module Formatters - class Unboundmethod < Base + class Unboundmethod < Formatter def call AwesomePrint::Formatters::Method.new(formatter, object).call From 896e8f7d4ee0df85579806543654f75dc7ff0616 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 19 Mar 2015 18:14:42 -0300 Subject: [PATCH 55/59] Rename Base type to Type --- lib/awesome_print/types.rb | 2 +- lib/awesome_print/types/active_record.rb | 2 +- lib/awesome_print/types/active_support.rb | 2 +- lib/awesome_print/types/mongo_mapper.rb | 2 +- lib/awesome_print/types/mongoid.rb | 2 +- lib/awesome_print/types/no_brainer.rb | 2 +- lib/awesome_print/types/nokogiri.rb | 2 +- lib/awesome_print/types/ostruct.rb | 2 +- lib/awesome_print/types/ripple.rb | 2 +- lib/awesome_print/types/sequel.rb | 2 +- lib/awesome_print/types/{base.rb => type.rb} | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) rename lib/awesome_print/types/{base.rb => type.rb} (91%) diff --git a/lib/awesome_print/types.rb b/lib/awesome_print/types.rb index cc539005..04bdfda3 100644 --- a/lib/awesome_print/types.rb +++ b/lib/awesome_print/types.rb @@ -1,6 +1,6 @@ module AwesomePrint module Types - require 'awesome_print/types/base' + require 'awesome_print/types/type' require 'awesome_print/types/active_record' if defined?(ActiveRecord) || AwesomePrint.rails_console? require 'awesome_print/types/active_support' if defined?(ActiveSupport) || AwesomePrint.rails_console? require 'awesome_print/types/mongoid' if defined?(Mongoid) diff --git a/lib/awesome_print/types/active_record.rb b/lib/awesome_print/types/active_record.rb index 1dd05a1b..edac9ac6 100644 --- a/lib/awesome_print/types/active_record.rb +++ b/lib/awesome_print/types/active_record.rb @@ -1,6 +1,6 @@ module AwesomePrint module Types - class ActiveRecord < Base + class ActiveRecord < Type def call if object.is_a?(::ActiveRecord::Base) diff --git a/lib/awesome_print/types/active_support.rb b/lib/awesome_print/types/active_support.rb index b67ef7fb..e0b4a00b 100644 --- a/lib/awesome_print/types/active_support.rb +++ b/lib/awesome_print/types/active_support.rb @@ -4,7 +4,7 @@ module AwesomePrint module Types - class ActiveSupport < Base + class ActiveSupport < Type def call if (defined?(::ActiveSupport::TimeWithZone) && object.is_a?(::ActiveSupport::TimeWithZone)) || object.is_a?(::Date) diff --git a/lib/awesome_print/types/mongo_mapper.rb b/lib/awesome_print/types/mongo_mapper.rb index b9bc9389..f51899db 100644 --- a/lib/awesome_print/types/mongo_mapper.rb +++ b/lib/awesome_print/types/mongo_mapper.rb @@ -1,6 +1,6 @@ module AwesomePrint module Types - class MongoMapper < Base + class MongoMapper < Type def call if object.is_a?(Class) && (object.ancestors & [ ::MongoMapper::Document, ::MongoMapper::EmbeddedDocument ]).size > 0 diff --git a/lib/awesome_print/types/mongoid.rb b/lib/awesome_print/types/mongoid.rb index 2650b944..235d3177 100644 --- a/lib/awesome_print/types/mongoid.rb +++ b/lib/awesome_print/types/mongoid.rb @@ -1,6 +1,6 @@ module AwesomePrint module Types - class Mongoid < Base + class Mongoid < Type def call if object.is_a?(Class) && object.ancestors.include?(::Mongoid::Document) diff --git a/lib/awesome_print/types/no_brainer.rb b/lib/awesome_print/types/no_brainer.rb index 8b17c943..d22362fd 100644 --- a/lib/awesome_print/types/no_brainer.rb +++ b/lib/awesome_print/types/no_brainer.rb @@ -1,6 +1,6 @@ module AwesomePrint module Types - class NoBrainer < Base + class NoBrainer < Type def call if object.is_a?(Class) && object < ::NoBrainer::Document diff --git a/lib/awesome_print/types/nokogiri.rb b/lib/awesome_print/types/nokogiri.rb index e53e56ef..2f7b2e0b 100644 --- a/lib/awesome_print/types/nokogiri.rb +++ b/lib/awesome_print/types/nokogiri.rb @@ -1,6 +1,6 @@ module AwesomePrint module Types - class Nokogiri < Base + class Nokogiri < Type def call if (defined?(::Nokogiri::XML::Node) && object.is_a?(::Nokogiri::XML::Node)) || diff --git a/lib/awesome_print/types/ostruct.rb b/lib/awesome_print/types/ostruct.rb index 8c720c25..cc55d5a5 100644 --- a/lib/awesome_print/types/ostruct.rb +++ b/lib/awesome_print/types/ostruct.rb @@ -1,6 +1,6 @@ module AwesomePrint module Types - class OpenStruct < Base + class OpenStruct < Type def call if (defined?(::OpenStruct)) && (object.is_a?(::OpenStruct)) diff --git a/lib/awesome_print/types/ripple.rb b/lib/awesome_print/types/ripple.rb index e6cd8f56..33874a26 100644 --- a/lib/awesome_print/types/ripple.rb +++ b/lib/awesome_print/types/ripple.rb @@ -1,6 +1,6 @@ module AwesomePrint module Types - class Ripple < Base + class Ripple < Type def call if object.is_a?(::Ripple::AttributeMethods) # Module used to access attributes across documents and embedded documents diff --git a/lib/awesome_print/types/sequel.rb b/lib/awesome_print/types/sequel.rb index c5dc9994..9e52f5f2 100644 --- a/lib/awesome_print/types/sequel.rb +++ b/lib/awesome_print/types/sequel.rb @@ -1,6 +1,6 @@ module AwesomePrint module Types - class Sequel < Base + class Sequel < Type def call if defined?(::Sequel::Model) && object.is_a?(::Sequel::Model) diff --git a/lib/awesome_print/types/base.rb b/lib/awesome_print/types/type.rb similarity index 91% rename from lib/awesome_print/types/base.rb rename to lib/awesome_print/types/type.rb index f11aa4ce..6c0efc20 100644 --- a/lib/awesome_print/types/base.rb +++ b/lib/awesome_print/types/type.rb @@ -1,6 +1,6 @@ module AwesomePrint module Types - class Base + class Type def initialize(object) @object = object From a4d960f1c8675779e2fd97a0563786d7c1762500 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 19 Mar 2015 18:18:19 -0300 Subject: [PATCH 56/59] Rename the option from new_hash_syntax to colonize_symbol_keys --- README.md | 18 +++++++++--------- lib/awesome_print/formatters/hash.rb | 2 +- lib/awesome_print/inspector.rb | 18 +++++++++--------- spec/formats_spec.rb | 4 ++-- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index f5e6f069..2d0e462a 100644 --- a/README.md +++ b/README.md @@ -33,15 +33,15 @@ ap object, options = {} Default options: ```ruby -:indent => 4, # Indent using 4 spaces. -:index => true, # Display array indices. -:html => false, # Use ANSI color codes rather than HTML. -:multiline => true, # Display in multiple lines. -:plain => false, # Use colors. -:raw => false, # Do not recursively format object instance variables. -:sort_keys => false, # Do not sort hash keys. -:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. -:new_hash_syntax => false, # Use the foo: 'bar' syntax, when the key is a symbol +:indent => 4, # Indent using 4 spaces. +:index => true, # Display array indices. +:html => false, # Use ANSI color codes rather than HTML. +:multiline => true, # Display in multiple lines. +:plain => false, # Use colors. +:raw => false, # Do not recursively format object instance variables. +:sort_keys => false, # Do not sort hash keys. +:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. +:colonize_symbol_keys => false, # Use the foo: 'bar' syntax, when the key is a symbol :color => { :args => :pale, :array => :white, diff --git a/lib/awesome_print/formatters/hash.rb b/lib/awesome_print/formatters/hash.rb index 217d0dc6..cf99d3be 100644 --- a/lib/awesome_print/formatters/hash.rb +++ b/lib/awesome_print/formatters/hash.rb @@ -43,7 +43,7 @@ def build_data @data = @data.map do |key, value| formatter.indented do - if options[:new_hash_syntax] && is_a_symbol?(key) + if options[:colonize_symbol_keys] && is_a_symbol?(key) new_hash_syntax_format(key, value) else old_hash_syntax_format(key, value) diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 94634a70..f9009775 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -6,15 +6,15 @@ class Inspector def initialize(options = {}) @options = { - :indent => 4, # Indent using 4 spaces. - :index => true, # Display array indices. - :html => false, # Use ANSI color codes rather than HTML. - :multiline => true, # Display in multiple lines. - :plain => false, # Use colors. - :raw => false, # Do not recursively format object instance variables. - :sort_keys => false, # Do not sort hash keys. - :limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. - :new_hash_syntax => false, # Use the foo: 'bar' syntax, when the key is a symbol + :indent => 4, # Indent using 4 spaces. + :index => true, # Display array indices. + :html => false, # Use ANSI color codes rather than HTML. + :multiline => true, # Display in multiple lines. + :plain => false, # Use colors. + :raw => false, # Do not recursively format object instance variables. + :sort_keys => false, # Do not sort hash keys. + :limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. + :colonize_symbol_keys => false, # Use the foo: 'bar' syntax, when the key is a symbol :color => { :args => :pale, :array => :white, diff --git a/spec/formats_spec.rb b/spec/formats_spec.rb index cf76cb6d..83a5124e 100644 --- a/spec/formats_spec.rb +++ b/spec/formats_spec.rb @@ -276,7 +276,7 @@ end it "new hash syntax" do - expect(@hash.ai(:plain => true, :new_hash_syntax => true)).to eq <<-EOS.strip + expect(@hash.ai(:plain => true, :colonize_symbol_keys => true)).to eq <<-EOS.strip { 1 => { sym: { @@ -330,7 +330,7 @@ end it "colored with new hash syntax" do - expect(@hash.ai(:new_hash_syntax => true)).to eq <<-EOS.strip + expect(@hash.ai(:colonize_symbol_keys => true)).to eq <<-EOS.strip { 1\e[0;37m => \e[0m{ sym\e[0;37m: \e[0m{ From 1dd896fceed845fd58eddcf55483a4fd13434731 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 19 Mar 2015 18:20:59 -0300 Subject: [PATCH 57/59] Create AwesomePrint::FormatterFactory.from --- lib/awesome_print/formatter.rb | 2 +- lib/awesome_print/formatter_factory.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 7dd8297b..c464017a 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -19,7 +19,7 @@ def initialize(inspector) def format(object) @type = printable(object) @object = object - AwesomePrint::FormatterFactory.new(self, object).call + AwesomePrint::FormatterFactory.from(self, object) end # Pick the color and apply it to the given string as necessary. diff --git a/lib/awesome_print/formatter_factory.rb b/lib/awesome_print/formatter_factory.rb index 98e63469..0fc22146 100644 --- a/lib/awesome_print/formatter_factory.rb +++ b/lib/awesome_print/formatter_factory.rb @@ -3,6 +3,10 @@ module AwesomePrint class FormatterFactory + def self.from(formatter, object) + new(formatter, object).call + end + def initialize(formatter, object) @type = AwesomePrint::TypeDiscover.new(formatter).call @class_name = @type.to_s.split('_').map(&:capitalize).join('') From a9c6a90d93a1a8fcd28aad70673ec18ec13fa08c Mon Sep 17 00:00:00 2001 From: Mauro George Date: Thu, 26 Mar 2015 11:59:25 -0300 Subject: [PATCH 58/59] Fix broken specs on AR 4.2.1 From the 4.2.0 to 4.2.1 the internals of AR have change. --- spec/ext/active_record_spec.rb | 90 +++++----------------------------- 1 file changed, 12 insertions(+), 78 deletions(-) diff --git a/spec/ext/active_record_spec.rb b/spec/ext/active_record_spec.rb index 860a5db2..ed2b611b 100644 --- a/spec/ext/active_record_spec.rb +++ b/spec/ext/active_record_spec.rb @@ -140,21 +140,10 @@ >, attr_reader :value = 1992-10-10 12:30:00 UTC, attr_reader :value_before_type_cast = "1992-10-10 12:30:00" - >, - "id" => #, - attr_reader :value = nil, - attr_reader :value_before_type_cast = nil >, "name" => #, - "name" => # #, attr_reader :value = 1992-10-10 12:30:00 UTC, attr_reader :value_before_type_cast = "1992-10-10 12:30:00" - >, - "id" => #, - attr_reader :value = nil, - attr_reader :value_before_type_cast = nil >, "name" => #, - "name" => # #, attr_reader :value = 1992-10-10 12:30:00 UTC, attr_reader :value_before_type_cast = "1992-10-10 12:30:00" - >, - "id" => #, - attr_reader :value = nil, - attr_reader :value_before_type_cast = nil >, "name" => #, - "name" => # #, attr_reader :value = 2003-05-26 14:15:00 UTC, attr_reader :value_before_type_cast = "2003-05-26 14:15:00" - >, - "id" => #, - attr_reader :value = nil, - attr_reader :value_before_type_cast = nil >, "name" => #, - "name" => # #, attr_reader :value = 1992-10-10 12:30:00 UTC, attr_reader :value_before_type_cast = "1992-10-10 12:30:00" - >, - "id" => #, - attr_reader :value = nil, - attr_reader :value_before_type_cast = nil >, "name" => #, - "name" => # #, attr_reader :value = 2003-05-26 14:15:00 UTC, attr_reader :value_before_type_cast = "2003-05-26 14:15:00" - >, - "id" => #, - attr_reader :value = nil, - attr_reader :value_before_type_cast = nil >, "name" => #, - "name" => # # Date: Sat, 28 Mar 2015 10:00:53 -0300 Subject: [PATCH 59/59] Automatically require all custom types --- lib/awesome_print/support.rb | 5 +++++ lib/awesome_print/type_discover.rb | 22 ++++++++++++++----- lib/awesome_print/types.rb | 14 ------------ .../types/{ostruct.rb => open_struct.rb} | 0 4 files changed, 22 insertions(+), 19 deletions(-) delete mode 100644 lib/awesome_print/types.rb rename lib/awesome_print/types/{ostruct.rb => open_struct.rb} (100%) diff --git a/lib/awesome_print/support.rb b/lib/awesome_print/support.rb index a4b9a03a..496855ec 100644 --- a/lib/awesome_print/support.rb +++ b/lib/awesome_print/support.rb @@ -26,5 +26,10 @@ def self.constantize(camel_cased_word) end end end + + def self.camelize(term) + term = term.capitalize + term.gsub(/(?:_|(\/))([a-z\d]*)/i) { $2.capitalize } + end end end diff --git a/lib/awesome_print/type_discover.rb b/lib/awesome_print/type_discover.rb index 6723a7c3..d543a751 100644 --- a/lib/awesome_print/type_discover.rb +++ b/lib/awesome_print/type_discover.rb @@ -1,15 +1,15 @@ -require 'awesome_print/types' +require 'awesome_print/types/type' module AwesomePrint class TypeDiscover BUILT_IN_TYPES = [ :array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod ] - CUSTOM_TYPES = %w(ActiveRecord ActiveSupport Mongoid MongoMapper NoBrainer Nokogiri - OpenStruct Ripple Sequel) def initialize(formatter) @type = formatter.type @object = formatter.object + @custom_types = [] + require_custom_types end def call @@ -18,10 +18,10 @@ def call private - attr_reader :object, :type + attr_reader :object, :type, :custom_types def custom_type - CUSTOM_TYPES.map do |type| + custom_types.map do |type| begin klass = AwesomePrint::Support.constantize("AwesomePrint::Types::#{type}") klass.new(object).call @@ -34,5 +34,17 @@ def custom_type def built_in_type BUILT_IN_TYPES.grep(type)[0] end + + def require_custom_types + Dir[File.dirname(__FILE__) + '/types/*.rb'].each do |file| + add_custom_type(file) + require file + end + end + + def add_custom_type(file) + file_name = File.basename(file, '.rb') + @custom_types << AwesomePrint::Support.camelize(file_name) + end end end diff --git a/lib/awesome_print/types.rb b/lib/awesome_print/types.rb deleted file mode 100644 index 04bdfda3..00000000 --- a/lib/awesome_print/types.rb +++ /dev/null @@ -1,14 +0,0 @@ -module AwesomePrint - module Types - require 'awesome_print/types/type' - require 'awesome_print/types/active_record' if defined?(ActiveRecord) || AwesomePrint.rails_console? - require 'awesome_print/types/active_support' if defined?(ActiveSupport) || AwesomePrint.rails_console? - require 'awesome_print/types/mongoid' if defined?(Mongoid) - require 'awesome_print/types/mongo_mapper' if defined?(MongoMapper) - require 'awesome_print/types/no_brainer' if defined?(NoBrainer) - require 'awesome_print/types/nokogiri' if defined?(Nokogiri) - require 'awesome_print/types/ostruct' if defined?(OpenStruct) - require 'awesome_print/types/ripple' if defined?(Ripple) - require 'awesome_print/types/sequel' if defined?(Sequel) - end -end diff --git a/lib/awesome_print/types/ostruct.rb b/lib/awesome_print/types/open_struct.rb similarity index 100% rename from lib/awesome_print/types/ostruct.rb rename to lib/awesome_print/types/open_struct.rb