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

Move some methods to the right place

This commit is contained in:
Mauro George 2015-03-18 18:38:58 -03:00
parent 7695cfcbe6
commit a9629ba578
5 changed files with 69 additions and 66 deletions

View file

@ -6,10 +6,7 @@ require 'awesome_print/type_discover'
module AwesomePrint module AwesomePrint
class Formatter class Formatter
CORE = [ :array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod ] attr_reader :options, :inspector, :indentation, :type, :object
DEFAULT_LIMIT_SIZE = 7
attr_reader :options, :inspector, :indentation, :type
def initialize(inspector) def initialize(inspector)
@inspector = inspector @inspector = inspector
@ -19,17 +16,10 @@ module AwesomePrint
# Main entry point to format an object. # Main entry point to format an object.
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
def format(object, type = nil) def format(object)
@type = type @type = printable(object)
core_class = cast(object, type) @object = object
AwesomePrint::FormatterFactory.new(core_class, self, object).call AwesomePrint::FormatterFactory.new(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
end end
# Pick the color and apply it to the given string as necessary. # Pick the color and apply it to the given string as necessary.
@ -85,5 +75,39 @@ module AwesomePrint
ensure ensure
@options[:indent] = current @options[:indent] = current
end 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
end end

View file

@ -3,8 +3,9 @@ require 'awesome_print/formatters'
module AwesomePrint module AwesomePrint
class FormatterFactory class FormatterFactory
def initialize(class_name, formatter, object) def initialize(formatter, object)
@class_name = class_name.to_s.split('_').map(&:capitalize).join('') @type = AwesomePrint::TypeDiscover.new(formatter).call
@class_name = @type.to_s.split('_').map(&:capitalize).join('')
@formatter = formatter @formatter = formatter
@object = object @object = object
end end

View file

@ -2,6 +2,8 @@ module AwesomePrint
module Formatters module Formatters
module Enumerable module Enumerable
DEFAULT_LIMIT_SIZE = 7
# To support limited output, for example: # To support limited output, for example:
# #
# ap ('a'..'z').to_a, :limit => 3 # ap ('a'..'z').to_a, :limit => 3
@ -51,7 +53,7 @@ module AwesomePrint
end end
def get_limit_size 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 end
end end

View file

@ -49,11 +49,11 @@ module AwesomePrint
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
def awesome(object) def awesome(object)
if Thread.current[AP].include?(object.object_id) if Thread.current[AP].include?(object.object_id)
nested(object) @formatter.nested(object)
else else
begin begin
Thread.current[AP] << object.object_id Thread.current[AP] << object.object_id
unnested(object) @formatter.unnested(object)
ensure ensure
Thread.current[AP].pop Thread.current[AP].pop
end end
@ -69,40 +69,6 @@ module AwesomePrint
private 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. # Update @options by first merging the :color hash and then the remaining keys.
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
def merge_options!(options = {}) def merge_options!(options = {})

View file

@ -3,26 +3,36 @@ require 'awesome_print/types'
module AwesomePrint module AwesomePrint
class TypeDiscover 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) OpenStruct Ripple Sequel)
def initialize(object) def initialize(formatter)
@object = object @type = formatter.type
@object = formatter.object
end end
def call def call
TYPES.map do |type| custom_type || built_in_type || :self
begin
klass = AwesomePrint::Support.constantize("AwesomePrint::Types::#{type}")
klass.new(object).call
rescue NameError
nil
end
end.detect { |type| !type.nil? }
end end
private 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
end end