1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Automatically require all custom types

This commit is contained in:
Mauro George 2015-03-28 10:00:53 -03:00
parent a9c6a90d93
commit aef581da18
4 changed files with 22 additions and 19 deletions

View file

@ -26,5 +26,10 @@ module AwesomePrint
end end
end end
end end
def self.camelize(term)
term = term.capitalize
term.gsub(/(?:_|(\/))([a-z\d]*)/i) { $2.capitalize }
end
end end
end end

View file

@ -1,15 +1,15 @@
require 'awesome_print/types' require 'awesome_print/types/type'
module AwesomePrint module AwesomePrint
class TypeDiscover class TypeDiscover
BUILT_IN_TYPES = [ :array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod ] 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) def initialize(formatter)
@type = formatter.type @type = formatter.type
@object = formatter.object @object = formatter.object
@custom_types = []
require_custom_types
end end
def call def call
@ -18,10 +18,10 @@ module AwesomePrint
private private
attr_reader :object, :type attr_reader :object, :type, :custom_types
def custom_type def custom_type
CUSTOM_TYPES.map do |type| custom_types.map do |type|
begin begin
klass = AwesomePrint::Support.constantize("AwesomePrint::Types::#{type}") klass = AwesomePrint::Support.constantize("AwesomePrint::Types::#{type}")
klass.new(object).call klass.new(object).call
@ -34,5 +34,17 @@ module AwesomePrint
def built_in_type def built_in_type
BUILT_IN_TYPES.grep(type)[0] BUILT_IN_TYPES.grep(type)[0]
end 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
end end

View file

@ -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