From aef581da18a39273ae5c9975ad32a5a5b8a31d1f Mon Sep 17 00:00:00 2001 From: Mauro George Date: Sat, 28 Mar 2015 10:00:53 -0300 Subject: [PATCH] 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 a4b9a03..496855e 100644 --- a/lib/awesome_print/support.rb +++ b/lib/awesome_print/support.rb @@ -26,5 +26,10 @@ module AwesomePrint 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 6723a7c..d543a75 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 @@ module AwesomePrint 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 @@ module AwesomePrint 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 04bdfda..0000000 --- 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