diff --git a/awesome_print.gemspec b/awesome_print.gemspec index 8059d38..f758e47 100644 --- a/awesome_print.gemspec +++ b/awesome_print.gemspec @@ -4,10 +4,12 @@ # See LICENSE file or http://www.opensource.org/licenses/mit-license.php #------------------------------------------------------------------------------ +$:.push File.expand_path('../lib', __FILE__) +require 'awesome_print/version' + Gem::Specification.new do |s| s.name = "awesome_print" - s.version = "1.7.0" -# s.platform = Gem::Platform::RUBY + s.version = AwesomePrint.version s.authors = "Michael Dvorkin" s.date = Time.now.strftime("%Y-%m-%d") s.email = "mike@dvorkin.net" diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index f7d1d4d..d769ac1 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -9,32 +9,33 @@ # 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/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/custom_defaults' + require 'awesome_print/inspector' + require 'awesome_print/formatter' + require 'awesome_print/version' + require '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_record" if defined?(ActiveRecord) || AwesomePrint.rails_console? - require File.dirname(__FILE__) + "/awesome_print/ext/active_support" if defined?(ActiveSupport) || AwesomePrint.rails_console? + require 'awesome_print/ext/active_record' if defined?(ActiveRecord) || AwesomePrint.rails_console? + require 'awesome_print/ext/active_support' if defined?(ActiveSupport) || AwesomePrint.rails_console? # # Load remaining extensions. # if defined?(ActiveSupport.on_load) ActiveSupport.on_load(:action_view) do - require File.dirname(__FILE__) + "/awesome_print/ext/action_view" + require '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/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) - require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel) - require File.dirname(__FILE__) + "/awesome_print/ext/ostruct" if defined?(OpenStruct) + require 'awesome_print/ext/mongo_mapper' if defined?(MongoMapper) + require 'awesome_print/ext/mongoid' if defined?(Mongoid) + require 'awesome_print/ext/nokogiri' if defined?(Nokogiri) + require 'awesome_print/ext/nobrainer' if defined?(NoBrainer) + require 'awesome_print/ext/ripple' if defined?(Ripple) + require 'awesome_print/ext/sequel' if defined?(Sequel) + require 'awesome_print/ext/ostruct' if defined?(OpenStruct) end diff --git a/lib/awesome_print/custom_defaults.rb b/lib/awesome_print/custom_defaults.rb new file mode 100644 index 0000000..9b5bea7 --- /dev/null +++ b/lib/awesome_print/custom_defaults.rb @@ -0,0 +1,48 @@ +module AwesomePrint + class << self + 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 diet_rb + IRB.formatter = Class.new(IRB::Formatter) do + def inspect_object(object) + object.ai + end + end.new + end + + def usual_rb + IRB::Irb.class_eval do + def output_value + ap @context.last_value + rescue NoMethodError + puts "(Object doesn't support #ai)" + end + end + end + + def irb! + return unless defined?(IRB) + + IRB.version.include?("DietRB") ? diet_rb : usual_rb + end + + def pry! + Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry) + end + end +end diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 99e02e6..96e96ef 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -3,15 +3,7 @@ # Awesome Print is freely distributable under the terms of MIT license. # See LICENSE file or http://www.opensource.org/licenses/mit-license.php #------------------------------------------------------------------------------ -require_relative "formatters/object_formatter" -require_relative "formatters/hash_formatter" -require_relative "formatters/array_formatter" -require_relative "formatters/simple_formatter" -require_relative "formatters/method_formatter" -require_relative "formatters/class_formatter" -require_relative "formatters/dir_formatter" -require_relative "formatters/file_formatter" -require_relative "colorize" +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 0000000..f3e11de --- /dev/null +++ b/lib/awesome_print/formatters.rb @@ -0,0 +1,14 @@ +module AwesomePrint + module Formatters + require 'awesome_print/formatters/object_formatter' + require 'awesome_print/formatters/hash_formatter' + require 'awesome_print/formatters/array_formatter' + require 'awesome_print/formatters/simple_formatter' + require 'awesome_print/formatters/method_formatter' + require 'awesome_print/formatters/class_formatter' + require 'awesome_print/formatters/dir_formatter' + require 'awesome_print/formatters/file_formatter' + require 'awesome_print/colorize' + end +end + diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 9caa512..9da697d 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -6,48 +6,6 @@ require_relative 'indentator' 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 - rescue NoMethodError - puts "(Object doesn't support #ai)" - end - end - else # MacRuby - IRB.formatter = Class.new(IRB::Formatter) do - def inspect_object(object) - object.ai - end - end.new - end - end - - def pry! - Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry) - end - end - class Inspector attr_accessor :options, :indentator