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:
parent
a9c6a90d93
commit
aef581da18
4 changed files with 22 additions and 19 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
Loading…
Reference in a new issue