class AmazingPrint::Formatter

Constants

CORE_FORMATTERS

Attributes

inspector[R]
options[R]

Public Class Methods

new(inspector) click to toggle source
# File lib/amazing_print/formatter.rb, line 18
def initialize(inspector)
  @inspector   = inspector
  @options     = inspector.options
end

Public Instance Methods

cast(_object, type) click to toggle source

Hook this when adding custom formatters. Check out lib/amazing_print/ext directory for custom formatters that ship with amazing_print.

# File lib/amazing_print/formatter.rb, line 37
def cast(_object, type)
  CORE_FORMATTERS.include?(type) ? type : :self
end
format(object, type = nil) click to toggle source

Main entry point to format an object.

# File lib/amazing_print/formatter.rb, line 25
def format(object, type = nil)
  core_class = cast(object, type)
  if core_class == :self
    awesome_self(object, type) # Catch all that falls back to object.inspect.
  else
    send(:"awesome_#{core_class}", object) # Core formatters.
  end
end

Private Instance Methods

awesome_array(a) click to toggle source
# File lib/amazing_print/formatter.rb, line 71
def awesome_array(a)
  Formatters::ArrayFormatter.new(a, @inspector).format
end
awesome_bigdecimal(n) click to toggle source
# File lib/amazing_print/formatter.rb, line 55
def awesome_bigdecimal(n)
  o = n.to_s('F')
  type = :bigdecimal
  awesome_simple(o, type, @inspector)
end
awesome_class(c) click to toggle source
# File lib/amazing_print/formatter.rb, line 96
def awesome_class(c)
  Formatters::ClassFormatter.new(c, @inspector).format
end
awesome_dir(d) click to toggle source
# File lib/amazing_print/formatter.rb, line 104
def awesome_dir(d)
  Formatters::DirFormatter.new(d, @inspector).format
end
awesome_file(f) click to toggle source
# File lib/amazing_print/formatter.rb, line 100
def awesome_file(f)
  Formatters::FileFormatter.new(f, @inspector).format
end
awesome_hash(h) click to toggle source
# File lib/amazing_print/formatter.rb, line 79
def awesome_hash(h)
  Formatters::HashFormatter.new(h, @inspector).format
end
awesome_method(m) click to toggle source
# File lib/amazing_print/formatter.rb, line 91
def awesome_method(m)
  Formatters::MethodFormatter.new(m, @inspector).format
end
Also aliased as: awesome_unboundmethod
awesome_object(o) click to toggle source
# File lib/amazing_print/formatter.rb, line 83
def awesome_object(o)
  Formatters::ObjectFormatter.new(o, @inspector).format
end
awesome_rational(n) click to toggle source
# File lib/amazing_print/formatter.rb, line 61
def awesome_rational(n)
  o = n.to_s
  type = :rational
  awesome_simple(o, type, @inspector)
end
awesome_self(object, type) click to toggle source

Catch all method to format an arbitrary object.

# File lib/amazing_print/formatter.rb, line 45
def awesome_self(object, type)
  if @options[:raw] && object.instance_variables.any?
    awesome_object(object)
  elsif (hash = convert_to_hash(object))
    awesome_hash(hash)
  else
    awesome_simple(object.inspect.to_s, type, @inspector)
  end
end
awesome_set(s) click to toggle source
# File lib/amazing_print/formatter.rb, line 75
def awesome_set(s)
  Formatters::ArrayFormatter.new(s.to_a, @inspector).format
end
awesome_simple(o, type, inspector = @inspector) click to toggle source
# File lib/amazing_print/formatter.rb, line 67
def awesome_simple(o, type, inspector = @inspector)
  AmazingPrint::Formatters::SimpleFormatter.new(o, type, inspector).format
end
awesome_struct(s) click to toggle source
# File lib/amazing_print/formatter.rb, line 87
def awesome_struct(s)
  Formatters::StructFormatter.new(s, @inspector).format
end
awesome_unboundmethod(m)
Alias for: awesome_method
convert_to_hash(object) click to toggle source

Utility methods.

# File lib/amazing_print/formatter.rb, line 110
def convert_to_hash(object)
  return nil unless object.respond_to?(:to_hash)

  return nil if object.method(:to_hash).arity != 0

  # ActionController::Parameters will raise if they are not yet permitted and
  # we try to convert to hash.
  # https://api.rubyonrails.org/classes/ActionController/Parameters.html
  return nil if object.respond_to?(:permitted?) && !object.permitted?

  hash = object.to_hash
  return nil if !hash.respond_to?(:keys) || !hash.respond_to?(:[])

  hash
end