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:
parent
7695cfcbe6
commit
a9629ba578
5 changed files with 69 additions and 66 deletions
|
@ -6,10 +6,7 @@ require 'awesome_print/type_discover'
|
|||
module AwesomePrint
|
||||
class Formatter
|
||||
|
||||
CORE = [ :array, :bigdecimal, :class, :dir, :file, :hash, :method, :rational, :set, :struct, :unboundmethod ]
|
||||
DEFAULT_LIMIT_SIZE = 7
|
||||
|
||||
attr_reader :options, :inspector, :indentation, :type
|
||||
attr_reader :options, :inspector, :indentation, :type, :object
|
||||
|
||||
def initialize(inspector)
|
||||
@inspector = inspector
|
||||
|
@ -19,17 +16,10 @@ module AwesomePrint
|
|||
|
||||
# Main entry point to format an object.
|
||||
#------------------------------------------------------------------------------
|
||||
def format(object, type = nil)
|
||||
@type = type
|
||||
core_class = cast(object, type)
|
||||
AwesomePrint::FormatterFactory.new(core_class, 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
|
||||
def format(object)
|
||||
@type = printable(object)
|
||||
@object = object
|
||||
AwesomePrint::FormatterFactory.new(self, object).call
|
||||
end
|
||||
|
||||
# Pick the color and apply it to the given string as necessary.
|
||||
|
@ -85,5 +75,39 @@ module AwesomePrint
|
|||
ensure
|
||||
@options[:indent] = current
|
||||
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
|
||||
|
|
|
@ -3,8 +3,9 @@ require 'awesome_print/formatters'
|
|||
module AwesomePrint
|
||||
class FormatterFactory
|
||||
|
||||
def initialize(class_name, formatter, object)
|
||||
@class_name = class_name.to_s.split('_').map(&:capitalize).join('')
|
||||
def initialize(formatter, object)
|
||||
@type = AwesomePrint::TypeDiscover.new(formatter).call
|
||||
@class_name = @type.to_s.split('_').map(&:capitalize).join('')
|
||||
@formatter = formatter
|
||||
@object = object
|
||||
end
|
||||
|
|
|
@ -2,6 +2,8 @@ module AwesomePrint
|
|||
module Formatters
|
||||
module Enumerable
|
||||
|
||||
DEFAULT_LIMIT_SIZE = 7
|
||||
|
||||
# To support limited output, for example:
|
||||
#
|
||||
# ap ('a'..'z').to_a, :limit => 3
|
||||
|
@ -51,7 +53,7 @@ module AwesomePrint
|
|||
end
|
||||
|
||||
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
|
||||
|
|
|
@ -49,11 +49,11 @@ module AwesomePrint
|
|||
#------------------------------------------------------------------------------
|
||||
def awesome(object)
|
||||
if Thread.current[AP].include?(object.object_id)
|
||||
nested(object)
|
||||
@formatter.nested(object)
|
||||
else
|
||||
begin
|
||||
Thread.current[AP] << object.object_id
|
||||
unnested(object)
|
||||
@formatter.unnested(object)
|
||||
ensure
|
||||
Thread.current[AP].pop
|
||||
end
|
||||
|
@ -69,40 +69,6 @@ module AwesomePrint
|
|||
|
||||
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.
|
||||
#------------------------------------------------------------------------------
|
||||
def merge_options!(options = {})
|
||||
|
|
|
@ -3,26 +3,36 @@ require 'awesome_print/types'
|
|||
module AwesomePrint
|
||||
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)
|
||||
|
||||
def initialize(object)
|
||||
@object = object
|
||||
def initialize(formatter)
|
||||
@type = formatter.type
|
||||
@object = formatter.object
|
||||
end
|
||||
|
||||
def call
|
||||
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? }
|
||||
custom_type || built_in_type || :self
|
||||
end
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue